-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: Expands create-listeners API to allow recursive and functions
1. Adds a Listeners class, which lets us... 2. Add Listeners recursively. So you can createListeners().add(createListeners()) 3. Also add the ability to `.add` as a plain function This moves the entire idea more towards a generic teardown utility
- Loading branch information
John Cowen
committed
Apr 8, 2019
1 parent
34ed2c4
commit 34c558c
Showing
2 changed files
with
61 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
export default function(listeners = []) { | ||
const add = function(target, event, handler) { | ||
let addEventListener = 'addEventListener'; | ||
let removeEventListener = 'removeEventListener'; | ||
if (typeof target[addEventListener] === 'undefined') { | ||
addEventListener = 'on'; | ||
removeEventListener = 'off'; | ||
class Listeners { | ||
constructor(listeners = []) { | ||
this.listeners = listeners; | ||
} | ||
add(target, event, handler) { | ||
let remove; | ||
if (typeof target === 'function') { | ||
remove = target; | ||
} else if (target instanceof Listeners) { | ||
remove = target.remove.bind(target); | ||
} else { | ||
let addEventListener = 'addEventListener'; | ||
let removeEventListener = 'removeEventListener'; | ||
if (typeof target[addEventListener] === 'undefined') { | ||
addEventListener = 'on'; | ||
removeEventListener = 'off'; | ||
} | ||
target[addEventListener](event, handler); | ||
remove = function() { | ||
target[removeEventListener](event, handler); | ||
return handler; | ||
}; | ||
} | ||
target[addEventListener](event, handler); | ||
const remove = function() { | ||
target[removeEventListener](event, handler); | ||
return handler; | ||
this.listeners.push(remove); | ||
return () => { | ||
const pos = this.listeners.findIndex(function(item) { | ||
return item === remove; | ||
}); | ||
return this.listeners.splice(pos, 1)[0](); | ||
}; | ||
listeners.push(remove); | ||
return remove; | ||
}; | ||
// TODO: Allow passing of a 'listener remove' in here | ||
// call it, find in the array and remove | ||
// Post-thoughts, pretty sure this is covered now by returning the remove | ||
// function above, use-case for wanting to use this method to remove individual | ||
// listeners is probably pretty limited, this method itself could be easily implemented | ||
// from the outside also, but I suppose its handy to keep here | ||
const remove = function() { | ||
const handlers = listeners.map(item => item()); | ||
listeners.splice(0, listeners.length); | ||
} | ||
remove() { | ||
const handlers = this.listeners.map(item => item()); | ||
this.listeners.splice(0, this.listeners.length); | ||
return handlers; | ||
}; | ||
return { | ||
add: add, | ||
remove: remove, | ||
}; | ||
} | ||
} | ||
export default function(listeners = []) { | ||
return new Listeners(listeners); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters