effection
Advanced tools
Comparing version 0.4.0-1c60ace to 0.4.0-3f2707f
@@ -5,4 +5,2 @@ 'use strict'; | ||
var events = require('events'); | ||
/** | ||
@@ -98,3 +96,4 @@ * Create an execution controller that resumes after the specified | ||
} | ||
class Fork extends events.EventEmitter { | ||
class Fork { | ||
get isUnstarted() { | ||
@@ -160,3 +159,2 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -167,3 +165,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.atExitHooks = []; | ||
this.state = 'unstarted'; | ||
@@ -194,2 +192,6 @@ this.exitPrevious = noop; | ||
atExit(fn) { | ||
this.atExitHooks.push(fn); | ||
} | ||
halt(value) { | ||
@@ -341,2 +343,3 @@ if (this.isRunning) { | ||
this.atExitHooks.forEach(fn => fn(this)); | ||
this.finalizePromise(); | ||
@@ -355,7 +358,2 @@ } | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -403,110 +401,3 @@ 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"; | ||
import { EventEmitter } from 'events'; | ||
export class Fork extends EventEmitter { | ||
class Fork { | ||
get isUnstarted() { | ||
@@ -48,3 +48,2 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -55,3 +54,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.atExitHooks = []; | ||
this.state = 'unstarted'; | ||
@@ -82,2 +81,6 @@ this.exitPrevious = noop; | ||
atExit(fn) { | ||
this.atExitHooks.push(fn); | ||
} | ||
halt(value) { | ||
@@ -229,2 +232,3 @@ if (this.isRunning) { | ||
this.atExitHooks.forEach(fn => fn(this)); | ||
this.finalizePromise(); | ||
@@ -243,7 +247,2 @@ } | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -250,0 +249,0 @@ if (this.parent) { |
export { timeout } from "./timeout.js"; | ||
export { fork } from "./fork.js"; | ||
export { promiseOf } from "./promise-of.js"; | ||
export { receive } from "./receive.js"; | ||
export { any } from "./pattern-match.js"; | ||
export { fork } from "./fork.js"; |
@@ -1,3 +0,1 @@ | ||
import { EventEmitter } from 'events'; | ||
/** | ||
@@ -98,3 +96,4 @@ * Create an execution controller that resumes after the specified | ||
} | ||
class Fork extends EventEmitter { | ||
class Fork { | ||
get isUnstarted() { | ||
@@ -139,3 +138,2 @@ return this.state === 'unstarted'; | ||
constructor(operation, parent, sync) { | ||
super(); | ||
this.id = Fork.ids++; | ||
@@ -146,3 +144,3 @@ this.operation = toGeneratorFunction(operation); | ||
this.children = new Set(); | ||
this.mailbox = []; | ||
this.atExitHooks = []; | ||
this.state = 'unstarted'; | ||
@@ -173,2 +171,6 @@ this.exitPrevious = noop; | ||
atExit(fn) { | ||
this.atExitHooks.push(fn); | ||
} | ||
halt(value) { | ||
@@ -293,2 +295,3 @@ if (this.isRunning) { | ||
this.atExitHooks.forEach(fn => fn(this)); | ||
this.finalizePromise(); | ||
@@ -307,7 +310,2 @@ } | ||
send(message) { | ||
this.mailbox.push(message); | ||
this.emit("message", message); | ||
} | ||
get root() { | ||
@@ -357,112 +355,2 @@ 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((_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 }; | ||
export { fork, timeout }; |
declare module "effection" { | ||
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; | ||
@@ -14,3 +8,3 @@ export type SequenceFn = (this: Execution) => Sequence; | ||
export interface Execution<T = any> { | ||
export interface Execution<T = any> extends PromiseLike<any> { | ||
id: number; | ||
@@ -20,3 +14,4 @@ resume(result: T): void; | ||
halt(reason?: any): void; | ||
send(message: any): void; | ||
catch<R>(fn: (Error) => R): Promise<R>; | ||
finally(fn: () => void): Promise<any>; | ||
} | ||
@@ -26,11 +21,3 @@ | ||
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-1c60ace", | ||
"version": "0.4.0-3f2707f", | ||
"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
38254
13
936