-
Notifications
You must be signed in to change notification settings - Fork 78
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
5 changed files
with
72 additions
and
25 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 |
---|---|---|
|
@@ -4,6 +4,10 @@ define([ | |
) { | ||
class EventEmitter { | ||
constructor() { | ||
this.init(); | ||
} | ||
|
||
init() { | ||
this._handlers = {}; | ||
} | ||
|
||
|
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,33 @@ | ||
/*globals define*/ | ||
define([ | ||
'deepforge/EventEmitter', | ||
'deepforge/utils', | ||
], function( | ||
EventEmitter, | ||
utils, | ||
) { | ||
class PromiseEvents extends Promise { | ||
constructor(fn) { | ||
super(fn); | ||
this.init(); | ||
} | ||
|
||
static new(fn) { | ||
let promise; | ||
promise = new PromiseEvents(async function(resolve, reject) { | ||
await utils.yield(); | ||
return fn.call(promise, resolve, reject); | ||
}); | ||
return promise; | ||
} | ||
} | ||
|
||
const methods = Object.getOwnPropertyNames(EventEmitter.prototype) | ||
.filter(fn => !PromiseEvents.prototype[fn]); | ||
|
||
methods.forEach(fn => { | ||
PromiseEvents.prototype[fn] = EventEmitter.prototype[fn]; | ||
}); | ||
|
||
return PromiseEvents; | ||
}); |
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,26 @@ | ||
describe('PromiseEvents', function() { | ||
const assert = require('assert'); | ||
const testFixture = require('../../globals'); | ||
const PromiseEvents = testFixture.requirejs('deepforge/PromiseEvents'); | ||
|
||
it('should resolve as a promise', async function() { | ||
const five = await PromiseEvents.new( | ||
resolve => setTimeout(() => resolve(5), 5) | ||
); | ||
assert.equal(five, 5); | ||
}); | ||
|
||
it('should support updates', async function() { | ||
const promise = PromiseEvents.new(function(resolve) { | ||
for (let i = 1; i < 6; i++) { | ||
this.emit('update', i); | ||
} | ||
resolve(6); | ||
}); | ||
const events = []; | ||
promise.on('update', status => events.push(status)); | ||
const six = await promise; | ||
assert.equal(events.length, 5); | ||
assert.equal(six, 6); | ||
}); | ||
}); |