Socket
Socket
Sign inDemoInstall

@effection/subscription

Package Overview
Dependencies
Maintainers
1
Versions
151
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@effection/subscription - npm Package Compare versions

Comparing version 0.7.3 to 0.8.0-a7f9396

dist/chainable-subscription.d.ts

6

CHANGELOG.md
# @effection/subscription
## 0.8.0
### Minor Changes
- 8303e92: Add a free `subscribe` function, allow chaining of `map`, `filter` etc on subscriptions and deprecate chaining on subscribables.
## 0.7.3

@@ -4,0 +10,0 @@

export { Subscription, createSubscription } from './subscription';
export { SymbolSubscribable, SubscriptionSource, forEach, Subscribable } from './subscribable';
export { ChainableSubscription } from './chainable-subscription';
import { Operation } from 'effection';
import { ChainableSubscription } from './chainable-subscription';
import { SubscriptionSource } from './subscribable';
export declare function subscribe<T, TReturn>(source: SubscriptionSource<T, TReturn>): Operation<ChainableSubscription<T, TReturn>>;

1

dist/subscribable.d.ts

@@ -24,1 +24,2 @@ import { Operation } from 'effection';

}
export declare function subscribe<T, TReturn>(source: SubscriptionSource<T, TReturn>): Operation<Subscription<T, TReturn>>;

276

dist/subscription.cjs.development.js

@@ -5,79 +5,2 @@ 'use strict';

var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
}
};
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
});
};
};
function createSubscription(subscribe) {
return function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
});
semaphore.signal();
};
var next = function next() {
try {
var wait = semaphore.wait();
if (results.length > 0) {
semaphore.signal();
}
return Promise.resolve(wait.then(function () {
return results.shift();
}));
} catch (e) {
return Promise.reject(e);
}
};
var subscription = yield effection.resource({
next: next
}, function* () {
try {
var value = yield subscribe(function (value) {
return publish(value);
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
};
}
});
return subscription;
};
}
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
}
function matcher(reference) {

@@ -182,28 +105,196 @@ return function (value) {

}();
function subscribe(source) {
if (isSubscribable(source)) {
var subscriber = getSubscriber(source);
if (subscriber) {
return subscriber.call(source);
} else {
var error = new Error("cannot subscribe to " + source + " because it does not contain Symbol.subscription");
error.name = 'TypeError';
throw error;
}
return source[SymbolSubscribable]();
} else {
return source;
}
}
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
function isSubscribable(value) {
return !!getSubscriber(value);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
return !!value[SymbolSubscribable];
}
var DUMMY = {
next: function next() {
throw new Error('dummy');
}
};
var ChainableSubscription = /*#__PURE__*/function () {
function ChainableSubscription(subscription) {
this.subscription = subscription;
}
function getSubscriber(source) {
return source[SymbolSubscribable];
ChainableSubscription.of = function* of(source) {
var chain = new ChainableSubscription(DUMMY);
return yield effection.resource(chain, function* () {
chain.subscription = yield subscribe(source);
yield;
});
};
var _proto = ChainableSubscription.prototype;
_proto.filter = function filter(predicate) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
if (result.done) {
return result;
} else if (predicate(result.value)) {
return result;
}
}
}
});
};
_proto.match = function match(reference) {
return this.filter(matcher(reference));
};
_proto.map = function map(mapper) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
if (result.done) {
return result;
} else {
return {
done: false,
value: mapper(result.value)
};
}
}
}
});
};
_proto.first = function* first() {
var result = yield this.subscription.next();
if (result.done) {
return undefined;
} else {
return result.value;
}
};
_proto.expect = function* expect() {
var result = yield this.subscription.next();
if (result.done) {
throw new Error('expected subscription to contain a value');
} else {
return result.value;
}
};
_proto.forEach = function* forEach(visit) {
while (true) {
var result = yield this.subscription.next();
if (result.done) {
return result.value;
} else {
yield visit(result.value);
}
}
};
_proto.next = function next() {
return this.subscription.next();
};
return ChainableSubscription;
}();
var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
}
};
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
});
};
};
function createSubscription(subscribe) {
return function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
});
semaphore.signal();
};
var next = function next() {
try {
var wait = semaphore.wait();
if (results.length > 0) {
semaphore.signal();
}
return Promise.resolve(wait.then(function () {
return results.shift();
}));
} catch (e) {
return Promise.reject(e);
}
};
var subscription = yield effection.resource(new ChainableSubscription({
next: next
}), function* () {
try {
var value = yield subscribe(function (value) {
return publish(value);
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
};
}
});
return subscription;
};
}
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
}
function* subscribe$1(source) {
return yield ChainableSubscription.of(source);
}
exports.ChainableSubscription = ChainableSubscription;
exports.Subscribable = Subscribable;

@@ -213,2 +304,3 @@ exports.SymbolSubscribable = SymbolSubscribable;

exports.forEach = _forEach;
exports.subscribe = subscribe$1;
//# sourceMappingURL=subscription.cjs.development.js.map

@@ -1,2 +0,2 @@

"use strict";var n=require("effection"),r=function(){var n=this;this.waiters=[],this.signal=function(r){var t=n.waiters.pop();t&&t(r)},this.wait=function(){return new Promise((function(r){return n.waiters.push(r)}))}};function t(t){return function*(){var e=[],i=new r,u=function(n){e.push({done:!1,value:n}),i.signal()};return yield n.resource({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 t((function(n){return u(n)}));e.push({done:!0,value:n}),i.signal()}finally{u=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)}}}))}}var e=Symbol.for("Symbol.subscription");function*i(n,r){for(var t=yield c(n);;){var e=yield t.next();if(e.done)return e.value;yield r(e.value)}}var u={from:function(n){return new o(n)}},o=function(){function n(n){this.source=n}var r=n.prototype;return r[e]=function(){return c(this.source)},r.map=function(n){return this.chain((function(r){return function(t){return i(r,(function*(r){t(n(r))}))}}))},r.filter=function(n){return this.chain((function(r){return function(t){return i(r,(function*(r){n(r)&&t(r)}))}}))},r.match=function(n){return this.filter(function n(r){return function(t){if("object"==typeof t&&"object"==typeof r){var e=t;return Object.entries(r).every((function(r){var t=r[0];return n(r[1])(e[t])}))}return t===r}}(n))},r.chain=function(r){return new n(t(r(this.source)))},r.forEach=function(n){return i(this.source,n)},r.first=function*(){var n=yield c(this.source),r=yield n.next();return r.done?void 0:r.value},n}();function c(n){if(s(n)){var r=s(n);if(r)return r.call(n);var t=new Error("cannot subscribe to "+n+" because it does not contain Symbol.subscription");throw t.name="TypeError",t}return n}function s(n){return n[e]}exports.Subscribable=u,exports.SymbolSubscribable=e,exports.createSubscription=t,exports.forEach=i;
"use strict";var n=require("effection");function r(n){return function(t){if("object"==typeof t&&"object"==typeof n){var e=t;return Object.entries(n).every((function(n){var t=n[0];return r(n[1])(e[t])}))}return t===n}}var t=Symbol.for("Symbol.subscription");function*e(n,r){for(var t=yield o(n);;){var e=yield t.next();if(e.done)return e.value;yield r(e.value)}}var i={from:function(n){return new u(n)}},u=function(){function n(n){this.source=n}var i=n.prototype;return i[t]=function(){return o(this.source)},i.map=function(n){return this.chain((function(r){return function(t){return e(r,(function*(r){t(n(r))}))}}))},i.filter=function(n){return this.chain((function(r){return function(t){return e(r,(function*(r){n(r)&&t(r)}))}}))},i.match=function(n){return this.filter(r(n))},i.chain=function(r){return new n(a(r(this.source)))},i.forEach=function(n){return e(this.source,n)},i.first=function*(){var n=yield o(this.source),r=yield n.next();return r.done?void 0:r.value},n}();function o(n){return n[t]?n[t]():n}var c={next:function(){throw new Error("dummy")}},s=function(){function t(n){this.subscription=n}t.of=function*(r){var e=new t(c);return yield n.resource(e,(function*(){e.subscription=yield o(r),yield}))};var e=t.prototype;return e.filter=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var t=yield r.next();if(t.done)return t;if(n(t.value))return t}}})},e.match=function(n){return this.filter(r(n))},e.map=function(n){var r=this.subscription;return new t({next:function*(){for(;;){var t=yield r.next();return t.done?t:{done:!1,value:n(t.value)}}}})},e.first=function*(){var n=yield this.subscription.next();return n.done?void 0:n.value},e.expect=function*(){var n=yield this.subscription.next();if(n.done)throw new Error("expected subscription to contain a value");return n.value},e.forEach=function*(n){for(;;){var r=yield this.subscription.next();if(r.done)return r.value;yield n(r.value)}},e.next=function(){return this.subscription.next()},t}(),f=function(){var n=this;this.waiters=[],this.signal=function(r){var t=n.waiters.pop();t&&t(r)},this.wait=function(){return new Promise((function(r){return n.waiters.push(r)}))}};function a(r){return function*(){var t=[],e=new f,i=function(n){t.push({done:!1,value:n}),e.signal()};return yield n.resource(new s({next:function(){try{var n=e.wait();return t.length>0&&e.signal(),Promise.resolve(n.then((function(){return t.shift()})))}catch(n){return Promise.reject(n)}}}),(function*(){try{var n=yield r((function(n){return i(n)}));t.push({done:!0,value:n}),e.signal()}finally{i=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.ChainableSubscription=s,exports.Subscribable=i,exports.SymbolSubscribable=t,exports.createSubscription=a,exports.forEach=e,exports.subscribe=function*(n){return yield s.of(n)};
//# sourceMappingURL=subscription.cjs.production.min.js.map
import { resource } from 'effection';
var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
}
};
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
});
};
};
function createSubscription(subscribe) {
return function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
});
semaphore.signal();
};
var next = function next() {
try {
var wait = semaphore.wait();
if (results.length > 0) {
semaphore.signal();
}
return Promise.resolve(wait.then(function () {
return results.shift();
}));
} catch (e) {
return Promise.reject(e);
}
};
var subscription = yield resource({
next: next
}, function* () {
try {
var value = yield subscribe(function (value) {
return publish(value);
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
};
}
});
return subscription;
};
}
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
}
function matcher(reference) {

@@ -179,29 +102,196 @@ return function (value) {

}();
function subscribe(source) {
if (isSubscribable(source)) {
var subscriber = getSubscriber(source);
if (subscriber) {
return subscriber.call(source);
} else {
var error = new Error("cannot subscribe to " + source + " because it does not contain Symbol.subscription");
error.name = 'TypeError';
throw error;
}
return source[SymbolSubscribable]();
} else {
return source;
}
}
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
function isSubscribable(value) {
return !!getSubscriber(value);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
return !!value[SymbolSubscribable];
}
var DUMMY = {
next: function next() {
throw new Error('dummy');
}
};
var ChainableSubscription = /*#__PURE__*/function () {
function ChainableSubscription(subscription) {
this.subscription = subscription;
}
function getSubscriber(source) {
return source[SymbolSubscribable];
ChainableSubscription.of = function* of(source) {
var chain = new ChainableSubscription(DUMMY);
return yield resource(chain, function* () {
chain.subscription = yield subscribe(source);
yield;
});
};
var _proto = ChainableSubscription.prototype;
_proto.filter = function filter(predicate) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
if (result.done) {
return result;
} else if (predicate(result.value)) {
return result;
}
}
}
});
};
_proto.match = function match(reference) {
return this.filter(matcher(reference));
};
_proto.map = function map(mapper) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
if (result.done) {
return result;
} else {
return {
done: false,
value: mapper(result.value)
};
}
}
}
});
};
_proto.first = function* first() {
var result = yield this.subscription.next();
if (result.done) {
return undefined;
} else {
return result.value;
}
};
_proto.expect = function* expect() {
var result = yield this.subscription.next();
if (result.done) {
throw new Error('expected subscription to contain a value');
} else {
return result.value;
}
};
_proto.forEach = function* forEach(visit) {
while (true) {
var result = yield this.subscription.next();
if (result.done) {
return result.value;
} else {
yield visit(result.value);
}
}
};
_proto.next = function next() {
return this.subscription.next();
};
return ChainableSubscription;
}();
var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
}
};
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
});
};
};
function createSubscription(subscribe) {
return function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
});
semaphore.signal();
};
var next = function next() {
try {
var wait = semaphore.wait();
if (results.length > 0) {
semaphore.signal();
}
return Promise.resolve(wait.then(function () {
return results.shift();
}));
} catch (e) {
return Promise.reject(e);
}
};
var subscription = yield resource(new ChainableSubscription({
next: next
}), function* () {
try {
var value = yield subscribe(function (value) {
return publish(value);
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
};
}
});
return subscription;
};
}
export { Subscribable, SymbolSubscribable, createSubscription, _forEach as forEach };
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
}
function* subscribe$1(source) {
return yield ChainableSubscription.of(source);
}
export { ChainableSubscription, Subscribable, SymbolSubscribable, createSubscription, _forEach as forEach, subscribe$1 as subscribe };
//# sourceMappingURL=subscription.esm.js.map
{
"name": "@effection/subscription",
"version": "0.7.3",
"version": "0.8.0-a7f9396",
"description": "Effection Subscriptions",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

export { Subscription, createSubscription } from './subscription';
export { SymbolSubscribable, SubscriptionSource, forEach, Subscribable } from './subscribable';
export { ChainableSubscription } from './chainable-subscription';
import { Operation } from 'effection';
import { ChainableSubscription } from './chainable-subscription';
import { SubscriptionSource } from './subscribable';
export function* subscribe<T, TReturn>(source: SubscriptionSource<T,TReturn>): Operation<ChainableSubscription<T,TReturn>> {
return yield ChainableSubscription.of(source);
}

@@ -73,12 +73,5 @@ import { Operation } from 'effection';

function subscribe<T, TReturn>(source: SubscriptionSource<T,TReturn>): Operation<Subscription<T,TReturn>> {
export function subscribe<T, TReturn>(source: SubscriptionSource<T,TReturn>): Operation<Subscription<T,TReturn>> {
if (isSubscribable<T,TReturn>(source)) {
let subscriber = getSubscriber<T,TReturn>(source);
if (subscriber) {
return subscriber.call(source);
} else {
let error = new Error(`cannot subscribe to ${source} because it does not contain Symbol.subscription`)
error.name = 'TypeError';
throw error;
}
return source[SymbolSubscribable]()
} else {

@@ -89,9 +82,5 @@ return source;

function isSubscribable<T,TReturn>(value: unknown): value is Subscribable<T,TReturn> {
return !!getSubscriber<T,TReturn>(value);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getSubscriber<T,TReturn>(source: any): undefined | (() => Operation<Subscription<T,TReturn>>) {
return source[SymbolSubscribable] as () => Operation<Subscription<T,TReturn>>;
function isSubscribable<T,TReturn>(value: any): value is Subscribable<T,TReturn> {
return !!value[SymbolSubscribable];
}
import { Operation, resource } from 'effection';
import { ChainableSubscription } from './chainable-subscription';
import { Semaphore } from './semaphore';

@@ -30,3 +31,3 @@

let subscription = yield resource({ next }, function*() {
let subscription = yield resource(new ChainableSubscription({ next }), function*() {
try {

@@ -33,0 +34,0 @@ let value = yield subscribe((value: T) => publish(value));

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc