callbag-state
Advanced tools
Comparing version 0.1.9 to 0.2.0
@@ -1,1 +0,1 @@ | ||
var callbagState=function(t){"use strict";function e(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function n(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return r(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function o(t){return!t||!t.subs}function u(t,e,r){n(r).forEach((function(n){r.indexOf(n)>=0&&n(t,e)}))}function a(t){return null==t||"number"==typeof t||"string"==typeof t||"boolean"==typeof t||t instanceof Date}function i(t,e){if(t!==e){if(!a(t)&&!a(e)){var n={subs:{}},r=new Set,o=!1;for(var u in t){r.add(u);var f=i(t[u],e[u]);f&&(n.subs[u]=f,o=!0)}for(var c in e)r.has(c)||(n.subs[c]=i(t[c],e[c]),o=!0);return o?n:void 0}return t instanceof Date&&e instanceof Date&&+t!=+e||t!==e?{from:t,to:e}:void 0}}function f(t){if(o(t.trace)&&t.trace){var e=i(t.trace.from,t.trace.to);if(e)return{value:t.value,trace:e}}return t}function c(t,n,r){var a=[],i=t,s=void 0,l=function(t){u(2,t,a),a.length=0,s&&(s(2,t),s=void 0)},v=function(t,e){if(0===t){var r=e;a.push(r),r(0,(function(t){if(2===t){var e=a.indexOf(r);e>=0&&a.splice(e,1),0===a.length&&l()}})),1===a.length&&n(0,(function(t,e){if(0===t)s=e;else if(1===t){var n=f(e);n.value!==i&&(i=n.value),u(1,n,a)}else 2===t&&l(e)}))}},d=function(t,e){if(0===t){var n=e;v(0,(function(t,e){0===t?(n(0,e),n(1,i)):1===t?n(1,e.value):2===t&&n(2,e)}))}else 1===t?r(1,{value:e,trace:{from:i,to:e}}):2===t&&(r(2,e),l(e))};return d.get=function(){return i},d.set=function(t){return d(1,t)},d.clear=function(){return d(2)},d.downstream=function(){return v},d.upstream=function(){return r},d.sub=function(t){var n,u,a,f=c(i?i[t]:void 0,(n=v,u=t,a=function(){return f.get()},function(t,e){0===t&&n(0,(function(t,n){var r;if(1===t){var i=n,f=i.value?i.value[u]:void 0;(o(i.trace)&&f!==a()||!o(i.trace)&&u in(null===(r=i.trace)||void 0===r?void 0:r.subs))&&e(1,{value:f,trace:o(i.trace)?void 0:i.trace.subs[u]})}else e(t,n)}))}),function(t,n,r){return function(o,u){if(1===o){var a=u;r&&(r[n]=a.value),t(1,{value:r,trace:{subs:e({},n,a.trace)}})}else 2===o&&u&&t(2,u)}}(r,t,i));return f},d}function s(t){var e,n=(e=void 0,function(t,n){0===t?e||(e=n)(0,(function(t){2===t&&(e=void 0)})):e&&(e(t,n),2===t&&(e=void 0))});return c(t,n,n)}return t._Data=1,t._End=2,t._Start=0,t.broadcast=u,t.change=function(t,e){var n=i(t,e);if(n)return{value:e,trace:n}},t.default=s,t.isLeaf=o,t.isState=function(t){return t&&"function"==typeof t&&2===t.length&&t.get&&"function"==typeof t.get&&0===t.get.length&&t.set&&"function"==typeof t.set&&1===t.set.length&&t.clear&&"function"==typeof t.clear&&0===t.clear.length&&t.downstream&&"function"==typeof t.downstream&&0===t.downstream.length&&t.upstream&&"function"==typeof t.upstream&&0===t.upstream.length&&t.sub&&"function"==typeof t.sub&&1===t.sub.length},t.makeState=c,t.postTrace=f,t.state=s,t.trace=i,Object.defineProperty(t,"__esModule",{value:!0}),t}({}); | ||
var callbagState=function(t){"use strict";function e(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function n(t){return!t||!t.subs}function i(t,e,i,a,r){var s;if(1===a){var u=r,o=u.value?u.value[t]:void 0;(n(u.trace)&&o!==e()||!n(u.trace)&&t in(null===(s=u.trace)||void 0===s?void 0:s.subs))&&i(1,{value:o,trace:n(u.trace)?void 0:u.trace.subs[t]})}else i(a,r)}function a(t,e,n){return function(a,r){0===a&&t(0,i.bind(null,e,n,r))}}function r(t,e,n){for(var i=n.slice(0),a=0;a<i.length;a++){var r=i[a];-1!==n.indexOf(r)&&r(t,e)}}function s(t){return null==t||"number"==typeof t||"string"==typeof t||"boolean"==typeof t||t instanceof Date}function u(t,e){if(t!==e){if(!s(t)&&!s(e)){var n={subs:{}},i=new Set,a=!1;for(var r in t){i.add(r);var o=u(t[r],e[r]);o&&(n.subs[r]=o,a=!0)}for(var f in e)i.has(f)||(n.subs[f]=u(t[f],e[f]),a=!0);return a?n:void 0}return t instanceof Date&&e instanceof Date&&+t!=+e||t!==e?{from:t,to:e}:void 0}}function o(t){if(n(t.trace)&&t.trace){var e=u(t.trace.from,t.trace.to);if(e)return{value:t.value,trace:e}}return t}function f(t,e,n){r(2,n,t),t.length=0,e&&e(2,n)}function c(){return this(100)}function l(t){this(1,t)}function v(){this(2)}function h(){return this(101)}function d(){return this(102)}function b(t){var n,i,r,s=y(this.get()?this.get()[t]:void 0,a(this.downstream(),t,(function(){return s.get()})),(n=this.upstream(),i=t,r=this.get(),function(t,a){if(1===t){var s=a;r&&(r[i]=s.value),n(1,{value:r,trace:{subs:e({},i,s.trace)}})}else 2===t&&a&&n(2,a)}));return s}function k(t,e){if(2===e){var n=this.sinks.indexOf(t);n>=0&&this.sinks.splice(n,1),0===this.sinks.length&&(f(this.sinks,this.talkback),this.talkback=void 0)}}function p(t,e){var n=this;if(0===t){var i=e;this.sinks.push(i),i(0,k.bind(this,i)),1===this.sinks.length&&this.downstream(0,(function(t,e){if(0===t)n.talkback=e;else if(1===t){var i=o(e);i.value!==n.value&&(n.value=i.value),r(1,i,n.sinks)}else 2===t&&(f(n.sinks,n.talkback,e),n.talkback=void 0)}))}}function g(t,e,n){0===e?(t(0,n),t(1,this.value)):1===e?t(1,n.value):2===e&&t(2,n)}function m(t,e,n){if(0===e){var i=n;t(0,g.bind(this,i))}else if(1===e)this.upstream(1,{value:n,trace:{from:this.value,to:n}});else if(2===e)this.upstream(2,n),f(this.sinks,this.talkback,n),this.talkback=void 0;else{if(100===e)return this.value;if(101===e)return t;if(102===e)return this.upstream}}function y(t,e,n){var i={sinks:[],value:t,talkback:void 0,downstream:e,upstream:n},a=p.bind(i),r=m.bind(i,a);return r.get=c,r.set=l,r.clear=v,r.downstream=h,r.upstream=d,r.sub=b,r}function w(t){var e,n=(e=void 0,function(t,n){0===t?e||(e=n)(0,(function(t){2===t&&(e=void 0)})):e&&(e(t,n),2===t&&(e=void 0))});return y(t,n,n)}return t._Data=1,t._End=2,t._Start=0,t.broadcast=r,t.change=function(t,e){var n=u(t,e);if(n)return{value:e,trace:n}},t.default=w,t.isLeaf=n,t.isState=function(t){return t&&"function"==typeof t&&2===t.length&&t.get&&"function"==typeof t.get&&0===t.get.length&&t.set&&"function"==typeof t.set&&1===t.set.length&&t.clear&&"function"==typeof t.clear&&0===t.clear.length&&t.downstream&&"function"==typeof t.downstream&&0===t.downstream.length&&t.upstream&&"function"==typeof t.upstream&&0===t.upstream.length&&t.sub&&"function"==typeof t.sub&&1===t.sub.length},t.makeState=y,t.postTrace=o,t.state=w,t.trace=u,Object.defineProperty(t,"__esModule",{value:!0}),t}({}); |
@@ -1,1 +0,1 @@ | ||
var callbagState=function(t){"use strict";function e(t){return!t||!t.subs}function n(t,e,n){[...n].forEach((o=>{n.indexOf(o)>=0&&o(t,e)}))}function o(t){return null==t||"number"==typeof t||"string"==typeof t||"boolean"==typeof t||t instanceof Date}function r(t,e){if(t!==e){if(!o(t)&&!o(e)){const n={subs:{}},o=new Set;let a=!1;for(const u in t){o.add(u);const c=r(t[u],e[u]);c&&(n.subs[u]=c,a=!0)}for(const u in e)o.has(u)||(n.subs[u]=r(t[u],e[u]),a=!0);return a?n:void 0}return t instanceof Date&&e instanceof Date&&+t!=+e||t!==e?{from:t,to:e}:void 0}}function a(t){if(e(t.trace)&&t.trace){const e=r(t.trace.from,t.trace.to);if(e)return{value:t.value,trace:e}}return t}function u(t,o,r){const c=[];let s=t,i=void 0;const f=t=>{n(2,t,c),c.length=0,i&&(i(2,t),i=void 0)},l=(t,e)=>{if(0===t){const t=e;c.push(t),t(0,(e=>{if(2===e){const e=c.indexOf(t);e>=0&&c.splice(e,1),0===c.length&&f()}})),1===c.length&&o(0,((t,e)=>{if(0===t)i=e;else if(1===t){const t=a(e);t.value!==s&&(s=t.value),n(1,t,c)}else 2===t&&f(e)}))}},v=(t,e)=>{if(0===t){const t=e;l(0,((e,n)=>{0===e?(t(0,n),t(1,s)):1===e?t(1,n.value):2===e&&t(2,n)}))}else 1===t?r(1,{value:e,trace:{from:s,to:e}}):2===t&&(r(2,e),f(e))};return v.get=()=>s,v.set=t=>v(1,t),v.clear=()=>v(2),v.downstream=()=>l,v.upstream=()=>r,v.sub=t=>{const n=u(s?s[t]:void 0,(o=l,a=t,c=()=>n.get(),(t,n)=>{0===t&&o(0,((t,o)=>{var r;if(1===t){const t=o,u=t.value?t.value[a]:void 0;(e(t.trace)&&u!==c()||!e(t.trace)&&a in(null===(r=t.trace)||void 0===r?void 0:r.subs))&&n(1,{value:u,trace:e(t.trace)?void 0:t.trace.subs[a]})}else n(t,o)}))}),function(t,e,n){return(o,r)=>{if(1===o){const o=r;n&&(n[e]=o.value),t(1,{value:n,trace:{subs:{[e]:o.trace}}})}else 2===o&&r&&t(2,r)}}(r,t,s));var o,a,c;return n},v}function c(t){const e=function(){let t=void 0;return(e,n)=>{0===e?t||(t=n,t(0,(e=>{2===e&&(t=void 0)}))):t&&(t(e,n),2===e&&(t=void 0))}}();return u(t,e,e)}return t._Data=1,t._End=2,t._Start=0,t.broadcast=n,t.change=function(t,e){const n=r(t,e);if(n)return{value:e,trace:n}},t.default=c,t.isLeaf=e,t.isState=function(t){return t&&"function"==typeof t&&2===t.length&&t.get&&"function"==typeof t.get&&0===t.get.length&&t.set&&"function"==typeof t.set&&1===t.set.length&&t.clear&&"function"==typeof t.clear&&0===t.clear.length&&t.downstream&&"function"==typeof t.downstream&&0===t.downstream.length&&t.upstream&&"function"==typeof t.upstream&&0===t.upstream.length&&t.sub&&"function"==typeof t.sub&&1===t.sub.length},t.makeState=u,t.postTrace=a,t.state=c,t.trace=r,Object.defineProperty(t,"__esModule",{value:!0}),t}({}); | ||
var callbagState=function(t){"use strict";function e(t){return!t||!t.subs}function n(t,n,s,i,a){var u;if(1===i){const i=a,o=i.value?i.value[t]:void 0;(e(i.trace)&&o!==n()||!e(i.trace)&&t in(null===(u=i.trace)||void 0===u?void 0:u.subs))&&s(1,{value:o,trace:e(i.trace)?void 0:i.trace.subs[t]})}else s(i,a)}function s(t,e,s){return(i,a)=>{0===i&&t(0,n.bind(null,e,s,a))}}function i(t,e,n){const s=n.slice(0);for(let i=0;i<s.length;i++){const a=s[i];-1!==n.indexOf(a)&&a(t,e)}}function a(t){return null==t||"number"==typeof t||"string"==typeof t||"boolean"==typeof t||t instanceof Date}function u(t,e){if(t!==e){if(!a(t)&&!a(e)){const n={subs:{}},s=new Set;let i=!1;for(const a in t){s.add(a);const o=u(t[a],e[a]);o&&(n.subs[a]=o,i=!0)}for(const a in e)s.has(a)||(n.subs[a]=u(t[a],e[a]),i=!0);return i?n:void 0}return t instanceof Date&&e instanceof Date&&+t!=+e||t!==e?{from:t,to:e}:void 0}}function o(t){if(e(t.trace)&&t.trace){const e=u(t.trace.from,t.trace.to);if(e)return{value:t.value,trace:e}}return t}function r(t,e,n){i(2,n,t),t.length=0,e&&e(2,n)}function c(){return this(100)}function f(t){this(1,t)}function l(){this(2)}function h(){return this(101)}function v(){return this(102)}function d(t){const e=m(this.get()?this.get()[t]:void 0,s(this.downstream(),t,(()=>e.get())),(n=this.upstream(),i=t,a=this.get(),(t,e)=>{if(1===t){const t=e;a&&(a[i]=t.value),n(1,{value:a,trace:{subs:{[i]:t.trace}}})}else 2===t&&e&&n(2,e)}));var n,i,a;return e}function b(t,e){if(2===e){const e=this.sinks.indexOf(t);e>=0&&this.sinks.splice(e,1),0===this.sinks.length&&(r(this.sinks,this.talkback),this.talkback=void 0)}}function k(t,e){if(0===t){const t=e;this.sinks.push(t),t(0,b.bind(this,t)),1===this.sinks.length&&this.downstream(0,((t,e)=>{if(0===t)this.talkback=e;else if(1===t){const t=o(e);t.value!==this.value&&(this.value=t.value),i(1,t,this.sinks)}else 2===t&&(r(this.sinks,this.talkback,e),this.talkback=void 0)}))}}function p(t,e,n){0===e?(t(0,n),t(1,this.value)):1===e?t(1,n.value):2===e&&t(2,n)}function g(t,e,n){if(0===e){const e=n;t(0,p.bind(this,e))}else if(1===e)this.upstream(1,{value:n,trace:{from:this.value,to:n}});else if(2===e)this.upstream(2,n),r(this.sinks,this.talkback,n),this.talkback=void 0;else{if(100===e)return this.value;if(101===e)return t;if(102===e)return this.upstream}}function m(t,e,n){const s={sinks:[],value:t,talkback:void 0,downstream:e,upstream:n},i=k.bind(s),a=g.bind(s,i);return a.get=c,a.set=f,a.clear=l,a.downstream=h,a.upstream=v,a.sub=d,a}function y(t){const e=function(){let t=void 0;return(e,n)=>{0===e?t||(t=n,t(0,(e=>{2===e&&(t=void 0)}))):t&&(t(e,n),2===e&&(t=void 0))}}();return m(t,e,e)}return t._Data=1,t._End=2,t._Start=0,t.broadcast=i,t.change=function(t,e){const n=u(t,e);if(n)return{value:e,trace:n}},t.default=y,t.isLeaf=e,t.isState=function(t){return t&&"function"==typeof t&&2===t.length&&t.get&&"function"==typeof t.get&&0===t.get.length&&t.set&&"function"==typeof t.set&&1===t.set.length&&t.clear&&"function"==typeof t.clear&&0===t.clear.length&&t.downstream&&"function"==typeof t.downstream&&0===t.downstream.length&&t.upstream&&"function"==typeof t.upstream&&0===t.upstream.length&&t.sub&&"function"==typeof t.sub&&1===t.sub.length},t.makeState=m,t.postTrace=o,t.state=y,t.trace=u,Object.defineProperty(t,"__esModule",{value:!0}),t}({}); |
@@ -8,84 +8,112 @@ "use strict"; | ||
var trace_1 = require("./trace"); | ||
function makeState(initial, downstream, upstream) { | ||
var sinks = []; | ||
var value = initial; | ||
var talkback = undefined; | ||
var terminate = function (err) { | ||
broadcast_1.broadcast(types_1._End, err, sinks); | ||
sinks.length = 0; | ||
if (talkback) { | ||
talkback(types_1._End, err); | ||
talkback = undefined; | ||
function terminate(sinks, talkback, err) { | ||
broadcast_1.broadcast(types_1._End, err, sinks); | ||
sinks.length = 0; | ||
if (talkback) { | ||
talkback(types_1._End, err); | ||
} | ||
} | ||
function get() { return this(types_1._Latest); } | ||
function set(t) { this(types_1._Data, t); } | ||
function clear() { this(types_1._End); } | ||
function getDownstream() { return this(types_1._Downstream); } | ||
function getUpstream() { return this(types_1._Upstream); } | ||
function sub(k) { | ||
var _sub = makeState(this.get() ? this.get()[k] : undefined, substream_1.subDownstream(this.downstream(), k, function () { return _sub.get(); }), substream_1.subUpstream(this.upstream(), k, this.get())); | ||
return _sub; | ||
} | ||
function _dsgreeter(sink, type) { | ||
if (type === types_1._End) { | ||
var index = this.sinks.indexOf(sink); | ||
if (index >= 0) { | ||
this.sinks.splice(index, 1); | ||
} | ||
}; | ||
var _downstream = function (type, m) { | ||
if (type === types_1._Start) { | ||
var sink_1 = m; | ||
sinks.push(sink_1); | ||
sink_1(types_1._Start, function (t) { | ||
if (t === types_1._End) { | ||
var index = sinks.indexOf(sink_1); | ||
if (index >= 0) { | ||
sinks.splice(index, 1); | ||
} | ||
if (sinks.length === 0) { | ||
terminate(); | ||
} | ||
} | ||
}); | ||
if (sinks.length === 1) { | ||
downstream(types_1._Start, function (t, _m) { | ||
if (t === types_1._Start) { | ||
talkback = _m; | ||
} | ||
else if (t === types_1._Data) { | ||
var change = trace_1.postTrace(_m); | ||
if (change.value !== value) { | ||
value = change.value; | ||
} | ||
broadcast_1.broadcast(types_1._Data, change, sinks); | ||
} | ||
else if (t === types_1._End) { | ||
terminate(_m); | ||
} | ||
}); | ||
} | ||
if (this.sinks.length === 0) { | ||
terminate(this.sinks, this.talkback); | ||
this.talkback = undefined; | ||
} | ||
}; | ||
var _state = function (type, m) { | ||
if (type === types_1._Start) { | ||
var sink_2 = m; | ||
_downstream(types_1._Start, function (t, _m) { | ||
} | ||
} | ||
function _downstream(type, m) { | ||
var _this = this; | ||
if (type === types_1._Start) { | ||
var sink = m; | ||
this.sinks.push(sink); | ||
// TODO: we can perhaps keep track of sub sinks (sinks listening for a particular sub) | ||
// more efficiently, so changes are broadcast to them properly by accessing them via | ||
// a hash table. this helps performance of cases with large fan-out (like big arrays). | ||
sink(types_1._Start, _dsgreeter.bind(this, sink)); | ||
if (this.sinks.length === 1) { | ||
this.downstream(types_1._Start, function (t, _m) { | ||
if (t === types_1._Start) { | ||
sink_2(types_1._Start, _m); | ||
sink_2(types_1._Data, value); | ||
_this.talkback = _m; | ||
} | ||
else if (t === types_1._Data) { | ||
sink_2(types_1._Data, _m.value); | ||
var change = trace_1.postTrace(_m); | ||
if (change.value !== _this.value) { | ||
_this.value = change.value; | ||
} | ||
broadcast_1.broadcast(types_1._Data, change, _this.sinks); | ||
} | ||
else if (t === types_1._End) { | ||
sink_2(types_1._End, _m); | ||
terminate(_this.sinks, _this.talkback, _m); | ||
_this.talkback = undefined; | ||
} | ||
}); | ||
} | ||
else if (type === types_1._Data) { | ||
upstream(types_1._Data, { value: m, trace: { from: value, to: m } }); | ||
} | ||
else if (type === types_1._End) { | ||
upstream(types_1._End, m); | ||
terminate(m); | ||
} | ||
} | ||
} | ||
function _sgreeter(sink, t, m) { | ||
if (t === types_1._Start) { | ||
sink(types_1._Start, m); | ||
sink(types_1._Data, this.value); | ||
} | ||
else if (t === types_1._Data) { | ||
sink(types_1._Data, m.value); | ||
} | ||
else if (t === types_1._End) { | ||
sink(types_1._End, m); | ||
} | ||
} | ||
function _state(_d, type, m) { | ||
if (type === types_1._Start) { | ||
var sink = m; | ||
_d(types_1._Start, _sgreeter.bind(this, sink)); | ||
} | ||
else if (type === types_1._Data) { | ||
this.upstream(types_1._Data, { value: m, trace: { from: this.value, to: m } }); | ||
} | ||
else if (type === types_1._End) { | ||
this.upstream(types_1._End, m); | ||
terminate(this.sinks, this.talkback, m); | ||
this.talkback = undefined; | ||
} | ||
else if (type === types_1._Latest) { | ||
return this.value; | ||
} | ||
else if (type === types_1._Downstream) { | ||
return _d; | ||
} | ||
else if (type === types_1._Upstream) { | ||
return this.upstream; | ||
} | ||
} | ||
function makeState(initial, downstream, upstream) { | ||
var profile = { | ||
sinks: [], | ||
value: initial, | ||
talkback: undefined, | ||
downstream: downstream, upstream: upstream | ||
}; | ||
_state.get = function () { return value; }; | ||
_state.set = function (v) { return _state(types_1._Data, v); }; | ||
_state.clear = function () { return _state(types_1._End); }; | ||
_state.downstream = function () { return _downstream; }; | ||
_state.upstream = function () { return upstream; }; | ||
_state.sub = function (k) { | ||
var _sub = makeState(value ? value[k] : undefined, substream_1.subDownstream(_downstream, k, function () { return _sub.get(); }), substream_1.subUpstream(upstream, k, value)); | ||
return _sub; | ||
}; | ||
return _state; | ||
var _d = _downstream.bind(profile); | ||
var _s = _state.bind(profile, _d); | ||
_s.get = get; | ||
_s.set = set; | ||
_s.clear = clear; | ||
_s.downstream = getDownstream; | ||
_s.upstream = getUpstream; | ||
_s.sub = sub; | ||
return _s; | ||
} | ||
exports.makeState = makeState; | ||
//# sourceMappingURL=state.js.map |
@@ -5,2 +5,19 @@ "use strict"; | ||
var types_1 = require("./types"); | ||
function _subDownSinkWrap(key, current, sink, t, m) { | ||
var _a; | ||
if (t === types_1._Data) { | ||
var change = m; | ||
var value = change.value ? change.value[key] : undefined; | ||
if ((types_1.isLeaf(change.trace) && value !== current()) || | ||
(!types_1.isLeaf(change.trace) && key in ((_a = change.trace) === null || _a === void 0 ? void 0 : _a.subs))) { | ||
sink(types_1._Data, { | ||
value: value, | ||
trace: types_1.isLeaf(change.trace) ? undefined : change.trace.subs[key] | ||
}); | ||
} | ||
} | ||
else { | ||
sink(t, m); | ||
} | ||
} | ||
function subDownstream(src, key, current) { | ||
@@ -11,19 +28,3 @@ return (function (start, sink) { | ||
} | ||
src(types_1._Start, function (t, m) { | ||
var _a; | ||
if (t === types_1._Data) { | ||
var change = m; | ||
var value = change.value ? change.value[key] : undefined; | ||
if ((types_1.isLeaf(change.trace) && value !== current()) || | ||
(!types_1.isLeaf(change.trace) && key in ((_a = change.trace) === null || _a === void 0 ? void 0 : _a.subs))) { | ||
sink(types_1._Data, { | ||
value: value, | ||
trace: types_1.isLeaf(change.trace) ? undefined : change.trace.subs[key] | ||
}); | ||
} | ||
} | ||
else { | ||
sink(t, m); | ||
} | ||
}); | ||
src(types_1._Start, _subDownSinkWrap.bind(null, key, current, sink)); | ||
}); | ||
@@ -30,0 +31,0 @@ } |
@@ -22,2 +22,3 @@ import { Source, Sink, START, DATA, END, Callbag } from 'callbag'; | ||
export declare type MsgType = START | DATA | END; | ||
export declare type StateMsgType = MsgType | typeof _Latest | typeof _Downstream | typeof _Upstream; | ||
export declare type SubState<T, K extends keyof T> = Callbag<T[K], T[K] | undefined> & { | ||
@@ -43,1 +44,4 @@ get(): T[K] | undefined; | ||
export declare const _End = 2; | ||
export declare const _Latest = 100; | ||
export declare const _Downstream = 101; | ||
export declare const _Upstream = 102; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._End = exports._Data = exports._Start = exports.isState = exports.isLeaf = void 0; | ||
exports._Upstream = exports._Downstream = exports._Latest = exports._End = exports._Data = exports._Start = exports.isState = exports.isLeaf = void 0; | ||
function isLeaf(c) { | ||
@@ -21,2 +21,5 @@ return !c || !c.subs; | ||
exports._End = 2; | ||
exports._Latest = 100; | ||
exports._Downstream = 101; | ||
exports._Upstream = 102; | ||
//# sourceMappingURL=types.js.map |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.broadcast = void 0; | ||
function broadcast(type, msg, sinks) { | ||
var _sinks = __spreadArrays(sinks); | ||
_sinks.forEach(function (sink) { | ||
if (sinks.indexOf(sink) >= 0) { | ||
var _sinks = sinks.slice(0); | ||
for (var i = 0; i < _sinks.length; i++) { | ||
var sink = _sinks[i]; | ||
if (sinks.indexOf(sink) !== -1) { | ||
sink(type, msg); | ||
} | ||
}); | ||
} | ||
} | ||
exports.broadcast = broadcast; | ||
//# sourceMappingURL=broadcast.js.map |
import { subDownstream, subUpstream } from './substream'; | ||
import { _Start, _Data, _End } from './types'; | ||
import { _Start, _Data, _End, _Latest, _Downstream, _Upstream } from './types'; | ||
import { broadcast } from './util/broadcast'; | ||
import { postTrace } from './trace'; | ||
export function makeState(initial, downstream, upstream) { | ||
const sinks = []; | ||
let value = initial; | ||
let talkback = undefined; | ||
const terminate = (err) => { | ||
broadcast(_End, err, sinks); | ||
sinks.length = 0; | ||
if (talkback) { | ||
talkback(_End, err); | ||
talkback = undefined; | ||
function terminate(sinks, talkback, err) { | ||
broadcast(_End, err, sinks); | ||
sinks.length = 0; | ||
if (talkback) { | ||
talkback(_End, err); | ||
} | ||
} | ||
function get() { return this(_Latest); } | ||
function set(t) { this(_Data, t); } | ||
function clear() { this(_End); } | ||
function getDownstream() { return this(_Downstream); } | ||
function getUpstream() { return this(_Upstream); } | ||
function sub(k) { | ||
const _sub = makeState(this.get() ? this.get()[k] : undefined, subDownstream(this.downstream(), k, () => _sub.get()), subUpstream(this.upstream(), k, this.get())); | ||
return _sub; | ||
} | ||
function _dsgreeter(sink, type) { | ||
if (type === _End) { | ||
const index = this.sinks.indexOf(sink); | ||
if (index >= 0) { | ||
this.sinks.splice(index, 1); | ||
} | ||
}; | ||
const _downstream = (type, m) => { | ||
if (type === _Start) { | ||
const sink = m; | ||
sinks.push(sink); | ||
sink(_Start, (t) => { | ||
if (t === _End) { | ||
const index = sinks.indexOf(sink); | ||
if (index >= 0) { | ||
sinks.splice(index, 1); | ||
} | ||
if (sinks.length === 0) { | ||
terminate(); | ||
} | ||
} | ||
}); | ||
if (sinks.length === 1) { | ||
downstream(_Start, (t, _m) => { | ||
if (t === _Start) { | ||
talkback = _m; | ||
} | ||
else if (t === _Data) { | ||
const change = postTrace(_m); | ||
if (change.value !== value) { | ||
value = change.value; | ||
} | ||
broadcast(_Data, change, sinks); | ||
} | ||
else if (t === _End) { | ||
terminate(_m); | ||
} | ||
}); | ||
} | ||
if (this.sinks.length === 0) { | ||
terminate(this.sinks, this.talkback); | ||
this.talkback = undefined; | ||
} | ||
}; | ||
const _state = (type, m) => { | ||
if (type === _Start) { | ||
const sink = m; | ||
_downstream(_Start, (t, _m) => { | ||
} | ||
} | ||
function _downstream(type, m) { | ||
if (type === _Start) { | ||
const sink = m; | ||
this.sinks.push(sink); | ||
// TODO: we can perhaps keep track of sub sinks (sinks listening for a particular sub) | ||
// more efficiently, so changes are broadcast to them properly by accessing them via | ||
// a hash table. this helps performance of cases with large fan-out (like big arrays). | ||
sink(_Start, _dsgreeter.bind(this, sink)); | ||
if (this.sinks.length === 1) { | ||
this.downstream(_Start, (t, _m) => { | ||
if (t === _Start) { | ||
sink(_Start, _m); | ||
sink(_Data, value); | ||
this.talkback = _m; | ||
} | ||
else if (t === _Data) { | ||
sink(_Data, _m.value); | ||
const change = postTrace(_m); | ||
if (change.value !== this.value) { | ||
this.value = change.value; | ||
} | ||
broadcast(_Data, change, this.sinks); | ||
} | ||
else if (t === _End) { | ||
sink(_End, _m); | ||
terminate(this.sinks, this.talkback, _m); | ||
this.talkback = undefined; | ||
} | ||
}); | ||
} | ||
else if (type === _Data) { | ||
upstream(_Data, { value: m, trace: { from: value, to: m } }); | ||
} | ||
else if (type === _End) { | ||
upstream(_End, m); | ||
terminate(m); | ||
} | ||
} | ||
} | ||
function _sgreeter(sink, t, m) { | ||
if (t === _Start) { | ||
sink(_Start, m); | ||
sink(_Data, this.value); | ||
} | ||
else if (t === _Data) { | ||
sink(_Data, m.value); | ||
} | ||
else if (t === _End) { | ||
sink(_End, m); | ||
} | ||
} | ||
function _state(_d, type, m) { | ||
if (type === _Start) { | ||
const sink = m; | ||
_d(_Start, _sgreeter.bind(this, sink)); | ||
} | ||
else if (type === _Data) { | ||
this.upstream(_Data, { value: m, trace: { from: this.value, to: m } }); | ||
} | ||
else if (type === _End) { | ||
this.upstream(_End, m); | ||
terminate(this.sinks, this.talkback, m); | ||
this.talkback = undefined; | ||
} | ||
else if (type === _Latest) { | ||
return this.value; | ||
} | ||
else if (type === _Downstream) { | ||
return _d; | ||
} | ||
else if (type === _Upstream) { | ||
return this.upstream; | ||
} | ||
} | ||
export function makeState(initial, downstream, upstream) { | ||
const profile = { | ||
sinks: [], | ||
value: initial, | ||
talkback: undefined, | ||
downstream, upstream | ||
}; | ||
_state.get = () => value; | ||
_state.set = (v) => _state(_Data, v); | ||
_state.clear = () => _state(_End); | ||
_state.downstream = () => _downstream; | ||
_state.upstream = () => upstream; | ||
_state.sub = (k) => { | ||
const _sub = makeState(value ? value[k] : undefined, subDownstream(_downstream, k, () => _sub.get()), subUpstream(upstream, k, value)); | ||
return _sub; | ||
}; | ||
return _state; | ||
const _d = _downstream.bind(profile); | ||
const _s = _state.bind(profile, _d); | ||
_s.get = get; | ||
_s.set = set; | ||
_s.clear = clear; | ||
_s.downstream = getDownstream; | ||
_s.upstream = getUpstream; | ||
_s.sub = sub; | ||
return _s; | ||
} | ||
//# sourceMappingURL=state.js.map |
import { isLeaf, _Data, _End, _Start } from './types'; | ||
function _subDownSinkWrap(key, current, sink, t, m) { | ||
var _a; | ||
if (t === _Data) { | ||
const change = m; | ||
const value = change.value ? change.value[key] : undefined; | ||
if ((isLeaf(change.trace) && value !== current()) || | ||
(!isLeaf(change.trace) && key in ((_a = change.trace) === null || _a === void 0 ? void 0 : _a.subs))) { | ||
sink(_Data, { | ||
value, | ||
trace: isLeaf(change.trace) ? undefined : change.trace.subs[key] | ||
}); | ||
} | ||
} | ||
else { | ||
sink(t, m); | ||
} | ||
} | ||
export function subDownstream(src, key, current) { | ||
@@ -7,19 +24,3 @@ return ((start, sink) => { | ||
} | ||
src(_Start, (t, m) => { | ||
var _a; | ||
if (t === _Data) { | ||
const change = m; | ||
const value = change.value ? change.value[key] : undefined; | ||
if ((isLeaf(change.trace) && value !== current()) || | ||
(!isLeaf(change.trace) && key in ((_a = change.trace) === null || _a === void 0 ? void 0 : _a.subs))) { | ||
sink(_Data, { | ||
value, | ||
trace: isLeaf(change.trace) ? undefined : change.trace.subs[key] | ||
}); | ||
} | ||
} | ||
else { | ||
sink(t, m); | ||
} | ||
}); | ||
src(_Start, _subDownSinkWrap.bind(null, key, current, sink)); | ||
}); | ||
@@ -26,0 +27,0 @@ } |
@@ -22,2 +22,3 @@ import { Source, Sink, START, DATA, END, Callbag } from 'callbag'; | ||
export declare type MsgType = START | DATA | END; | ||
export declare type StateMsgType = MsgType | typeof _Latest | typeof _Downstream | typeof _Upstream; | ||
export declare type SubState<T, K extends keyof T> = Callbag<T[K], T[K] | undefined> & { | ||
@@ -43,1 +44,4 @@ get(): T[K] | undefined; | ||
export declare const _End = 2; | ||
export declare const _Latest = 100; | ||
export declare const _Downstream = 101; | ||
export declare const _Upstream = 102; |
@@ -16,2 +16,5 @@ export function isLeaf(c) { | ||
export const _End = 2; | ||
export const _Latest = 100; | ||
export const _Downstream = 101; | ||
export const _Upstream = 102; | ||
//# sourceMappingURL=types.js.map |
export function broadcast(type, msg, sinks) { | ||
const _sinks = [...sinks]; | ||
_sinks.forEach(sink => { | ||
if (sinks.indexOf(sink) >= 0) { | ||
const _sinks = sinks.slice(0); | ||
for (let i = 0; i < _sinks.length; i++) { | ||
const sink = _sinks[i]; | ||
if (sinks.indexOf(sink) !== -1) { | ||
sink(type, msg); | ||
} | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=broadcast.js.map |
{ | ||
"name": "callbag-state", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"description": "Callbag-based State Management", | ||
@@ -10,2 +10,4 @@ "main": "dist/es5/index.js", | ||
"start": "ts-node samples", | ||
"inspect": "node -r ts-node/register --inspect samples", | ||
"web": "webpack serve --config ./conf/webpack/dev.conf.ts", | ||
"test": "ts-node --project conf/typescript/test.json test.ts", | ||
@@ -51,2 +53,4 @@ "build": "tsc -p conf/typescript/es5.json && tsc -p conf/typescript/es6.json", | ||
"@types/node": "^14.11.8", | ||
"@types/webpack": "^4.41.24", | ||
"@types/estree": "0.0.45", | ||
"callbag-behavior-subject": "^1.2.0", | ||
@@ -61,2 +65,3 @@ "callbag-interval": "^1.1.0", | ||
"chai": "^4.2.0", | ||
"html-webpack-plugin": "^4.5.0", | ||
"lodash": "^4.17.20", | ||
@@ -71,3 +76,7 @@ "mocha": "^8.1.3", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.0.3" | ||
"typescript": "^4.0.3", | ||
"webpack": "^5.3.2", | ||
"webpack-cli": "^4.1.0", | ||
"webpack-dev-server": "^3.11.0", | ||
"webpack-merge": "^5.3.0" | ||
}, | ||
@@ -74,0 +83,0 @@ "nyc": { |
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
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
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
74012
772
35