@mcastiello/simple-store
Version:
A simple state manager that can store data via a reducer function.
2 lines (1 loc) • 906 B
JavaScript
var e=require("@mcastiello/cancellable-promise");exports.createStore=function(r,n){var t=n,u=new Map,o=new Map;return{subscribe:function(r,n){void 0===n&&(n=[]);var o=crypto.randomUUID(),a=e.CancellablePromise.defer(r);return u.set(o,[a,n]),t&&0===n.length&&r(t),function(){u.delete(o)}},intercept:function(e,r){var n=crypto.randomUUID();return o.set(n,[e,r]),function(){o.delete(n)}},dispatch:function(){var e=[].slice.call(arguments),n=e[0],a={type:n,payload:e[1]},c=r(t,a),i=Array.from(u.values()).filter(function(e){var r=e[1];return 0===(null==r?void 0:r.length)||(null==r?void 0:r.includes(n))}).map(function(e){return e[0]}),l=Array.from(o.values()).filter(function(e){return e[1].includes(n)}).map(function(e){return e[0]}).reduce(function(e,r){return r(t,e,a)},c);t!==l&&(i.forEach(function(e){return e(l,a)}),t=l)},state:new Proxy({},{get:function(e,r){var n;return null==(n=t)?void 0:n[r]}})}};