-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make npm scripts work under Windows * fix quotes of argument to uglify * not sure whether specifying the path node_modules/mocha/bin/ is an antipattern (some comments by npm authors claim so) but istanbul does not seem to take the PATH into account * generalise actions * fixes and additions to test suite * enable ES6 syntax for profiling just use "npm run build-es6" instead of "npm run build" and the /dist/creed.js file will contain native ES6 except for the module syntax benchmarks will automatically use it instead of /src/main.js as entry point * always resume coroutines asynchronously closes #31.
- Loading branch information
1 parent
0f16a8e
commit 42a81f0
Showing
30 changed files
with
516 additions
and
188 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
experiments/ | ||
node_modules/ | ||
coverage/ | ||
perf/logs/ |
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
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
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,29 @@ | ||
export default class Action { | ||
constructor (promise) { | ||
this.promise = promise | ||
} | ||
|
||
// default onFulfilled action | ||
/* istanbul ignore next */ | ||
fulfilled (p) { | ||
this.promise._become(p) | ||
} | ||
|
||
// default onRejected action | ||
rejected (p) { | ||
this.promise._become(p) | ||
return false | ||
} | ||
|
||
tryCall (f, x) { | ||
let result | ||
// test if `f` (and only it) throws | ||
try { | ||
result = f(x) | ||
} catch (e) { | ||
this.promise._reject(e) | ||
return | ||
} // else | ||
this.handle(result) | ||
} | ||
} |
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,34 +1,26 @@ | ||
import Action from './Action' | ||
import maybeThenable from './maybeThenable' | ||
|
||
export default function (f, p, promise) { | ||
p._when(new Chain(f, promise)) | ||
return promise | ||
} | ||
|
||
class Chain { | ||
class Chain extends Action { | ||
constructor (f, promise) { | ||
super(promise) | ||
this.f = f | ||
this.promise = promise | ||
} | ||
|
||
fulfilled (p) { | ||
try { | ||
runChain(this.f, p.value, this.promise) | ||
} catch (e) { | ||
this.promise._reject(e) | ||
} | ||
this.tryCall(this.f, p.value) | ||
} | ||
|
||
rejected (p) { | ||
this.promise._become(p) | ||
} | ||
} | ||
handle (y) { | ||
if (!(maybeThenable(y) && typeof y.then === 'function')) { | ||
this.promise._reject(new TypeError('f must return a promise')) | ||
} | ||
|
||
function runChain (f, x, p) { | ||
const y = f(x) | ||
if (!(maybeThenable(y) && typeof y.then === 'function')) { | ||
throw new TypeError('f must return a promise') | ||
this.promise._resolve(y) | ||
} | ||
|
||
p._resolve(y) | ||
} |
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,41 +1,47 @@ | ||
import Action from './Action' | ||
|
||
export default function (resolve, iterator, promise) { | ||
new Coroutine(resolve, iterator, promise).run() | ||
return promise | ||
} | ||
|
||
class Coroutine { | ||
class Coroutine extends Action { | ||
constructor (resolve, iterator, promise) { | ||
super(promise) | ||
this.resolve = resolve | ||
this.iterator = iterator | ||
this.promise = promise | ||
this.generator = iterator | ||
} | ||
|
||
run () { | ||
this.step(this.iterator.next, void 0) | ||
this.tryStep(this.generator.next, void 0) | ||
} | ||
|
||
step (continuation, x) { | ||
tryStep (resume, x) { | ||
let result | ||
// test if `resume` (and only it) throws | ||
try { | ||
this.handle(continuation.call(this.iterator, x)) | ||
result = resume.call(this.generator, x) | ||
} catch (e) { | ||
this.promise._reject(e) | ||
} | ||
return | ||
} // else | ||
this.handle(result) | ||
} | ||
|
||
handle (result) { | ||
if (result.done) { | ||
return this.promise._resolve(result.value) | ||
} | ||
|
||
this.resolve(result.value)._runAction(this) | ||
this.resolve(result.value)._when(this) | ||
} | ||
|
||
fulfilled (ref) { | ||
this.step(this.iterator.next, ref.value) | ||
this.tryStep(this.generator.next, ref.value) | ||
} | ||
|
||
rejected (ref) { | ||
this.step(this.iterator.throw, ref.value) | ||
this.tryStep(this.generator.throw, ref.value) | ||
return true | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
import Action from './Action' | ||
|
||
export default function (f, p, promise) { | ||
p._when(new Map(f, promise)) | ||
return promise | ||
} | ||
|
||
class Map { | ||
class Map extends Action { | ||
constructor (f, promise) { | ||
super(promise) | ||
this.f = f | ||
this.promise = promise | ||
} | ||
|
||
fulfilled (p) { | ||
try { | ||
const f = this.f | ||
this.promise._fulfill(f(p.value)) | ||
} catch (e) { | ||
this.promise._reject(e) | ||
} | ||
this.tryCall(this.f, p.value) | ||
} | ||
|
||
rejected (p) { | ||
this.promise._become(p) | ||
handle (result) { | ||
this.promise._fulfill(result) | ||
} | ||
} |
Oops, something went wrong.