New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Task.do #128

Merged
merged 3 commits into from Jul 10, 2017
Jump to file or symbol
Failed to load files and symbols.
+70 −0
Diff settings

Always

Just for now

@@ -0,0 +1,35 @@
//----------------------------------------------------------------------
//
// This source file is part of the Folktale project.
//
// Licensed under MIT. See LICENCE for full licence information.
// See CONTRIBUTORS for the list of contributors to the project.
//
//----------------------------------------------------------------------
const Task = require('./_task');
/*~
* stability: experimental
* type: |
* forall v, e: (GeneratorInstance [Task e v Any]) => Any => Task e [v] Any
*/
const runGenerator = (generator, value) => (resolver) => {
const { value: task, done } = generator.next(value);
task.run().listen({
onCancelled: resolver.cancel,
onResolved: (result) =>
!done ? runGenerator(generator, result)(resolver)
: resolver.resolve(result),
onRejected: resolver.reject
});
};
/*~
* stability: experimental
* type: |
* forall v, e: (Generator [Task e v Any]) => Task e [v] Any
*/
const taskDo = generator => new Task(runGenerator(generator()));

This comment has been minimized.

@Avaq

Avaq Jun 21, 2017

I think this task will break when .run() multiple times.

For an overview of related issues, see: fluture-js/Fluture#83

For a quick fix, move the call to generator to inside the computation.

This comment has been minimized.

@framp

framp Jun 21, 2017

Contributor

Thanks for spotting this, fixed!

module.exports = taskDo;
@@ -20,6 +20,7 @@ module.exports = {
task: require('./task'),
waitAny: require('./wait-any'),
waitAll: require('./wait-all'),
do: require('./do'),
_Task: Task,
_TaskExecution: require('./_task-execution'),
@@ -326,6 +326,40 @@ describe('Data.Task', () => {
$ASSERT(result2 == true);
});
it('do()', async () => {
const delay = (ms) => Task.task((r) => {
const timer = setTimeout(() => r.resolve(ms), ms);
r.cleanup(() => clearTimeout(timer));
});
const result = await Task.do(function *() {
const a = yield delay(10);
const b = yield delay(11);
const c = yield Task.of(5);
return Task.of(a + b + c + 4);
}).run().promise();
$ASSERT(result == 30)
const exceptionThrown = [false, false];
await Task.do(function *() {
const a = yield Task.rejected(5);
return Task.of(a);
}).run().promise().catch((exception) => {
exceptionThrown[0] = true;
$ASSERT(exception == 5);
})
$ASSERT(exceptionThrown[0] == true);
const resultCancel = await Task.do(function *() {
const a = yield Task.task(r => r.cancel());
return Task.of(a);
}).run().promise().catch(() => {
exceptionThrown[1] = true;
});
$ASSERT(exceptionThrown[1] == true);
});
describe('#run()', () => {
it('Executes the computation for the task', () => {
let ran = false;
ProTip! Use n and p to navigate between commits in a pull request.