-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge cost-tour branch. didnt really do a cost tour, but did some oth…
…er cool stuff.
- Loading branch information
Showing
21 changed files
with
19,114 additions
and
164 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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const EventBus = function EventBus() { | ||
const event_store = {}; | ||
|
||
function eachev(events, looper) { | ||
(Array.isArray(events) ? events : [events]).forEach(looper); | ||
} | ||
|
||
const on = (events, fn) => { | ||
eachev(events, (ev) => { | ||
event_store[ev] || (event_store[ev] = []); | ||
event_store[ev].push(fn); | ||
}); | ||
}; | ||
|
||
const once = (events, fn) => { | ||
eachev(events, (ev) => { | ||
const newfn = (...args) => { | ||
fn(...args); | ||
off(ev, newfn); | ||
}; | ||
on(ev, newfn); | ||
}); | ||
}; | ||
|
||
const off = (events, fn) => { | ||
eachev(events, (ev) => { | ||
if(!event_store[ev]) return; | ||
if(!fn) { | ||
delete event_store[ev]; | ||
return; | ||
} | ||
event_store[ev] = event_store[ev] | ||
.filter((fn_comp) => fn !== fn_comp); | ||
}); | ||
}; | ||
|
||
const emit = (event, ...args) => { | ||
const fnlist = event_store[event] || []; | ||
fnlist.forEach((fn) => { | ||
fn(...args); | ||
}); | ||
}; | ||
|
||
const destroy = () => { | ||
Object.keys(event_store).forEach((k) => delete event_store[k]); | ||
}; | ||
|
||
return { | ||
on, | ||
once, | ||
off, | ||
emit, | ||
destroy, | ||
} | ||
}; | ||
|
Oops, something went wrong.