-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
1 deletion.
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
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,25 @@ | ||
import Promises from '@promises/core'; | ||
import fromEvent, { IEmitterLike, IFromEventOptions } from './'; | ||
|
||
Promises._setOnConstructor('fromEvent', fromEvent); | ||
|
||
export {IEmitterLike, IFromEventOptions} from './'; | ||
export default fromEvent; | ||
|
||
declare module '@promises/core' { | ||
namespace Promises { | ||
/** | ||
* @example | ||
* | ||
* ```typescript | ||
* let emitter: EventEmitter = new EventEmitter() | ||
* Promises.fromEvent(emitter, 'foo').then((data) => { | ||
* console.log(data); // data => 'bar' | ||
* }); | ||
* | ||
* emitter.emit('foo', 'bar'); | ||
* ``` | ||
*/ | ||
export function fromEvent<R>(emitter: IEmitterLike, event: string, options?: IFromEventOptions): Promises<R>; | ||
} | ||
} |
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,44 @@ | ||
/** | ||
* @module @promises/from-event | ||
* @copyright © 2017 Yisrael Eliav <yisraelx@gmail.com> (https://github.com/yisraelx) | ||
* @license MIT | ||
*/ | ||
|
||
export type IEmitterLike = { addEventListener, removeEventListener } | { addListener, removeListener } | { on, off }; | ||
|
||
export interface IFromEventOptions { | ||
multi?: boolean; | ||
reject?: boolean; | ||
} | ||
|
||
/** | ||
* @example | ||
* | ||
* ```typescript | ||
* let emitter: EventEmitter = new EventEmitter() | ||
* Promise.race([ | ||
* fromEvent(emitter, 'foo'), | ||
* fromEvent(emitter, 'error', {reject: true}) | ||
* ]).catch((error) => { | ||
* console.log(error); // error => 'some error' | ||
* }); | ||
* | ||
* emitter.emit('error', 'some error'); | ||
* ``` | ||
*/ | ||
function fromEvent<R>(emitter: IEmitterLike, event: string, options: IFromEventOptions = {}): Promise<R> { | ||
let {multi = false, reject: rejection = false} = options; | ||
|
||
let add = (emitter as any).addEventListener || (emitter as any).addListener || (emitter as any).on; | ||
let remove = (emitter as any).removeEventListener || (emitter as any).removeListener || (emitter as any).off; | ||
|
||
return new Promise((resolve, reject) => { | ||
add.call(emitter, event, function handler(...args: any[]) { | ||
remove.call(emitter, event, handler); | ||
let data: R = multi ? args : args[0]; | ||
rejection ? reject(data) : resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
export default fromEvent; |
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,37 @@ | ||
{ | ||
"name": "@promises/from-event", | ||
"version": "NEXT-PLACEHOLDER", | ||
"description": "From Event is package from Promises library", | ||
"main": "es5.js", | ||
"browser": "umd.min.js", | ||
"module": "index.js", | ||
"es2015": "index.js", | ||
"typings": "index.d.ts", | ||
"bundle": "bundle.min.js", | ||
"author": { | ||
"name": "Yisrael Eliev", | ||
"url": "https://github.com/yisraelx", | ||
"email": "yisraelx@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"promise", | ||
"promises", | ||
"utility", | ||
"modules", | ||
"async", | ||
"await", | ||
"deferred" | ||
], | ||
"homepage": "https://github.com/yisraelx/promises#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yisraelx/promises.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/yisraelx/promises/issues" | ||
}, | ||
"optionalDependencies": { | ||
"@promises/core": "^0.2.0" | ||
} | ||
} |
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,55 @@ | ||
import fromEvent from '@promises/from-event'; | ||
|
||
class Emitter { | ||
|
||
_events: { [key: string]: Function[] } = {}; | ||
|
||
on(event: string, handler) { | ||
if (!this._events[event]) this._events[event] = []; | ||
if (this._events[event].indexOf(handler) === -1) this._events[event].push(handler); | ||
} | ||
|
||
off(event: string, handler) { | ||
let handlers = this._events[event] || []; | ||
let index = handlers.indexOf(handler); | ||
if (index !== -1) handlers.splice(index, 1); | ||
} | ||
|
||
emit(event: string, ...args: any[]) { | ||
let handlers = this._events[event] || []; | ||
let {length} = handlers; | ||
for (let i = 0; i < length; i++) { | ||
let handler = handlers[i]; | ||
handler(...args); | ||
} | ||
} | ||
} | ||
|
||
describe('fromEvent', () => { | ||
|
||
it('should be resolve on emit', () => { | ||
let emitter = new Emitter(); | ||
fromEvent(emitter, 'foo').then((data) => { | ||
expect(data).toBe('bar'); | ||
}); | ||
emitter.emit('foo', 'bar'); | ||
}); | ||
|
||
it('should be reject on emit', () => { | ||
let emitter = new Emitter(); | ||
fromEvent(emitter, 'error', {reject: true}).then(() => { | ||
throw 'resolve'; | ||
}).catch((error) => { | ||
expect(error).toBe('reject'); | ||
}); | ||
emitter.emit('error', 'reject'); | ||
}); | ||
|
||
it('should be resolve multi on emit', () => { | ||
let emitter = new Emitter(); | ||
fromEvent(emitter, 'color', {multi: true}).then((args) => { | ||
expect(args).toEqual(['red', 'blue']); | ||
}); | ||
emitter.emit('color', 'red', 'blue'); | ||
}); | ||
}); |