effection
Advanced tools
Comparing version 0.4.0-aa88a97 to 0.4.0-b3114fa
@@ -5,2 +5,4 @@ 'use strict'; | ||
var events = require('events'); | ||
/** | ||
@@ -34,5 +36,11 @@ * Create an execution controller that resumes after the specified | ||
let noop = x => x; | ||
let noop = x => x; // return values of succeed and fail are deliberately ignored. | ||
// see https://github.com/thefrontside/effection.js/pull/44 | ||
promise.then(value => succeed(value), error => fail(error)); // this execution has passed out of scope, so we don't care | ||
promise.then(value => { | ||
succeed(value); | ||
}, error => { | ||
fail(error); | ||
}); // this execution has passed out of scope, so we don't care | ||
// what happened to the promise, so make the callbacks noops. | ||
@@ -91,4 +99,3 @@ // this effectively "unsubscribes" to the promise. | ||
} | ||
class Fork { | ||
class Fork extends events.EventEmitter { | ||
get isUnstarted() { | ||
@@ -154,2 +161,3 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -160,2 +168,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.state = 'unstarted'; | ||
@@ -345,2 +354,7 @@ this.exitPrevious = noop; | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -388,4 +402,110 @@ if (this.parent) { | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { | ||
return; | ||
} | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
function any(type) { | ||
if (type === "array") { | ||
return function anyMatcher(value) { | ||
return Array.isArray(value); | ||
}; | ||
} else if (type) { | ||
return function anyMatcher(value) { | ||
return typeof value === type; | ||
}; | ||
} else { | ||
return function anyMatcher() { | ||
return true; | ||
}; | ||
} | ||
} | ||
function patternMatch(match) { | ||
return function matcher(target) { | ||
if (match === undefined) { | ||
return true; | ||
} else if (Array.isArray(match)) { | ||
return match.every((value, index) => patternMatch(value)(target[index])); | ||
} else if (typeof match === "object") { | ||
return Object.entries(match).every(([key, value]) => patternMatch(value)(target[key])); | ||
} else if (typeof match === "function") { | ||
return match(target); | ||
} else { | ||
return match === target; | ||
} | ||
}; | ||
} | ||
function receive(target, match) { | ||
if (target && !(target instanceof Fork)) { | ||
match = target; | ||
target = undefined; | ||
} | ||
return execution => { | ||
if (!target) { | ||
target = execution; | ||
} | ||
function scanMailbox() { | ||
let index = target.mailbox.findIndex(message => patternMatch(match)(message)); | ||
if (index >= 0) { | ||
let _target$mailbox$splic = target.mailbox.splice(index, 1), | ||
_target$mailbox$splic2 = _slicedToArray(_target$mailbox$splic, 1), | ||
message = _target$mailbox$splic2[0]; | ||
execution.resume(message); | ||
} | ||
} | ||
scanMailbox(); | ||
target.on("message", scanMailbox); | ||
return () => { | ||
target.off("message", scanMailbox); | ||
}; | ||
}; | ||
} | ||
exports.any = any; | ||
exports.fork = fork; | ||
exports.promiseOf = promiseOf; | ||
exports.receive = receive; | ||
exports.timeout = timeout; |
@@ -6,4 +6,4 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import { isGenerator, isGeneratorFunction, toGeneratorFunction } from "./generator-function.js"; | ||
class Fork { | ||
import { EventEmitter } from 'events'; | ||
export class Fork extends EventEmitter { | ||
get isUnstarted() { | ||
@@ -48,2 +48,3 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -54,2 +55,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.state = 'unstarted'; | ||
@@ -239,2 +241,7 @@ this.exitPrevious = noop; | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -241,0 +248,0 @@ if (this.parent) { |
export { timeout } from "./timeout.js"; | ||
export { fork } from "./fork.js"; | ||
export { promiseOf } from "./promise-of.js"; | ||
export { promiseOf } from "./promise-of.js"; | ||
export { receive } from "./receive.js"; | ||
export { any } from "./pattern-match.js"; |
@@ -11,5 +11,11 @@ /** | ||
let noop = x => x; | ||
let noop = x => x; // return values of succeed and fail are deliberately ignored. | ||
// see https://github.com/thefrontside/effection.js/pull/44 | ||
promise.then(value => succeed(value), error => fail(error)); // this execution has passed out of scope, so we don't care | ||
promise.then(value => { | ||
succeed(value); | ||
}, error => { | ||
fail(error); | ||
}); // this execution has passed out of scope, so we don't care | ||
// what happened to the promise, so make the callbacks noops. | ||
@@ -16,0 +22,0 @@ // this effectively "unsubscribes" to the promise. |
@@ -0,1 +1,3 @@ | ||
import { EventEmitter } from 'events'; | ||
/** | ||
@@ -30,5 +32,11 @@ * Create an execution controller that resumes after the specified | ||
let noop = x => x; | ||
let noop = x => x; // return values of succeed and fail are deliberately ignored. | ||
// see https://github.com/thefrontside/effection.js/pull/44 | ||
promise.then(value => succeed(value), error => fail(error)); // this execution has passed out of scope, so we don't care | ||
promise.then(value => { | ||
succeed(value); | ||
}, error => { | ||
fail(error); | ||
}); // this execution has passed out of scope, so we don't care | ||
// what happened to the promise, so make the callbacks noops. | ||
@@ -91,4 +99,3 @@ // this effectively "unsubscribes" to the promise. | ||
} | ||
class Fork { | ||
class Fork extends EventEmitter { | ||
get isUnstarted() { | ||
@@ -133,2 +140,3 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -139,2 +147,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.state = 'unstarted'; | ||
@@ -297,2 +306,7 @@ this.exitPrevious = noop; | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -342,2 +356,112 @@ if (this.parent) { | ||
export { fork, promiseOf, timeout }; | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { | ||
return; | ||
} | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
function any(type) { | ||
if (type === "array") { | ||
return function anyMatcher(value) { | ||
return Array.isArray(value); | ||
}; | ||
} else if (type) { | ||
return function anyMatcher(value) { | ||
return typeof value === type; | ||
}; | ||
} else { | ||
return function anyMatcher() { | ||
return true; | ||
}; | ||
} | ||
} | ||
function patternMatch(match) { | ||
return function matcher(target) { | ||
if (match === undefined) { | ||
return true; | ||
} else if (Array.isArray(match)) { | ||
return match.every((value, index) => patternMatch(value)(target[index])); | ||
} else if (typeof match === "object") { | ||
return Object.entries(match).every((_ref) => { | ||
let _ref2 = _slicedToArray(_ref, 2), | ||
key = _ref2[0], | ||
value = _ref2[1]; | ||
return patternMatch(value)(target[key]); | ||
}); | ||
} else if (typeof match === "function") { | ||
return match(target); | ||
} else { | ||
return match === target; | ||
} | ||
}; | ||
} | ||
function receive(target, match) { | ||
if (target && !(target instanceof Fork)) { | ||
match = target; | ||
target = undefined; | ||
} | ||
return execution => { | ||
if (!target) { | ||
target = execution; | ||
} | ||
function scanMailbox() { | ||
let index = target.mailbox.findIndex(message => patternMatch(match)(message)); | ||
if (index >= 0) { | ||
let _target$mailbox$splic = target.mailbox.splice(index, 1), | ||
_target$mailbox$splic2 = _slicedToArray(_target$mailbox$splic, 1), | ||
message = _target$mailbox$splic2[0]; | ||
execution.resume(message); | ||
} | ||
} | ||
scanMailbox(); | ||
target.on("message", scanMailbox); | ||
return () => { | ||
target.off("message", scanMailbox); | ||
}; | ||
}; | ||
} | ||
export { any, fork, promiseOf, receive, timeout }; |
declare module "effection" { | ||
export type Operation = SequenceFn | Sequence | Promise<any> | Controller | undefined; | ||
type PredicateFn = (value: any) => boolean; | ||
type AnyTypeName = "undefined" | "object" | "boolean" | "number" | "bigint" | "string" | "symbol" | "function" | "array" | ||
type PatternMatchObject = { [key: string]: PatternMatchOptions }; | ||
type Primitive = string | boolean | number | bigint | symbol | null | undefined; | ||
export type PatternMatchOptions = Primitive | PatternMatchObject | PatternMatchOptions[] | PredicateFn | ||
export type Operation = SequenceFn | Sequence | Promise<any> | Controller | Execution<any> | undefined; | ||
export type SequenceFn = (this: Execution) => Sequence; | ||
@@ -13,2 +19,3 @@ export type Controller = (execution: Execution) => void | (() => void); | ||
halt(reason?: any): void; | ||
send(message: any): void; | ||
} | ||
@@ -18,3 +25,11 @@ | ||
export function receive(): Controller; | ||
export function receive(fork: Execution<any>): Controller; | ||
export function receive(match: PatternMatchOptions): Controller; | ||
export function receive(fork: Execution<any>, match: PatternMatchOptions): Controller; | ||
export function any(): PredicateFn; | ||
export function any(type: AnyTypeName): PredicateFn; | ||
export function timeout(durationMillis: number): Operation; | ||
} |
{ | ||
"name": "effection", | ||
"description": "Effortlessly composable structured concurrency primitive for JavaScript", | ||
"version": "0.4.0-aa88a97", | ||
"version": "0.4.0-b3114fa", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "files": [ |
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
45913
15
1194