@bigtest/effection
Advanced tools
Comparing version 0.1.0-533fc47 to 0.1.0-64de8f7
export { once } from './events'; | ||
export { Mailbox } from './mailbox'; | ||
export { Mailbox, SubscriptionMessage, subscribe } from './mailbox'; | ||
export { any } from './pattern'; | ||
export { suspend } from './suspend'; | ||
export { ensure } from './ensure'; | ||
export { monitorErrors } from './monitor-errors'; |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.Mailbox = mailbox_1.Mailbox; | ||
exports.subscribe = mailbox_1.subscribe; | ||
var pattern_1 = require("./pattern"); | ||
@@ -14,2 +15,4 @@ exports.any = pattern_1.any; | ||
exports.ensure = ensure_1.ensure; | ||
var monitor_errors_1 = require("./monitor-errors"); | ||
exports.monitorErrors = monitor_errors_1.monitorErrors; | ||
//# sourceMappingURL=index.js.map |
@@ -5,15 +5,15 @@ /// <reference types="node" /> | ||
export { any } from './pattern'; | ||
export declare class Mailbox { | ||
export interface SubscriptionMessage { | ||
event: string; | ||
args: unknown[]; | ||
} | ||
export declare class Mailbox<T = any> { | ||
private subscriptions; | ||
private messages; | ||
static subscribe(emitter: EventEmitter | EventTarget, events: string | string[], prepare?: (event: { | ||
event: string; | ||
args: unknown[]; | ||
}) => unknown): Operation; | ||
send(message: unknown): void; | ||
receive(pattern?: unknown): Operation; | ||
subscribe(emitter: EventEmitter | EventTarget, events: string | string[], prepare?: (event: { | ||
event: string; | ||
args: unknown[]; | ||
}) => unknown): Operation; | ||
static subscribe(emitter: EventEmitter | EventTarget, events: string | string[]): Operation<Mailbox<SubscriptionMessage>>; | ||
send(message: T): void; | ||
receive(pattern?: unknown): Operation<T>; | ||
pipe(other: Mailbox<T>): Generator<Operation<any>, void, unknown>; | ||
map<R>(fn: (from: T) => R): Operation<Mailbox<R>>; | ||
} | ||
export declare function subscribe(mailbox: Mailbox<SubscriptionMessage>, emitter: EventEmitter | EventTarget, events: string | string[]): Operation; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const effection_1 = require("effection"); | ||
const events_1 = require("events"); | ||
@@ -10,2 +11,3 @@ const pattern_1 = require("./pattern"); | ||
function isEventTarget(target) { return typeof target.addEventListener === 'function'; } | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
class Mailbox { | ||
@@ -16,5 +18,5 @@ constructor() { | ||
} | ||
static *subscribe(emitter, events, prepare = x => x) { | ||
static *subscribe(emitter, events) { | ||
let mailbox = new Mailbox(); | ||
yield suspend_1.suspend(mailbox.subscribe(emitter, events, prepare)); | ||
yield suspend_1.suspend(effection_1.monitor(subscribe(mailbox, emitter, events))); | ||
return mailbox; | ||
@@ -46,19 +48,40 @@ } | ||
} | ||
*subscribe(emitter, events, prepare = x => x) { | ||
for (let name of [].concat(events)) { | ||
let listener = (...args) => { | ||
this.send(prepare({ event: name, args })); | ||
}; | ||
if (isEventTarget(emitter)) { | ||
emitter.addEventListener(name, listener); | ||
yield suspend_1.suspend(ensure_1.ensure(() => emitter.removeEventListener(name, listener))); | ||
*pipe(other) { | ||
let that = this; // eslint-disable-line @typescript-eslint/no-this-alias | ||
yield suspend_1.suspend(effection_1.monitor(function* () { | ||
while (true) { | ||
let message = yield that.receive(); | ||
other.send(message); | ||
} | ||
else { | ||
emitter.on(name, listener); | ||
yield suspend_1.suspend(ensure_1.ensure(() => emitter.off(name, listener))); | ||
})); | ||
} | ||
*map(fn) { | ||
let that = this; // eslint-disable-line @typescript-eslint/no-this-alias | ||
let other = new Mailbox(); | ||
yield suspend_1.suspend(effection_1.monitor(function* () { | ||
while (true) { | ||
let message = yield that.receive(); | ||
other.send(fn(message)); | ||
} | ||
})); | ||
return other; | ||
} | ||
} | ||
exports.Mailbox = Mailbox; | ||
function* subscribe(mailbox, emitter, events) { | ||
for (let name of [].concat(events)) { | ||
let listener = (...args) => { | ||
mailbox.send({ event: name, args }); | ||
}; | ||
if (isEventTarget(emitter)) { | ||
emitter.addEventListener(name, listener); | ||
yield suspend_1.suspend(ensure_1.ensure(() => emitter.removeEventListener(name, listener))); | ||
} | ||
else { | ||
emitter.on(name, listener); | ||
yield suspend_1.suspend(ensure_1.ensure(() => emitter.off(name, listener))); | ||
} | ||
} | ||
} | ||
exports.Mailbox = Mailbox; | ||
exports.subscribe = subscribe; | ||
//# sourceMappingURL=mailbox.js.map |
{ | ||
"name": "@bigtest/effection", | ||
"version": "0.1.0-533fc47", | ||
"version": "0.1.0-64de8f7", | ||
"description": "Collection of handly utilities for working with effection. May find a different home later", | ||
@@ -28,3 +28,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"effection": "^0.5.2" | ||
"@types/node": "^12.7.11", | ||
"effection": "0.5.2" | ||
}, | ||
@@ -31,0 +32,0 @@ "volta": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15152
23
227
2
+ Added@types/node@^12.7.11
+ Added@types/node@12.20.55(transitive)
Updatedeffection@0.5.2