effection
Advanced tools
Comparing version 0.2.0-9fbcd6e to 0.2.0-bed72bd
@@ -6,2 +6,18 @@ 'use strict'; | ||
/** | ||
* Create an execution controller that resumes after the specified | ||
* duration. E.g. | ||
* | ||
* function* waitAndSayHello(target) { | ||
* yield timeout(100); | ||
* console.log(`Hello ${target}!`); | ||
* } | ||
*/ | ||
function timeout(durationMillis = 0) { | ||
return function (execution) { | ||
let timeoutId = setTimeout(() => execution.resume(), durationMillis); | ||
return () => clearTimeout(timeoutId); | ||
}; | ||
} | ||
/** | ||
* An execution controller that resumes or throws based | ||
@@ -136,2 +152,8 @@ * on a promise. | ||
static start(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} | ||
get isUnstarted() { | ||
@@ -406,2 +428,6 @@ return this.status instanceof Unstarted; | ||
function fork(operation) { | ||
if (currentExecution == null) { | ||
return Execution.start(operation); | ||
} | ||
let parent = currentExecution; | ||
@@ -448,27 +474,4 @@ let child = new Execution(operation).then(() => { | ||
function execute(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} | ||
/** | ||
* Create an execution controller that resumes after the specified | ||
* duration. E.g. | ||
* | ||
* function* waitAndSayHello(target) { | ||
* yield timeout(100); | ||
* console.log(`Hello ${target}!`); | ||
* } | ||
*/ | ||
function timeout(durationMillis = 0) { | ||
return function (execution) { | ||
let timeoutId = setTimeout(() => execution.resume(), durationMillis); | ||
return () => clearTimeout(timeoutId); | ||
}; | ||
} | ||
exports.execute = execute; | ||
exports.fork = fork; | ||
exports.promiseOf = promiseOf; | ||
exports.timeout = timeout; |
@@ -9,2 +9,8 @@ import { promiseOf } from "./promise-of.js"; | ||
static start(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} | ||
get isUnstarted() { | ||
@@ -293,2 +299,6 @@ return this.status instanceof Unstarted; | ||
export function fork(operation) { | ||
if (currentExecution == null) { | ||
return Execution.start(operation); | ||
} | ||
let parent = currentExecution; | ||
@@ -295,0 +305,0 @@ let child = new Execution(operation).then(() => { |
@@ -1,4 +0,3 @@ | ||
export { execute } from "./execute.js"; | ||
export { timeout } from "./timeout.js"; | ||
export { fork } from "./execution.js"; | ||
export { promiseOf } from "./promise-of.js"; |
/** | ||
* Create an execution controller that resumes after the specified | ||
* duration. E.g. | ||
* | ||
* function* waitAndSayHello(target) { | ||
* yield timeout(100); | ||
* console.log(`Hello ${target}!`); | ||
* } | ||
*/ | ||
function timeout() { | ||
let durationMillis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
return function (execution) { | ||
let timeoutId = setTimeout(() => execution.resume(), durationMillis); | ||
return () => clearTimeout(timeoutId); | ||
}; | ||
} | ||
/** | ||
* An execution controller that resumes or throws based | ||
@@ -135,2 +152,8 @@ * on a promise. | ||
static start(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} | ||
get isUnstarted() { | ||
@@ -406,2 +429,6 @@ return this.status instanceof Unstarted; | ||
function fork(operation) { | ||
if (currentExecution == null) { | ||
return Execution.start(operation); | ||
} | ||
let parent = currentExecution; | ||
@@ -448,25 +475,2 @@ let child = new Execution(operation).then(() => { | ||
function execute(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} | ||
/** | ||
* Create an execution controller that resumes after the specified | ||
* duration. E.g. | ||
* | ||
* function* waitAndSayHello(target) { | ||
* yield timeout(100); | ||
* console.log(`Hello ${target}!`); | ||
* } | ||
*/ | ||
function timeout() { | ||
let durationMillis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
return function (execution) { | ||
let timeoutId = setTimeout(() => execution.resume(), durationMillis); | ||
return () => clearTimeout(timeoutId); | ||
}; | ||
} | ||
export { execute, fork, promiseOf, timeout }; | ||
export { fork, promiseOf, timeout }; |
@@ -13,3 +13,2 @@ declare module "effection" { | ||
export function execute<T>(operation: Operation): Execution<T>; | ||
export function fork<T>(operation: Operation): Execution<T>; | ||
@@ -16,0 +15,0 @@ |
{ | ||
"name": "effection", | ||
"description": "Effortlessly composable structured concurrency primitive for JavaScript", | ||
"version": "0.2.0-9fbcd6e", | ||
"version": "0.2.0-bed72bd", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -68,3 +68,3 @@ [![npm](https://img.shields.io/npm/v/effection.svg)](https://www.npmjs.com/package/effection) | ||
The process primitive is the `Execution`. To create (and start) an | ||
`Execution`, use the `execute` function and pass it a generator. This | ||
`Execution`, use the `fork` function and pass it a generator. This | ||
simplest example waits for 1 second, then prints out "hello world" to | ||
@@ -74,5 +74,5 @@ the console. | ||
``` javascript | ||
import { execute, timeout } from 'effection'; | ||
import { fork, timeout } from 'effection'; | ||
let process = execute(function*() { | ||
let process = fork(function*() { | ||
yield timeout(1000); | ||
@@ -92,3 +92,3 @@ return 'hello world'; | ||
``` javascript | ||
execute(function*() { | ||
fork(function*() { | ||
yield function*() { | ||
@@ -106,3 +106,3 @@ for (let i = 0; i < 10; i++) { | ||
``` javascript | ||
let process = execute(function*() { | ||
let process = fork(function*() { | ||
return yield function*() { | ||
@@ -126,3 +126,3 @@ return yield function*() { | ||
import { execute, timeout, call } from 'effection'; | ||
import { fork, timeout, call } from 'effection'; | ||
@@ -133,3 +133,3 @@ function* waitForSeconds(durationSeconds) { | ||
execute(function*() { | ||
fork(function*() { | ||
yield call(waitforseconds, 10); | ||
@@ -153,3 +153,3 @@ }); | ||
Sometimes you want to execute some processes in parallel and not | ||
Sometimes you want to fork some processes in parallel and not | ||
necessarily block further execution on them. You still want the | ||
@@ -162,5 +162,5 @@ guarantees associated with structured concurrency however. For | ||
``` javascript | ||
import { execute, fork } from 'effection'; | ||
import { fork, fork } from 'effection'; | ||
execute(function*() { | ||
fork(function*() { | ||
fork(createFileServer); | ||
@@ -167,0 +167,0 @@ fork(createHttpServer); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40591
1224
14