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 1.0.0 to 2.0.0-beta.0

dist/operation-iterable.d.ts

148

CHANGELOG.md
# @effection/subscription
## 2.0.0-preview.14
### Patch Changes
- Updated dependencies [9998088]
- Updated dependencies [2bce454]
- Updated dependencies [1981b35]
- Updated dependencies [88dc59a]
- @effection/core@2.0.0-preview.12
## 2.0.0-preview.13
### Patch Changes
- Updated dependencies [88eca21]
- Updated dependencies [ae8d090]
- Updated dependencies [8bb4514]
- Updated dependencies [44c354d]
- @effection/core@2.0.0-preview.11
## 2.0.0-preview.12
### Patch Changes
- Updated dependencies [625b521]
- Updated dependencies [a06c679]
- Updated dependencies [4d04159]
- Updated dependencies [625b521]
- @effection/core@2.0.0-preview.10
## 2.0.0-preview.11
### Minor Changes
- 92f921e: Turn streams into resources which return a subscription
## 2.0.0-preview.10
### Patch Changes
- Updated dependencies [110a2cd]
- Updated dependencies [e2545b2]
- Updated dependencies [2b92370]
- Updated dependencies [00562fd]
- Updated dependencies [110a2cd]
- Updated dependencies [110a2cd]
- Updated dependencies [02446ad]
- Updated dependencies [da86a9c]
- @effection/core@2.0.0-preview.9
## 2.0.0-preview.9
### Patch Changes
- a13987f: make operation resolution an interface. Make operation iterators
an operation.
- Updated dependencies [a13987f]
- @effection/core@2.0.0-preview.8
## 2.0.0-preview.8
### Patch Changes
- Updated dependencies [2bad074]
- @effection/core@2.0.0-preview.7
## 2.0.0-preview.7
### Minor Changes
- 3db7270: Make Queue a first class citizen
## 2.0.0-preview.6
### Minor Changes
- 0dca571: Filter with a type predicate can narrow type of stream
### Patch Changes
- 1222756: Use strict dependency requirements for internal dependencies while in prerelease mode
- Updated dependencies [1222756]
- @effection/core@2.0.0-preview.6
## 2.0.0-preview.5
### Minor Changes
- 0b24415: Add WritableStream interface and implement it for channels
- 22e5230: Add `join` method on stream to return stream result
- 3983202: Add `stringBuffer` method to stream which buffers stream to a string
- 2c2749d: Add a `buffer` method on Stream to buffer stream contents for later replay
### Patch Changes
- Updated dependencies [70c358f]
- @effection/core@2.0.0-preview.5
## 2.0.0-preview.4
### Minor Changes
- ab41f6a: Rename `Subscribable` to `Stream`
### Patch Changes
- Updated dependencies [72f743c]
- @effection/core@2.0.0-preview.4
## 2.0.0-preview.3
### Minor Changes
- 3ca4cd4: Add `createSubscribable` and `Subscribable` interface
### Patch Changes
- Updated dependencies [bdedf68]
- Updated dependencies [2bf5ef4]
- @effection/core@2.0.0-preview.3
## 2.0.0-preview.2
### Patch Changes
- 93ec0d6: Include CHANGELOGS and src with all packages
- Updated dependencies [93ec0d6]
- @effection/core@2.0.0-preview.2
## 2.0.0-preview.1
### Patch Changes
- 80143d5: Fix packaging
- Updated dependencies [80143d5]
- @effection/core@2.0.0-preview.1
## 2.0.0-preview.0
### Major Changes
- Version 2
### Patch Changes
- Updated dependencies [undefined]
- effection@2.0.0-preview.0
## 1.0.0

@@ -4,0 +152,0 @@

16

dist/index.d.ts

@@ -0,11 +1,7 @@

export { SymbolOperationIterable } from './symbol-operation-iterable';
export { OperationIterable } from './operation-iterable';
export { OperationIterator } from './operation-iterator';
export { Subscription } from './subscription';
export { createSubscription } from './create-subscription';
export { SymbolSubscribable } from './symbol-subscribable';
export { Subscribable } from './subscribable';
export { SubscriptionSource } from './subscription-source';
export { ChainableSubscription } from './chainable-subscription';
export { ChainableSubscribable } from './chainable-subscribable';
export { subscribe } from './subscribe';
import { Operation } from 'effection';
import { SubscriptionSource } from './subscription-source';
export declare const forEach: <T, TReturn>(source: SubscriptionSource<T, TReturn>, visit: (value: T) => Operation<void>) => Operation<TReturn>;
export { createStream, Stream, StringBufferStream } from './stream';
export { Writable, WritableStream } from './writable-stream';
export { Queue, createQueue } from './queue';
'use strict';
var effection = require('effection');
function matcher(reference) {
return function (value) {
if (typeof value === 'object' && typeof reference === 'object') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedValue = value; // eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedReference = reference;
return Object.entries(castedReference).every(function (_ref) {
var key = _ref[0],
ref = _ref[1];
return matcher(ref)(castedValue[key]);
});
} else {
return value === reference;
}
};
}
/**

@@ -29,3 +9,3 @@ * Hack Zone!

*
* In order to satisfy TypeScript that the SymbolSubscribable
* In order to satisfy TypeScript that the SymbolOperationIterable
* represents the same value across different versions of the same

@@ -36,4 +16,3 @@ * package, we need to declare a "fake" global variable that does not

* use the `typeof` for the value, as the type of of our local symbol
* subscribable, and TypeScript will think that all of the types are
* the same.
* and TypeScript will think that all of the types are the same.
*

@@ -43,257 +22,308 @@ * See https://github.com/microsoft/TypeScript/issues/8099#issuecomment-210134773

*/
var SymbolSubscribable = /*#__PURE__*/Symbol["for"]('Symbol.subscription');
var SymbolOperationIterable = /*#__PURE__*/Symbol["for"]('effection/iterable');
function isSubscribable(value) {
return !!value[SymbolSubscribable];
}
function rawSubscribe(source) {
if (isSubscribable(source)) {
return source[SymbolSubscribable]();
} else {
return source;
function _defineEnumerableProperties(obj, descs) {
for (var key in descs) {
var desc = descs[key];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, key, desc);
}
}
var DUMMY = {
next: function next() {
throw new Error('dummy');
if (Object.getOwnPropertySymbols) {
var objectSymbols = Object.getOwnPropertySymbols(descs);
for (var i = 0; i < objectSymbols.length; i++) {
var sym = objectSymbols[i];
var desc = descs[sym];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, sym, desc);
}
}
};
var ChainableSubscription = /*#__PURE__*/function () {
function ChainableSubscription(subscription) {
this.subscription = subscription;
}
ChainableSubscription.of = function* of(source) {
var chain = new ChainableSubscription(DUMMY);
return yield effection.resource(chain, function* () {
chain.subscription = yield rawSubscribe(source);
yield;
});
};
return obj;
}
ChainableSubscription.wrap = function* wrap(source, fn) {
var chain = new ChainableSubscription(DUMMY);
return yield effection.resource(chain, function* () {
var subscription = yield source;
chain.subscription = fn(subscription);
yield;
});
};
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
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;
}
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
});
};
}
_proto.match = function match(reference) {
return this.filter(matcher(reference));
return target;
};
_proto.map = function map(mapper) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
return _extends.apply(this, arguments);
}
if (result.done) {
return result;
} else {
return {
done: false,
value: mapper(result.value)
};
}
}
}
});
function matcher(reference) {
return function (value) {
if (typeof value === 'object' && typeof reference === 'object') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedValue = value; // eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedReference = reference;
return Object.entries(castedReference).every(function (_ref) {
var key = _ref[0],
ref = _ref[1];
return matcher(ref)(castedValue[key]);
});
} else {
return value === reference;
}
};
}
_proto.first = function* first() {
var result = yield this.subscription.next();
function createQueue() {
var waiters = [];
var values = [];
if (result.done) {
return undefined;
var send = function send(value) {
var next = waiters.pop();
if (next) {
next({
value: value,
done: false
});
} else {
return result.value;
values.push({
value: value,
done: false
});
}
};
_proto.expect = function* expect() {
var result = yield this.subscription.next();
var _close = function close(value) {
if (result.done) {
throw new Error('expected subscription to contain a value');
var next = waiters.pop();
if (next) {
next({
value: value,
done: true
});
} else {
return result.value;
values.push({
value: value,
done: true
});
}
};
_proto.forEach = function* forEach(visit) {
while (true) {
var result = yield this.subscription.next();
var next = function next() {
return {
perform: function perform(resolve) {
if (values.length) {
resolve(values.shift());
} else {
waiters.push(resolve);
return function () {
var index = waiters.indexOf(resolve);
if (result.done) {
return result.value;
} else {
yield visit(result.value);
if (index > -1) {
waiters.splice(index, 1);
}
};
}
}
}
};
};
_proto.next = function next() {
return this.subscription.next();
};
var subscription = {
next: next,
close: function close() {
return _close(arguments.length <= 0 ? undefined : arguments[0]);
},
closeWith: _close,
first: function first() {
return function* () {
var result = yield next();
return ChainableSubscription;
}();
if (result.done) {
return undefined;
} else {
return result.value;
}
};
},
expect: function expect() {
return function* () {
var result = yield next();
function makeChainable(operation) {
var _Object$assign;
if (result.done) {
throw new Error('expected to contain a value');
} else {
return result.value;
}
};
},
forEach: function forEach(visit) {
return function* () {
while (true) {
var result = yield next();
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));
}
if (result.done) {
return result.value;
} else {
var operation = visit(result.value);
var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
if (operation) {
yield operation;
}
}
}
};
},
join: function join() {
return subscription.forEach(function () {});
},
collect: function collect() {
return function* () {
var items = [];
var result = yield subscription.forEach(function (item) {
return function* () {
items.push(item);
};
});
return function* () {
yield* items;
return result;
}();
};
},
toArray: function toArray() {
return function* () {
return Array.from(yield subscription.collect());
};
}
};
return _extends({
send: send,
subscription: subscription
}, subscription);
}
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
function createStream(callback) {
var _stream, _mutatorMap;
var subscribe = function subscribe(task) {
var queue = createQueue();
task.spawn(function* () {
var result = yield callback(queue.send);
queue.closeWith(result);
});
return queue.subscription;
};
};
function createSubscription(subscribe) {
return makeChainable(function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
function filter(predicate) {
return createStream(function (publish) {
return stream.forEach(function (value) {
return function* () {
if (predicate(value)) {
publish(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);
});
}
var stream = (_stream = {
subscribe: subscribe,
filter: filter,
init: function* init(task) {
return subscribe(task);
},
match: function match(reference) {
return stream.filter(matcher(reference));
},
map: function map(mapper) {
return createStream(function (publish) {
return stream.forEach(function (value) {
return function* () {
publish(mapper(value));
};
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
});
},
first: function first() {
return function* (task) {
return yield subscribe(task).first();
};
},
expect: function expect() {
return function* (task) {
return yield subscribe(task).expect();
};
},
forEach: function forEach(visit) {
return function* (task) {
return yield subscribe(task).forEach(visit);
};
},
join: function join() {
return function* (task) {
return yield subscribe(task).join();
};
},
collect: function collect() {
return function* (task) {
return yield subscribe(task).collect();
};
},
toArray: function toArray() {
return function* (task) {
return yield subscribe(task).toArray();
};
},
buffer: function buffer(scope) {
var buffer = [];
scope.spawn(stream.forEach(function (m) {
buffer.push(m);
}));
return createStream(function (publish) {
return function* () {
buffer.forEach(publish);
return yield stream.forEach(publish);
};
}
});
return subscription;
});
}
});
},
stringBuffer: function stringBuffer(scope) {
var buffer = "";
scope.spawn(stream.forEach(function (m) {
buffer += "" + m;
}));
var result = createStream(function (publish) {
return function* () {
var internalBuffer = buffer;
publish(internalBuffer);
return yield stream.forEach(function (m) {
internalBuffer += "" + m;
publish(internalBuffer);
});
};
});
return _extends({}, result, {
get value() {
return buffer;
}
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
});
}
}, _mutatorMap = {}, _mutatorMap[SymbolOperationIterable] = _mutatorMap[SymbolOperationIterable] || {}, _mutatorMap[SymbolOperationIterable].get = function () {
return subscribe;
}, _defineEnumerableProperties(_stream, _mutatorMap), _stream);
return stream;
}
function subscribe(source) {
return makeChainable(function* () {
return yield ChainableSubscription.of(source);
});
}
var Subscribable = {
from: /*#__PURE__*/effection.deprecated('`Subscribable.from(source)` is deprecated, use `subscribe(source).map(...)` instead', function (source) {
return subscribe(source);
})
};
var forEach = /*#__PURE__*/effection.deprecated('`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead', function* forEach(source, visit) {
return yield subscribe(source).forEach(visit);
});
exports.ChainableSubscription = ChainableSubscription;
exports.Subscribable = Subscribable;
exports.SymbolSubscribable = SymbolSubscribable;
exports.createSubscription = createSubscription;
exports.forEach = forEach;
exports.subscribe = subscribe;
exports.SymbolOperationIterable = SymbolOperationIterable;
exports.createQueue = createQueue;
exports.createStream = createStream;
//# sourceMappingURL=subscription.cjs.development.js.map

@@ -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}();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:n.deprecated("`Subscribable.from(source)` is deprecated, use `subscribe(source).map(...)` instead",(function(n){return o(n)}))},s=n.deprecated("`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead",(function*(n,r){return yield o(n).forEach(r)}));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=s,exports.subscribe=o;
"use strict";var n=Symbol.for("effection/iterable");function r(){return(r=Object.assign||function(n){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e])}return n}).apply(this,arguments)}function t(){var n=[],t=[],e=function(r){var e=n.pop();e?e({value:r,done:!0}):t.push({value:r,done:!0})},u=function(){return{perform:function(r){if(!t.length)return n.push(r),function(){var t=n.indexOf(r);t>-1&&n.splice(t,1)};r(t.shift())}}},o={next:u,close:function(){return e(arguments.length<=0?void 0:arguments[0])},closeWith:e,first:function(){return function*(){var n=yield u();return n.done?void 0:n.value}},expect:function(){return function*(){var n=yield u();if(n.done)throw new Error("expected to contain a value");return n.value}},forEach:function(n){return function*(){for(;;){var r=yield u();if(r.done)return r.value;var t=n(r.value);t&&(yield t)}}},join:function(){return o.forEach((function(){}))},collect:function(){return function*(){var n=[],r=yield o.forEach((function(r){return function*(){n.push(r)}}));return function*(){return yield*n,r}()}},toArray:function(){return function*(){return Array.from(yield o.collect())}}};return r({send:function(r){var e=n.pop();e?e({value:r,done:!1}):t.push({value:r,done:!1})},subscription:o},o)}exports.SymbolOperationIterable=n,exports.createQueue=t,exports.createStream=function e(u){var o,i,c=function(n){var r=t();return n.spawn((function*(){var n=yield u(r.send);r.closeWith(n)})),r.subscription},f=(o={subscribe:c,filter:function(n){return e((function(r){return f.forEach((function(t){return function*(){n(t)&&r(t)}}))}))},init:function*(n){return c(n)},match:function(n){return f.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))},map:function(n){return e((function(r){return f.forEach((function(t){return function*(){r(n(t))}}))}))},first:function(){return function*(n){return yield c(n).first()}},expect:function(){return function*(n){return yield c(n).expect()}},forEach:function(n){return function*(r){return yield c(r).forEach(n)}},join:function(){return function*(n){return yield c(n).join()}},collect:function(){return function*(n){return yield c(n).collect()}},toArray:function(){return function*(n){return yield c(n).toArray()}},buffer:function(n){var r=[];return n.spawn(f.forEach((function(n){r.push(n)}))),e((function(n){return function*(){return r.forEach(n),yield f.forEach(n)}}))},stringBuffer:function(n){var t="";return n.spawn(f.forEach((function(n){t+=""+n}))),r({},e((function(n){return function*(){var r=t;return n(r),yield f.forEach((function(t){n(r+=""+t)}))}})),{get value(){return t}})}},(i={})[n]=i[n]||{},i[n].get=function(){return c},function(n,r){for(var t in r)(o=r[t]).configurable=o.enumerable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(n,t,o);if(Object.getOwnPropertySymbols)for(var e=Object.getOwnPropertySymbols(r),u=0;u<e.length;u++){var o,i=e[u];(o=r[i]).configurable=o.enumerable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(n,i,o)}}(o,i),o);return f};
//# sourceMappingURL=subscription.cjs.production.min.js.map

@@ -1,5 +0,10 @@

import { Operation } from 'effection';
export declare type Subscriber<T, TReturn = undefined> = (publish: (value: T) => void) => Operation<TReturn>;
export interface Subscription<T, TReturn = undefined> {
next(): Operation<IteratorResult<T, TReturn>>;
import { Operation } from '@effection/core';
import { OperationIterator } from './operation-iterator';
export interface Subscription<T, TReturn = undefined> extends OperationIterator<T, TReturn> {
first(): Operation<T | undefined>;
expect(): Operation<T>;
forEach(visit: (value: T) => (Operation<void> | void)): Operation<TReturn>;
join(): Operation<TReturn>;
collect(): Operation<Iterator<T, TReturn>>;
toArray(): Operation<T[]>;
}

@@ -1,21 +0,1 @@

import { resource, deprecated } from 'effection';
function matcher(reference) {
return function (value) {
if (typeof value === 'object' && typeof reference === 'object') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedValue = value; // eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedReference = reference;
return Object.entries(castedReference).every(function (_ref) {
var key = _ref[0],
ref = _ref[1];
return matcher(ref)(castedValue[key]);
});
} else {
return value === reference;
}
};
}
/**

@@ -27,3 +7,3 @@ * Hack Zone!

*
* In order to satisfy TypeScript that the SymbolSubscribable
* In order to satisfy TypeScript that the SymbolOperationIterable
* represents the same value across different versions of the same

@@ -34,4 +14,3 @@ * package, we need to declare a "fake" global variable that does not

* use the `typeof` for the value, as the type of of our local symbol
* subscribable, and TypeScript will think that all of the types are
* the same.
* and TypeScript will think that all of the types are the same.
*

@@ -41,252 +20,306 @@ * See https://github.com/microsoft/TypeScript/issues/8099#issuecomment-210134773

*/
var SymbolSubscribable = /*#__PURE__*/Symbol["for"]('Symbol.subscription');
var SymbolOperationIterable = /*#__PURE__*/Symbol["for"]('effection/iterable');
function isSubscribable(value) {
return !!value[SymbolSubscribable];
}
function rawSubscribe(source) {
if (isSubscribable(source)) {
return source[SymbolSubscribable]();
} else {
return source;
function _defineEnumerableProperties(obj, descs) {
for (var key in descs) {
var desc = descs[key];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, key, desc);
}
}
var DUMMY = {
next: function next() {
throw new Error('dummy');
if (Object.getOwnPropertySymbols) {
var objectSymbols = Object.getOwnPropertySymbols(descs);
for (var i = 0; i < objectSymbols.length; i++) {
var sym = objectSymbols[i];
var desc = descs[sym];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, sym, desc);
}
}
};
var ChainableSubscription = /*#__PURE__*/function () {
function ChainableSubscription(subscription) {
this.subscription = subscription;
}
ChainableSubscription.of = function* of(source) {
var chain = new ChainableSubscription(DUMMY);
return yield resource(chain, function* () {
chain.subscription = yield rawSubscribe(source);
yield;
});
};
return obj;
}
ChainableSubscription.wrap = function* wrap(source, fn) {
var chain = new ChainableSubscription(DUMMY);
return yield resource(chain, function* () {
var subscription = yield source;
chain.subscription = fn(subscription);
yield;
});
};
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
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;
}
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
});
};
}
_proto.match = function match(reference) {
return this.filter(matcher(reference));
return target;
};
_proto.map = function map(mapper) {
var subscription = this.subscription;
return new ChainableSubscription({
next: function* next() {
while (true) {
var result = yield subscription.next();
return _extends.apply(this, arguments);
}
if (result.done) {
return result;
} else {
return {
done: false,
value: mapper(result.value)
};
}
}
}
});
function matcher(reference) {
return function (value) {
if (typeof value === 'object' && typeof reference === 'object') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedValue = value; // eslint-disable-next-line @typescript-eslint/no-explicit-any
var castedReference = reference;
return Object.entries(castedReference).every(function (_ref) {
var key = _ref[0],
ref = _ref[1];
return matcher(ref)(castedValue[key]);
});
} else {
return value === reference;
}
};
}
_proto.first = function* first() {
var result = yield this.subscription.next();
function createQueue() {
var waiters = [];
var values = [];
if (result.done) {
return undefined;
var send = function send(value) {
var next = waiters.pop();
if (next) {
next({
value: value,
done: false
});
} else {
return result.value;
values.push({
value: value,
done: false
});
}
};
_proto.expect = function* expect() {
var result = yield this.subscription.next();
var _close = function close(value) {
if (result.done) {
throw new Error('expected subscription to contain a value');
var next = waiters.pop();
if (next) {
next({
value: value,
done: true
});
} else {
return result.value;
values.push({
value: value,
done: true
});
}
};
_proto.forEach = function* forEach(visit) {
while (true) {
var result = yield this.subscription.next();
var next = function next() {
return {
perform: function perform(resolve) {
if (values.length) {
resolve(values.shift());
} else {
waiters.push(resolve);
return function () {
var index = waiters.indexOf(resolve);
if (result.done) {
return result.value;
} else {
yield visit(result.value);
if (index > -1) {
waiters.splice(index, 1);
}
};
}
}
}
};
};
_proto.next = function next() {
return this.subscription.next();
};
var subscription = {
next: next,
close: function close() {
return _close(arguments.length <= 0 ? undefined : arguments[0]);
},
closeWith: _close,
first: function first() {
return function* () {
var result = yield next();
return ChainableSubscription;
}();
if (result.done) {
return undefined;
} else {
return result.value;
}
};
},
expect: function expect() {
return function* () {
var result = yield next();
function makeChainable(operation) {
var _Object$assign;
if (result.done) {
throw new Error('expected to contain a value');
} else {
return result.value;
}
};
},
forEach: function forEach(visit) {
return function* () {
while (true) {
var result = yield next();
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));
}
if (result.done) {
return result.value;
} else {
var operation = visit(result.value);
var Semaphore = function Semaphore() {
var _this = this;
this.waiters = [];
this.signal = function (value) {
var next = _this.waiters.pop();
if (next) {
next(value);
if (operation) {
yield operation;
}
}
}
};
},
join: function join() {
return subscription.forEach(function () {});
},
collect: function collect() {
return function* () {
var items = [];
var result = yield subscription.forEach(function (item) {
return function* () {
items.push(item);
};
});
return function* () {
yield* items;
return result;
}();
};
},
toArray: function toArray() {
return function* () {
return Array.from(yield subscription.collect());
};
}
};
return _extends({
send: send,
subscription: subscription
}, subscription);
}
this.wait = function () {
return new Promise(function (resolve) {
return _this.waiters.push(resolve);
function createStream(callback) {
var _stream, _mutatorMap;
var subscribe = function subscribe(task) {
var queue = createQueue();
task.spawn(function* () {
var result = yield callback(queue.send);
queue.closeWith(result);
});
return queue.subscription;
};
};
function createSubscription(subscribe) {
return makeChainable(function* () {
var results = [];
var semaphore = new Semaphore();
var publish = function publish(value) {
results.push({
done: false,
value: value
function filter(predicate) {
return createStream(function (publish) {
return stream.forEach(function (value) {
return function* () {
if (predicate(value)) {
publish(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);
});
}
var stream = (_stream = {
subscribe: subscribe,
filter: filter,
init: function* init(task) {
return subscribe(task);
},
match: function match(reference) {
return stream.filter(matcher(reference));
},
map: function map(mapper) {
return createStream(function (publish) {
return stream.forEach(function (value) {
return function* () {
publish(mapper(value));
};
});
results.push({
done: true,
value: value
});
semaphore.signal();
} finally {
publish = function publish(value) {
throw InvalidPublication(value);
});
},
first: function first() {
return function* (task) {
return yield subscribe(task).first();
};
},
expect: function expect() {
return function* (task) {
return yield subscribe(task).expect();
};
},
forEach: function forEach(visit) {
return function* (task) {
return yield subscribe(task).forEach(visit);
};
},
join: function join() {
return function* (task) {
return yield subscribe(task).join();
};
},
collect: function collect() {
return function* (task) {
return yield subscribe(task).collect();
};
},
toArray: function toArray() {
return function* (task) {
return yield subscribe(task).toArray();
};
},
buffer: function buffer(scope) {
var buffer = [];
scope.spawn(stream.forEach(function (m) {
buffer.push(m);
}));
return createStream(function (publish) {
return function* () {
buffer.forEach(publish);
return yield stream.forEach(publish);
};
}
});
return subscription;
});
}
});
},
stringBuffer: function stringBuffer(scope) {
var buffer = "";
scope.spawn(stream.forEach(function (m) {
buffer += "" + m;
}));
var result = createStream(function (publish) {
return function* () {
var internalBuffer = buffer;
publish(internalBuffer);
return yield stream.forEach(function (m) {
internalBuffer += "" + m;
publish(internalBuffer);
});
};
});
return _extends({}, result, {
get value() {
return buffer;
}
function InvalidPublication(value) {
var error = new Error("tried to publish a value: " + value + " on an already finished subscription");
error.name = 'TypeError';
return error;
});
}
}, _mutatorMap = {}, _mutatorMap[SymbolOperationIterable] = _mutatorMap[SymbolOperationIterable] || {}, _mutatorMap[SymbolOperationIterable].get = function () {
return subscribe;
}, _defineEnumerableProperties(_stream, _mutatorMap), _stream);
return stream;
}
function subscribe(source) {
return makeChainable(function* () {
return yield ChainableSubscription.of(source);
});
}
var Subscribable = {
from: /*#__PURE__*/deprecated('`Subscribable.from(source)` is deprecated, use `subscribe(source).map(...)` instead', function (source) {
return subscribe(source);
})
};
var forEach = /*#__PURE__*/deprecated('`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead', function* forEach(source, visit) {
return yield subscribe(source).forEach(visit);
});
export { ChainableSubscription, Subscribable, SymbolSubscribable, createSubscription, forEach, subscribe };
export { SymbolOperationIterable, createQueue, createStream };
//# sourceMappingURL=subscription.esm.js.map
{
"name": "@effection/subscription",
"version": "1.0.0",
"version": "2.0.0-beta.0",
"description": "Effection Subscriptions",

@@ -12,2 +12,3 @@ "main": "dist/index.js",

"README.md",
"CHANGELOG.md",
"dist/**/*",

@@ -23,8 +24,10 @@ "src/**/*"

"dependencies": {
"effection": "^1.0.0"
"@effection/core": "2.0.0-beta.0"
},
"devDependencies": {
"@effection/mocha": "2.0.0-beta.0",
"@frontside/tsconfig": "0.0.1",
"@types/mocha": "^8.0.3",
"expect": "^25.4.0",
"mocha": "^7.1.1",
"mocha": "^8.3.1",
"ts-node": "^8.9.0",

@@ -31,0 +34,0 @@ "tsdx": "0.13.2",

@@ -0,19 +1,7 @@

export { SymbolOperationIterable } from './symbol-operation-iterable';
export { OperationIterable } from './operation-iterable';
export { OperationIterator } from './operation-iterator';
export { Subscription } from './subscription';
export { createSubscription } from './create-subscription';
export { SymbolSubscribable } from './symbol-subscribable';
export { Subscribable } from './subscribable';
export { SubscriptionSource } from './subscription-source';
export { ChainableSubscription } from './chainable-subscription';
export { ChainableSubscribable } from './chainable-subscribable';
export { subscribe } from './subscribe';
import { Operation, deprecated } from 'effection';
import { SubscriptionSource } from './subscription-source';
import { subscribe } from './subscribe';
export const forEach = deprecated(
'`forEach(source, ...)` is deprecated, use `subscribe(source).forEach(...)` instead',
function* forEach<T,TReturn>(source: SubscriptionSource<T,TReturn>, visit: (value: T) => Operation<void>): Operation<TReturn> {
return yield subscribe<T, TReturn>(source).forEach(visit);
}
);
export { createStream, Stream, StringBufferStream } from './stream';
export { Writable, WritableStream } from './writable-stream';
export { Queue, createQueue } from './queue';

@@ -1,7 +0,13 @@

import { Operation } from 'effection';
import { Operation } from '@effection/core';
import { OperationIterator } from './operation-iterator';
export type Subscriber<T,TReturn = undefined> = (publish: (value: T) => void) => Operation<TReturn>;
type Callback<T,TReturn> = (publish: (value: T) => void) => Operation<TReturn>;
export interface Subscription<T,TReturn = undefined> {
next(): Operation<IteratorResult<T,TReturn>>;
export interface Subscription<T, TReturn = undefined> extends OperationIterator<T, TReturn> {
first(): Operation<T | undefined>;
expect(): Operation<T>;
forEach(visit: (value: T) => (Operation<void> | void)): Operation<TReturn>;
join(): Operation<TReturn>;
collect(): Operation<Iterator<T, TReturn>>;
toArray(): Operation<T[]>;
}

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