effection
Advanced tools
Comparing version 0.2.0-698df47 to 0.2.0-9fbcd6e
@@ -19,2 +19,3 @@ 'use strict'; | ||
// what happened to the promise, so make the callbacks noops. | ||
// this effectively "unsubscribes" to the promise. | ||
@@ -30,2 +31,5 @@ return () => succeed = fail = noop; | ||
} | ||
function isGenerator(value) { | ||
return value != null && typeof value.next === 'function' && typeof value.throw === 'function' && typeof value.return === 'function'; | ||
} | ||
@@ -46,2 +50,4 @@ function fromConstant(value) { | ||
return value; | ||
} else if (isGenerator(value)) { | ||
return () => value; | ||
} else if (typeof value === 'function') { | ||
@@ -128,4 +134,4 @@ return fromFunction(value); | ||
class Execution { | ||
static of(proc) { | ||
return new Execution(proc, x => x); | ||
static of(operation) { | ||
return new Execution(operation, x => x); | ||
} | ||
@@ -169,4 +175,4 @@ | ||
constructor(proc) { | ||
this.proc = toGeneratorFunction(proc); | ||
constructor(operation) { | ||
this.operation = toGeneratorFunction(operation); | ||
this.status = new Unstarted(this); | ||
@@ -177,4 +183,4 @@ this.children = []; | ||
start(args) { | ||
return this.status.start(args); | ||
start() { | ||
return this.status.start(); | ||
} | ||
@@ -194,6 +200,2 @@ | ||
fork(proc, args) { | ||
return this.status.fork(proc, args); | ||
} | ||
then(...args) { | ||
@@ -237,6 +239,2 @@ this.continuation = this.continuation.then(...args); | ||
fork() { | ||
this.cannot('fork'); | ||
} | ||
cannot(operationName) { | ||
@@ -262,6 +260,6 @@ let name = this.constructor.name; | ||
class Unstarted extends Status { | ||
start(args) { | ||
let proc = this.execution.proc; | ||
start() { | ||
let operation = this.execution.operation; | ||
let execution = this.execution; | ||
let iterator = proc.apply(execution, args); | ||
let iterator = operation.apply(execution); | ||
execution.status = new Running(execution, iterator); | ||
@@ -273,2 +271,15 @@ execution.resume(); | ||
let currentExecution; | ||
function withCurrentExecution(execution, fn) { | ||
let previousExecution = currentExecution; | ||
try { | ||
currentExecution = execution; | ||
return fn(); | ||
} finally { | ||
currentExecution = previousExecution; | ||
} | ||
} | ||
class Running extends Status { | ||
@@ -292,4 +303,6 @@ constructor(execution, iterator, current = { | ||
try { | ||
this.releaseControl(); | ||
let next = thunk(iterator); | ||
let next = withCurrentExecution(execution, () => { | ||
this.releaseControl(); | ||
return thunk(iterator); | ||
}); | ||
@@ -340,14 +353,2 @@ if (next.done) { | ||
fork(task, args) { | ||
let parent = this.execution; | ||
let child = new Execution(task).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
this.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(args); | ||
return child; | ||
} | ||
} | ||
@@ -401,4 +402,4 @@ | ||
function controllerFor(value) { | ||
if (isGeneratorFunction(value)) { | ||
return call(value); | ||
if (isGeneratorFunction(value) || isGenerator(value)) { | ||
return invoke(value); | ||
} else if (typeof value === 'function') { | ||
@@ -415,5 +416,17 @@ return value; | ||
function call(task, ...args) { | ||
function fork(operation) { | ||
let parent = currentExecution; | ||
let child = new Execution(operation).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
parent.status.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(); | ||
return child; | ||
} | ||
function invoke(operation) { | ||
return parent => { | ||
let child = new Execution(task).then(child => { | ||
let child = new Execution(operation).then(child => { | ||
if (child.isCompleted) { | ||
@@ -432,5 +445,6 @@ return parent.resume(child.result); | ||
parent.children.push(child); | ||
child.start(args); | ||
child.start(); | ||
}; | ||
} | ||
const ExecutionFinalized = Continuation.of(execution => { | ||
@@ -446,5 +460,5 @@ if (execution.isErrored) { | ||
function execute(task, ...args) { | ||
let execution = Execution.of(task); | ||
execution.start(args); | ||
function execute(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
@@ -469,5 +483,5 @@ } | ||
exports.call = call; | ||
exports.execute = execute; | ||
exports.fork = fork; | ||
exports.promiseOf = promiseOf; | ||
exports.timeout = timeout; |
import Execution from "./execution.js"; | ||
export function execute(task, ...args) { | ||
let execution = Execution.of(task); | ||
execution.start(args); | ||
export function execute(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
} |
import { promiseOf } from "./promise-of.js"; | ||
import { isGeneratorFunction, toGeneratorFunction } from "./generator-function.js"; | ||
import { isGeneratorFunction, isGenerator, toGeneratorFunction } from "./generator-function.js"; | ||
import Continuation from "./continuation.js"; | ||
export default class Execution { | ||
static of(proc) { | ||
return new Execution(proc, x => x); | ||
static of(operation) { | ||
return new Execution(operation, x => x); | ||
} | ||
@@ -45,4 +45,4 @@ | ||
constructor(proc) { | ||
this.proc = toGeneratorFunction(proc); | ||
constructor(operation) { | ||
this.operation = toGeneratorFunction(operation); | ||
this.status = new Unstarted(this); | ||
@@ -53,4 +53,4 @@ this.children = []; | ||
start(args) { | ||
return this.status.start(args); | ||
start() { | ||
return this.status.start(); | ||
} | ||
@@ -70,6 +70,2 @@ | ||
fork(proc, args) { | ||
return this.status.fork(proc, args); | ||
} | ||
then(...args) { | ||
@@ -113,6 +109,2 @@ this.continuation = this.continuation.then(...args); | ||
fork() { | ||
this.cannot('fork'); | ||
} | ||
cannot(operationName) { | ||
@@ -140,5 +132,5 @@ let name = this.constructor.name; | ||
class Unstarted extends Status { | ||
start(args) { | ||
start() { | ||
let { | ||
proc | ||
operation | ||
} = this.execution; | ||
@@ -148,3 +140,3 @@ let { | ||
} = this; | ||
let iterator = proc.apply(execution, args); | ||
let iterator = operation.apply(execution); | ||
execution.status = new Running(execution, iterator); | ||
@@ -156,2 +148,15 @@ execution.resume(); | ||
let currentExecution; | ||
function withCurrentExecution(execution, fn) { | ||
let previousExecution = currentExecution; | ||
try { | ||
currentExecution = execution; | ||
return fn(); | ||
} finally { | ||
currentExecution = previousExecution; | ||
} | ||
} | ||
class Running extends Status { | ||
@@ -177,4 +182,6 @@ constructor(execution, iterator, current = { | ||
try { | ||
this.releaseControl(); | ||
let next = thunk(iterator); | ||
let next = withCurrentExecution(execution, () => { | ||
this.releaseControl(); | ||
return thunk(iterator); | ||
}); | ||
@@ -227,14 +234,2 @@ if (next.done) { | ||
fork(task, args) { | ||
let parent = this.execution; | ||
let child = new Execution(task).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
this.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(args); | ||
return child; | ||
} | ||
} | ||
@@ -292,4 +287,4 @@ | ||
function controllerFor(value) { | ||
if (isGeneratorFunction(value)) { | ||
return call(value); | ||
if (isGeneratorFunction(value) || isGenerator(value)) { | ||
return invoke(value); | ||
} else if (typeof value === 'function') { | ||
@@ -306,5 +301,17 @@ return value; | ||
export function call(task, ...args) { | ||
export function fork(operation) { | ||
let parent = currentExecution; | ||
let child = new Execution(operation).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
parent.status.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(); | ||
return child; | ||
} | ||
function invoke(operation) { | ||
return parent => { | ||
let child = new Execution(task).then(child => { | ||
let child = new Execution(operation).then(child => { | ||
if (child.isCompleted) { | ||
@@ -323,5 +330,6 @@ return parent.resume(child.result); | ||
parent.children.push(child); | ||
child.start(args); | ||
child.start(); | ||
}; | ||
} | ||
const ExecutionFinalized = Continuation.of(execution => { | ||
@@ -328,0 +336,0 @@ if (execution.isErrored) { |
@@ -6,2 +6,5 @@ const GeneratorFunction = function* () {}.constructor; | ||
} | ||
export function isGenerator(value) { | ||
return value != null && typeof value.next === 'function' && typeof value.throw === 'function' && typeof value.return === 'function'; | ||
} | ||
@@ -22,2 +25,4 @@ function fromConstant(value) { | ||
return value; | ||
} else if (isGenerator(value)) { | ||
return () => value; | ||
} else if (typeof value === 'function') { | ||
@@ -24,0 +29,0 @@ return fromFunction(value); |
export { execute } from "./execute.js"; | ||
export { timeout } from "./timeout.js"; | ||
export { call } from "./execution.js"; | ||
export { fork } from "./execution.js"; | ||
export { promiseOf } from "./promise-of.js"; |
@@ -15,2 +15,3 @@ /** | ||
// what happened to the promise, so make the callbacks noops. | ||
// this effectively "unsubscribes" to the promise. | ||
@@ -17,0 +18,0 @@ return () => succeed = fail = noop; |
@@ -15,2 +15,3 @@ /** | ||
// what happened to the promise, so make the callbacks noops. | ||
// this effectively "unsubscribes" to the promise. | ||
@@ -26,2 +27,5 @@ return () => succeed = fail = noop; | ||
} | ||
function isGenerator(value) { | ||
return value != null && typeof value.next === 'function' && typeof value.throw === 'function' && typeof value.return === 'function'; | ||
} | ||
@@ -46,2 +50,4 @@ function fromConstant(value) { | ||
return value; | ||
} else if (isGenerator(value)) { | ||
return () => value; | ||
} else if (typeof value === 'function') { | ||
@@ -128,4 +134,4 @@ return fromFunction(value); | ||
class Execution { | ||
static of(proc) { | ||
return new Execution(proc, x => x); | ||
static of(operation) { | ||
return new Execution(operation, x => x); | ||
} | ||
@@ -169,4 +175,4 @@ | ||
constructor(proc) { | ||
this.proc = toGeneratorFunction(proc); | ||
constructor(operation) { | ||
this.operation = toGeneratorFunction(operation); | ||
this.status = new Unstarted(this); | ||
@@ -177,4 +183,4 @@ this.children = []; | ||
start(args) { | ||
return this.status.start(args); | ||
start() { | ||
return this.status.start(); | ||
} | ||
@@ -194,6 +200,2 @@ | ||
fork(proc, args) { | ||
return this.status.fork(proc, args); | ||
} | ||
then() { | ||
@@ -237,6 +239,2 @@ this.continuation = this.continuation.then(...arguments); | ||
fork() { | ||
this.cannot('fork'); | ||
} | ||
cannot(operationName) { | ||
@@ -262,6 +260,6 @@ let name = this.constructor.name; | ||
class Unstarted extends Status { | ||
start(args) { | ||
let proc = this.execution.proc; | ||
start() { | ||
let operation = this.execution.operation; | ||
let execution = this.execution; | ||
let iterator = proc.apply(execution, args); | ||
let iterator = operation.apply(execution); | ||
execution.status = new Running(execution, iterator); | ||
@@ -273,2 +271,15 @@ execution.resume(); | ||
let currentExecution; | ||
function withCurrentExecution(execution, fn) { | ||
let previousExecution = currentExecution; | ||
try { | ||
currentExecution = execution; | ||
return fn(); | ||
} finally { | ||
currentExecution = previousExecution; | ||
} | ||
} | ||
class Running extends Status { | ||
@@ -293,4 +304,6 @@ constructor(execution, iterator) { | ||
try { | ||
this.releaseControl(); | ||
let next = thunk(iterator); | ||
let next = withCurrentExecution(execution, () => { | ||
this.releaseControl(); | ||
return thunk(iterator); | ||
}); | ||
@@ -341,14 +354,2 @@ if (next.done) { | ||
fork(task, args) { | ||
let parent = this.execution; | ||
let child = new Execution(task).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
this.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(args); | ||
return child; | ||
} | ||
} | ||
@@ -402,4 +403,4 @@ | ||
function controllerFor(value) { | ||
if (isGeneratorFunction(value)) { | ||
return call(value); | ||
if (isGeneratorFunction(value) || isGenerator(value)) { | ||
return invoke(value); | ||
} else if (typeof value === 'function') { | ||
@@ -416,9 +417,17 @@ return value; | ||
function call(task) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
function fork(operation) { | ||
let parent = currentExecution; | ||
let child = new Execution(operation).then(() => { | ||
if (parent.isWaiting && !parent.hasBlockingChildren) { | ||
parent.status.finalize(new Completed(parent, parent.result)); | ||
} | ||
}).catch(e => parent.throw(e)); | ||
parent.children.push(child); | ||
child.start(); | ||
return child; | ||
} | ||
function invoke(operation) { | ||
return parent => { | ||
let child = new Execution(task).then(child => { | ||
let child = new Execution(operation).then(child => { | ||
if (child.isCompleted) { | ||
@@ -437,5 +446,6 @@ return parent.resume(child.result); | ||
parent.children.push(child); | ||
child.start(args); | ||
child.start(); | ||
}; | ||
} | ||
const ExecutionFinalized = Continuation.of(execution => { | ||
@@ -451,10 +461,5 @@ if (execution.isErrored) { | ||
function execute(task) { | ||
let execution = Execution.of(task); | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
execution.start(args); | ||
function execute(operation) { | ||
let execution = Execution.of(operation); | ||
execution.start(); | ||
return execution; | ||
@@ -480,2 +485,2 @@ } | ||
export { call, execute, promiseOf, timeout }; | ||
export { execute, fork, promiseOf, timeout }; |
declare module "effection" { | ||
export type Operation = SequenceFn | Promise<any> | Controller | undefined; | ||
export type SequenceFn = (this: Execution, ...args: any[]) => Sequence; | ||
export type Sequence = Generator<Operation, any, any>; | ||
export type Operation = SequenceFn | Sequence | Promise<any> | Controller | undefined; | ||
export type SequenceFn = (this: Execution) => Sequence; | ||
export type Controller = (execution: Execution) => void | (() => void); | ||
export interface Sequence extends Generator<Operation, any, any> {} | ||
export interface Execution<T = any> { | ||
@@ -13,5 +14,5 @@ resume(result: T): void; | ||
export function execute<T>(operation: Operation): Execution<T>; | ||
export function call(operation: Operation, ...args: any[]): Operation; | ||
export function fork<T>(operation: Operation): Execution<T>; | ||
export function timeout(durationMillis: number): Operation; | ||
} |
{ | ||
"name": "effection", | ||
"description": "Effortlessly composable structured concurrency primitive for JavaScript", | ||
"version": "0.2.0-698df47", | ||
"version": "0.2.0-9fbcd6e", | ||
"license": "MIT", | ||
@@ -35,4 +35,2 @@ "files": [ | ||
"nyc": "~14.1.1", | ||
"ts-expect": "^1.1.0", | ||
"ts-node": "^8.1.0", | ||
"typescript": "^3.6.4" | ||
@@ -39,0 +37,0 @@ }, |
@@ -155,7 +155,7 @@ [![npm](https://img.shields.io/npm/v/effection.svg)](https://www.npmjs.com/package/effection) | ||
``` javascript | ||
import { execute } from 'effection'; | ||
import { execute, fork } from 'effection'; | ||
execute(function*() { | ||
this.fork(createFileServer); | ||
this.fork(createHttpServer); | ||
fork(createFileServer); | ||
fork(createHttpServer); | ||
}); | ||
@@ -162,0 +162,0 @@ ``` |
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
40556
17
1218