-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bundle): update exports to ease bundling
- Loading branch information
1 parent
13445f5
commit 283485a
Showing
7 changed files
with
136 additions
and
110 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,64 @@ | ||
'use strict' | ||
|
||
const names = 'created,ready,incoming,error,redirecting,redirected,aborted,closed'.split(',') | ||
const events = {} | ||
names.forEach((name, index) => { | ||
events['EVENT_' + name.toUpperCase()] = index | ||
}) | ||
|
||
module.exports = { | ||
...events, | ||
|
||
newEventEmitter () { | ||
const registry = [] | ||
function newEventEmitter () { | ||
const registry = [] | ||
|
||
const register = (event, callback) => { | ||
const eventIndex = names.indexOf(event) | ||
if (eventIndex === -1) { | ||
throw new Error('Unknown event name') | ||
} | ||
if (typeof callback !== 'function') { | ||
throw new Error('Invalid callback') | ||
} | ||
if (registry[eventIndex] === undefined) { | ||
registry[eventIndex] = [] | ||
} | ||
registry[eventIndex].push(callback) | ||
const register = (event, callback) => { | ||
const eventIndex = names.indexOf(event) | ||
if (eventIndex === -1) { | ||
throw new Error('Unknown event name') | ||
} | ||
if (typeof callback !== 'function') { | ||
throw new Error('Invalid callback') | ||
} | ||
if (registry[eventIndex] === undefined) { | ||
registry[eventIndex] = [] | ||
} | ||
registry[eventIndex].push(callback) | ||
} | ||
|
||
const on = function (event, callback) { | ||
if (event === '*') { | ||
names.forEach(name => register(name, callback)) | ||
} else { | ||
register(event, callback) | ||
} | ||
return this | ||
const on = function (event, callback) { | ||
if (event === '*') { | ||
names.forEach(name => register(name, callback)) | ||
} else { | ||
register(event, callback) | ||
} | ||
return this | ||
} | ||
|
||
const emit = (eventIndex, ...parameters) => { | ||
const callbacks = registry[eventIndex] | ||
if (callbacks !== undefined) { | ||
const event = Object.assign({ | ||
eventName: names[eventIndex] | ||
}, ...parameters) | ||
try { | ||
for (const callback of callbacks) { | ||
callback(event) | ||
} | ||
} catch (e) { | ||
if (eventIndex === events.EVENT_CREATED) { | ||
throw e | ||
} | ||
const emit = (eventIndex, ...parameters) => { | ||
const callbacks = registry[eventIndex] | ||
if (callbacks !== undefined) { | ||
const event = Object.assign({ | ||
eventName: names[eventIndex] | ||
}, ...parameters) | ||
try { | ||
for (const callback of callbacks) { | ||
callback(event) | ||
} | ||
} catch (e) { | ||
if (eventIndex === 0) { | ||
throw e | ||
} | ||
return callbacks.length | ||
} | ||
return 0 | ||
return callbacks.length | ||
} | ||
|
||
return { on, emit } | ||
return 0 | ||
} | ||
|
||
return { on, emit } | ||
} | ||
|
||
module.exports = { | ||
EVENT_CREATED: 0, | ||
EVENT_READY: 1, | ||
EVENT_INCOMING: 2, | ||
EVENT_ERROR: 3, | ||
EVENT_REDIRECTING: 4, | ||
EVENT_REDIRECTED: 5, | ||
EVENT_ABORTED: 6, | ||
EVENT_CLOSED: 7, | ||
newEventEmitter | ||
} |
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
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