@effection/subscription
Advanced tools
Comparing version 0.10.0 to 0.11.0
# @effection/subscription | ||
## 0.11.0 | ||
### Minor Changes | ||
- 25b68eb: Subscriptions created via `createSubscription` are chainable on both sides of the yield | ||
## 0.10.0 | ||
@@ -4,0 +10,0 @@ |
@@ -1,2 +0,3 @@ | ||
export { Subscription, createSubscription } from './subscription'; | ||
export { Subscription } from './subscription'; | ||
export { createSubscription } from './create-subscription'; | ||
export { SymbolSubscribable } from './symbol-subscribable'; | ||
@@ -3,0 +4,0 @@ export { Subscribable } from './subscribable'; |
@@ -163,2 +163,36 @@ 'use strict'; | ||
function makeChainable(operation) { | ||
var _Object$assign; | ||
return Object.assign(operation, (_Object$assign = {}, _Object$assign[SymbolSubscribable] = function* () { | ||
return yield operation; | ||
}, _Object$assign.filter = function filter(predicate) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.filter(predicate); | ||
}); | ||
}); | ||
}, _Object$assign.match = function match(reference) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.match(reference); | ||
}); | ||
}); | ||
}, _Object$assign.map = function map(mapper) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.map(mapper); | ||
}); | ||
}); | ||
}, _Object$assign.first = function* first() { | ||
return yield (yield operation).first(); | ||
}, _Object$assign.expect = function* expect() { | ||
return yield (yield operation).expect(); | ||
}, _Object$assign.forEach = function* forEach(visit) { | ||
return yield (yield operation).forEach(visit); | ||
}, _Object$assign.next = function* next() { | ||
return yield (yield operation).next(); | ||
}, _Object$assign)); | ||
} | ||
var Semaphore = function Semaphore() { | ||
@@ -185,3 +219,3 @@ var _this = this; | ||
function createSubscription(subscribe) { | ||
return function* () { | ||
return makeChainable(function* () { | ||
var results = []; | ||
@@ -233,3 +267,3 @@ var semaphore = new Semaphore(); | ||
return subscription; | ||
}; | ||
}); | ||
} | ||
@@ -243,36 +277,2 @@ | ||
function makeChainable(operation) { | ||
var _Object$assign; | ||
return Object.assign(operation, (_Object$assign = {}, _Object$assign[SymbolSubscribable] = function* () { | ||
return yield operation; | ||
}, _Object$assign.filter = function filter(predicate) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.filter(predicate); | ||
}); | ||
}); | ||
}, _Object$assign.match = function match(reference) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.match(reference); | ||
}); | ||
}); | ||
}, _Object$assign.map = function map(mapper) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.map(mapper); | ||
}); | ||
}); | ||
}, _Object$assign.first = function* first() { | ||
return yield (yield operation).first(); | ||
}, _Object$assign.expect = function* expect() { | ||
return yield (yield operation).expect(); | ||
}, _Object$assign.forEach = function* forEach(visit) { | ||
return yield (yield operation).forEach(visit); | ||
}, _Object$assign.next = function* next() { | ||
return yield (yield operation).next(); | ||
}, _Object$assign)); | ||
} | ||
function subscribe(source) { | ||
@@ -279,0 +279,0 @@ return makeChainable(function* () { |
@@ -1,2 +0,2 @@ | ||
"use strict";var n=require("effection"),r=Symbol.for("Symbol.subscription"),e={next:function(){throw new Error("dummy")}},t=function(){function t(n){this.subscription=n}t.of=function*(i){var u=new t(e);return yield n.resource(u,(function*(){u.subscription=yield function(n){return n[r]?n[r]():n}(i),yield}))},t.wrap=function*(r,i){var u=new t(e);return yield n.resource(u,(function*(){var n=yield r;u.subscription=i(n),yield}))};var i=t.prototype;return i.filter=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var e=yield r.next();if(e.done)return e;if(n(e.value))return e}}})},i.match=function(n){return this.filter(function n(r){return function(e){if("object"==typeof e&&"object"==typeof r){var t=e;return Object.entries(r).every((function(r){var e=r[0];return n(r[1])(t[e])}))}return e===r}}(n))},i.map=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var e=yield r.next();return e.done?e:{done:!1,value:n(e.value)}}}})},i.first=function*(){var n=yield this.subscription.next();return n.done?void 0:n.value},i.expect=function*(){var n=yield this.subscription.next();if(n.done)throw new Error("expected subscription to contain a value");return n.value},i.forEach=function*(n){for(;;){var r=yield this.subscription.next();if(r.done)return r.value;yield n(r.value)}},i.next=function(){return this.subscription.next()},t}(),i=function(){var n=this;this.waiters=[],this.signal=function(r){var e=n.waiters.pop();e&&e(r)},this.wait=function(){return new Promise((function(r){return n.waiters.push(r)}))}};function u(n){return function n(e){var i;return Object.assign(e,((i={})[r]=function*(){return yield e},i.filter=function(r){return n((function*(){return yield t.wrap(e,(function(n){return n.filter(r)}))}))},i.match=function(r){return n((function*(){return yield t.wrap(e,(function(n){return n.match(r)}))}))},i.map=function(r){return n((function*(){return yield t.wrap(e,(function(n){return n.map(r)}))}))},i.first=function*(){return yield(yield e).first()},i.expect=function*(){return yield(yield e).expect()},i.forEach=function*(n){return yield(yield e).forEach(n)},i.next=function*(){return yield(yield e).next()},i))}((function*(){return yield t.of(n)}))}var o={from:function(n){return console.warn("`Subscribable.from(source)` is deprecated, use `subscribe(source).map(...)` instead"),u(n)}};exports.ChainableSubscription=t,exports.Subscribable=o,exports.SymbolSubscribable=r,exports.createSubscription=function(r){return function*(){var e=[],u=new i,o=function(n){e.push({done:!1,value:n}),u.signal()};return yield n.resource(new t({next:function(){try{var n=u.wait();return e.length>0&&u.signal(),Promise.resolve(n.then((function(){return e.shift()})))}catch(n){return Promise.reject(n)}}}),(function*(){try{var n=yield r((function(n){return o(n)}));e.push({done:!0,value:n}),u.signal()}finally{o=function(n){throw function(n){var r=new Error("tried to publish a value: "+n+" on an already finished subscription");return r.name="TypeError",r}(n)}}}))}},exports.forEach=function*(n,r){return console.warn("`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead"),yield u(n).forEach(r)},exports.subscribe=u; | ||
"use strict";var n=require("effection"),r=Symbol.for("Symbol.subscription"),e={next:function(){throw new Error("dummy")}},t=function(){function t(n){this.subscription=n}t.of=function*(i){var u=new t(e);return yield n.resource(u,(function*(){u.subscription=yield function(n){return n[r]?n[r]():n}(i),yield}))},t.wrap=function*(r,i){var u=new t(e);return yield n.resource(u,(function*(){var n=yield r;u.subscription=i(n),yield}))};var i=t.prototype;return i.filter=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var e=yield r.next();if(e.done)return e;if(n(e.value))return e}}})},i.match=function(n){return this.filter(function n(r){return function(e){if("object"==typeof e&&"object"==typeof r){var t=e;return Object.entries(r).every((function(r){var e=r[0];return n(r[1])(t[e])}))}return e===r}}(n))},i.map=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var e=yield r.next();return e.done?e:{done:!1,value:n(e.value)}}}})},i.first=function*(){var n=yield this.subscription.next();return n.done?void 0:n.value},i.expect=function*(){var n=yield this.subscription.next();if(n.done)throw new Error("expected subscription to contain a value");return n.value},i.forEach=function*(n){for(;;){var r=yield this.subscription.next();if(r.done)return r.value;yield n(r.value)}},i.next=function(){return this.subscription.next()},t}();function i(n){var e;return Object.assign(n,((e={})[r]=function*(){return yield n},e.filter=function(r){return i((function*(){return yield t.wrap(n,(function(n){return n.filter(r)}))}))},e.match=function(r){return i((function*(){return yield t.wrap(n,(function(n){return n.match(r)}))}))},e.map=function(r){return i((function*(){return yield t.wrap(n,(function(n){return n.map(r)}))}))},e.first=function*(){return yield(yield n).first()},e.expect=function*(){return yield(yield n).expect()},e.forEach=function*(r){return yield(yield n).forEach(r)},e.next=function*(){return yield(yield n).next()},e))}var u=function(){var n=this;this.waiters=[],this.signal=function(r){var e=n.waiters.pop();e&&e(r)},this.wait=function(){return new Promise((function(r){return n.waiters.push(r)}))}};function o(n){return i((function*(){return yield t.of(n)}))}var c={from:function(n){return console.warn("`Subscribable.from(source)` is deprecated, use `subscribe(source).map(...)` instead"),o(n)}};exports.ChainableSubscription=t,exports.Subscribable=c,exports.SymbolSubscribable=r,exports.createSubscription=function(r){return i((function*(){var e=[],i=new u,o=function(n){e.push({done:!1,value:n}),i.signal()};return yield n.resource(new t({next:function(){try{var n=i.wait();return e.length>0&&i.signal(),Promise.resolve(n.then((function(){return e.shift()})))}catch(n){return Promise.reject(n)}}}),(function*(){try{var n=yield r((function(n){return o(n)}));e.push({done:!0,value:n}),i.signal()}finally{o=function(n){throw function(n){var r=new Error("tried to publish a value: "+n+" on an already finished subscription");return r.name="TypeError",r}(n)}}}))}))},exports.forEach=function*(n,r){return console.warn("`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead"),yield o(n).forEach(r)},exports.subscribe=o; | ||
//# sourceMappingURL=subscription.cjs.production.min.js.map |
@@ -6,2 +6,1 @@ import { Operation } from 'effection'; | ||
} | ||
export declare function createSubscription<T, TReturn>(subscribe: Subscriber<T, TReturn>): Operation<Subscription<T, TReturn>>; |
@@ -161,2 +161,36 @@ import { resource } from 'effection'; | ||
function makeChainable(operation) { | ||
var _Object$assign; | ||
return Object.assign(operation, (_Object$assign = {}, _Object$assign[SymbolSubscribable] = function* () { | ||
return yield operation; | ||
}, _Object$assign.filter = function filter(predicate) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.filter(predicate); | ||
}); | ||
}); | ||
}, _Object$assign.match = function match(reference) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.match(reference); | ||
}); | ||
}); | ||
}, _Object$assign.map = function map(mapper) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.map(mapper); | ||
}); | ||
}); | ||
}, _Object$assign.first = function* first() { | ||
return yield (yield operation).first(); | ||
}, _Object$assign.expect = function* expect() { | ||
return yield (yield operation).expect(); | ||
}, _Object$assign.forEach = function* forEach(visit) { | ||
return yield (yield operation).forEach(visit); | ||
}, _Object$assign.next = function* next() { | ||
return yield (yield operation).next(); | ||
}, _Object$assign)); | ||
} | ||
var Semaphore = function Semaphore() { | ||
@@ -183,3 +217,3 @@ var _this = this; | ||
function createSubscription(subscribe) { | ||
return function* () { | ||
return makeChainable(function* () { | ||
var results = []; | ||
@@ -231,3 +265,3 @@ var semaphore = new Semaphore(); | ||
return subscription; | ||
}; | ||
}); | ||
} | ||
@@ -241,36 +275,2 @@ | ||
function makeChainable(operation) { | ||
var _Object$assign; | ||
return Object.assign(operation, (_Object$assign = {}, _Object$assign[SymbolSubscribable] = function* () { | ||
return yield operation; | ||
}, _Object$assign.filter = function filter(predicate) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.filter(predicate); | ||
}); | ||
}); | ||
}, _Object$assign.match = function match(reference) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.match(reference); | ||
}); | ||
}); | ||
}, _Object$assign.map = function map(mapper) { | ||
return makeChainable(function* () { | ||
return yield ChainableSubscription.wrap(operation, function (inner) { | ||
return inner.map(mapper); | ||
}); | ||
}); | ||
}, _Object$assign.first = function* first() { | ||
return yield (yield operation).first(); | ||
}, _Object$assign.expect = function* expect() { | ||
return yield (yield operation).expect(); | ||
}, _Object$assign.forEach = function* forEach(visit) { | ||
return yield (yield operation).forEach(visit); | ||
}, _Object$assign.next = function* next() { | ||
return yield (yield operation).next(); | ||
}, _Object$assign)); | ||
} | ||
function subscribe(source) { | ||
@@ -277,0 +277,0 @@ return makeChainable(function* () { |
{ | ||
"name": "@effection/subscription", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "Effection Subscriptions", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,3 @@ | ||
export { Subscription, createSubscription } from './subscription'; | ||
export { Subscription } from './subscription'; | ||
export { createSubscription } from './create-subscription'; | ||
export { SymbolSubscribable } from './symbol-subscribable'; | ||
@@ -3,0 +4,0 @@ export { Subscribable } from './subscribable'; |
@@ -1,6 +0,3 @@ | ||
import { Operation, resource } from 'effection'; | ||
import { Operation } from 'effection'; | ||
import { ChainableSubscription } from './chainable-subscription'; | ||
import { Semaphore } from './semaphore'; | ||
export type Subscriber<T,TReturn> = (publish: (value: T) => void) => Operation<TReturn>; | ||
@@ -11,40 +8,1 @@ | ||
} | ||
export function createSubscription<T, TReturn>(subscribe: Subscriber<T,TReturn>): Operation<Subscription<T,TReturn>> { | ||
return function*() { | ||
let results: IteratorResult<T,TReturn>[] = []; | ||
let semaphore = new Semaphore<void>(); | ||
let publish = (value: T) => { | ||
results.push({ done: false, value }); | ||
semaphore.signal(); | ||
}; | ||
let next = async (): Promise<IteratorResult<T,TReturn>> => { | ||
let wait = semaphore.wait(); | ||
if (results.length > 0) { | ||
semaphore.signal(); | ||
} | ||
return wait.then(() => results.shift() as IteratorResult<T,TReturn>); | ||
}; | ||
let subscription = yield resource(new ChainableSubscription({ next }), function*() { | ||
try { | ||
let value = yield subscribe((value: T) => publish(value)); | ||
results.push({ done: true, value }); | ||
semaphore.signal(); | ||
} finally { | ||
publish = value => { throw InvalidPublication(value); } | ||
} | ||
}); | ||
return subscription; | ||
} | ||
} | ||
function InvalidPublication(value: unknown) { | ||
let error = new Error(`tried to publish a value: ${value} on an already finished subscription`); | ||
error.name = 'TypeError'; | ||
return error; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
100634
32
936