@coderkearns/reactivity
Advanced tools
+107
| export function subscription() { | ||
| const subscribers = [] | ||
| return { | ||
| $SUBSCRIPTION: true, | ||
| $subscribe(subscriber) { | ||
| subscribers.push(subscriber) | ||
| }, | ||
| $publish(...data) { | ||
| subscribers.forEach(subscriber => subscriber(...data)) | ||
| }, | ||
| } | ||
| } | ||
| export function makeSubscription(value) { | ||
| if (typeof value === "object") { | ||
| if (Array.isArray(value)) { | ||
| return group(value) | ||
| } else { | ||
| return store(value) | ||
| } | ||
| } | ||
| return reactive(value) | ||
| } | ||
| export function reactive(value) { | ||
| const { $subscribe, $publish } = subscription() | ||
| return { | ||
| $SUBSCRIPTION: true, | ||
| $subscribe, | ||
| $static() { | ||
| return value | ||
| }, | ||
| $get() { | ||
| return value | ||
| }, | ||
| $set(newValue) { | ||
| value = newValue | ||
| $publish(newValue) | ||
| } | ||
| } | ||
| } | ||
| export function group(initial = []) { | ||
| const { $subscribe, $publish } = subscription() | ||
| const _group = [] | ||
| for (let i = 0; i < initial.length; i++) { | ||
| const item = makeSubscription(initial[i]) | ||
| _group.push(item) | ||
| item.$subscribe((newValue, keyParts) => { | ||
| $publish(newValue, keyParts ? `${i}.${keyParts}` : `${i}`) | ||
| }) | ||
| } | ||
| _group.$SUBSCRIPTION = true | ||
| _group.$subscribe = $subscribe | ||
| _group.$static = function () { | ||
| return _group.map(item => item.$static()) | ||
| } | ||
| _group.$mutate = function (fn) { | ||
| fn(_group) | ||
| for (let i = 0; i < _group.length; i++) { | ||
| if (!_group[i].$SUBSCRIPTION) { | ||
| _group[i] = makeSubscription(_group[i]) | ||
| } | ||
| } | ||
| $publish(_group.$static()) | ||
| } | ||
| return _group | ||
| } | ||
| export function store(initial = {}) { | ||
| const { $subscribe, $publish } = subscription() | ||
| const _store = {} | ||
| for (const key in initial) { | ||
| _store[key] = makeSubscription(initial[key]) | ||
| _store[key].$subscribe((newValue, keyParts) => { | ||
| $publish(newValue, keyParts ? `${key}.${keyParts}` : key) | ||
| }) | ||
| } | ||
| _store.$SUBSCRIPTION = true | ||
| _store.$subscribe = $subscribe | ||
| _store.$static = function () { | ||
| const _static = {} | ||
| for (const key in _store) { | ||
| if (!key.startsWith("$")) { | ||
| _static[key] = _store[key].$static() | ||
| } | ||
| } | ||
| return _static | ||
| } | ||
| _store.$set = function (key, value) { | ||
| _store[key] = makeSubscription(value) | ||
| _store[key].$subscribe((newValue, keyParts) => { | ||
| $publish(newValue, keyParts ? `${key}.${keyParts}` : key) | ||
| }) | ||
| $publish(_store[key].$static(), key) | ||
| } | ||
| return _store | ||
| } |
+1
-183
@@ -1,183 +0,1 @@ | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // lib/index.js | ||
| var lib_exports = {}; | ||
| __export(lib_exports, { | ||
| ReactiveArray: () => ReactiveArray, | ||
| ReactiveObject: () => ReactiveObject, | ||
| ReactiveValue: () => ReactiveValue, | ||
| Subscription: () => Subscription, | ||
| a: () => factory2, | ||
| o: () => factory3, | ||
| s: () => factory, | ||
| v: () => factory4 | ||
| }); | ||
| module.exports = __toCommonJS(lib_exports); | ||
| // lib/subscription.js | ||
| var Subscription = class { | ||
| constructor() { | ||
| this._subscribers = []; | ||
| } | ||
| subscribe(subscriber) { | ||
| this._subscribers.push(subscriber); | ||
| } | ||
| publish(...values) { | ||
| for (let subscriber of this._subscribers) { | ||
| subscriber(...values); | ||
| } | ||
| } | ||
| }; | ||
| function factory() { | ||
| return new Subscriber(); | ||
| } | ||
| // lib/array.js | ||
| var ReactiveArray = class extends Subscription { | ||
| constructor(initialArray = []) { | ||
| super(); | ||
| this._internal = []; | ||
| for (const value of initialArray) { | ||
| this.push(value); | ||
| } | ||
| } | ||
| _makeSubscriberFunc(key) { | ||
| return (value, oldKey = null) => { | ||
| this.publish(value, oldKey ? `${key}.${oldKey}` : `${key}`); | ||
| }; | ||
| } | ||
| _makeAndSubscribe(value, key) { | ||
| const item = makeSubscription(value); | ||
| item.subscribe(this._makeSubscriberFunc(key)); | ||
| return item; | ||
| } | ||
| static() { | ||
| return this._internal.map((item) => item.static()); | ||
| } | ||
| set(newArr) { | ||
| this._internal.length = 0; | ||
| for (const value of newArr) { | ||
| this.publish(value); | ||
| } | ||
| } | ||
| /* Array methods */ | ||
| push(value) { | ||
| const key = this._internal.length; | ||
| const item = this._makeAndSubscribe(value, key); | ||
| this._internal.push(item); | ||
| this.publish(value, `${key}`); | ||
| } | ||
| }; | ||
| function factory2(initialArray) { | ||
| return new ReactiveArray(initialArray); | ||
| } | ||
| // lib/object.js | ||
| var ReactiveObject = class extends Subscription { | ||
| constructor(initialObject = {}) { | ||
| super(); | ||
| this._internal = {}; | ||
| for (const key in initialObject) { | ||
| this.set(key, initialObject[key]); | ||
| } | ||
| } | ||
| _makeSubscriberFunc(key) { | ||
| return (value, oldKey = null) => { | ||
| this.publish(value, oldKey ? `${key}.${oldKey}` : key); | ||
| }; | ||
| } | ||
| _makeAndSubscribe(value, key) { | ||
| const item = makeSubscription(value); | ||
| item.subscribe(this._makeSubscriberFunc(key)); | ||
| return item; | ||
| } | ||
| static() { | ||
| const _static = {}; | ||
| for (const key in this._internal) { | ||
| _static[key] = this._internal[key].static(); | ||
| } | ||
| } | ||
| /* Object methods */ | ||
| set(key, value) { | ||
| if (value === void 0) { | ||
| for (const key2 in this._internal) | ||
| delete this._internal[key2]; | ||
| for (const _key in key) | ||
| this.set(_key, key[_key]); | ||
| } else { | ||
| if (this._internal[key] !== void 0) { | ||
| this._internal[key].set(value); | ||
| } else { | ||
| const item = this._makeAndSubscribe(value, key); | ||
| this._internal[key] = item; | ||
| this.publish(value, key); | ||
| } | ||
| } | ||
| } | ||
| key(key) { | ||
| return this._internal[key]; | ||
| } | ||
| }; | ||
| function factory3(initialObject) { | ||
| return new ReactiveObject(initialObject); | ||
| } | ||
| // lib/shared.js | ||
| function makeSubscription(item) { | ||
| if (typeof item === "object") { | ||
| if (Array.isArray(item)) { | ||
| return new ReactiveArray(item); | ||
| } else { | ||
| return new ReactiveObject(item); | ||
| } | ||
| } | ||
| return new ReactiveValue(item); | ||
| } | ||
| // lib/value.js | ||
| var ReactiveValue = class extends Subscription { | ||
| constructor(initialValue) { | ||
| super(); | ||
| this._value = initialValue; | ||
| } | ||
| get() { | ||
| return this._value; | ||
| } | ||
| set(value) { | ||
| this._value = value; | ||
| this.publish(this._value); | ||
| } | ||
| static() { | ||
| return this._value; | ||
| } | ||
| }; | ||
| function factory4(initialValue) { | ||
| return new ReactiveValue(initialValue); | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| ReactiveArray, | ||
| ReactiveObject, | ||
| ReactiveValue, | ||
| Subscription, | ||
| a, | ||
| o, | ||
| s, | ||
| v | ||
| }); | ||
| var b=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var g=(s,c)=>{for(var i in c)b(s,i,{get:c[i],enumerable:!0})},m=(s,c,i,t)=>{if(c&&typeof c=="object"||typeof c=="function")for(let r of I(c))!x.call(s,r)&&r!==i&&b(s,r,{get:()=>c[r],enumerable:!(t=h(c,r))||t.enumerable});return s};var B=s=>m(b({},"__esModule",{value:!0}),s);var C={};g(C,{group:()=>p,makeSubscription:()=>$,reactive:()=>f,store:()=>S,subscription:()=>e});module.exports=B(C);function e(){let s=[];return{$SUBSCRIPTION:!0,$subscribe(c){s.push(c)},$publish(...c){s.forEach(i=>i(...c))}}}function $(s){return typeof s=="object"?Array.isArray(s)?p(s):S(s):f(s)}function f(s){let{$subscribe:c,$publish:i}=e();return{$SUBSCRIPTION:!0,$subscribe:c,$static(){return s},$get(){return s},$set(t){s=t,i(t)}}}function p(s=[]){let{$subscribe:c,$publish:i}=e(),t=[];for(let r=0;r<s.length;r++){let n=$(s[r]);t.push(n),n.$subscribe((o,u)=>{i(o,u?`${r}.${u}`:`${r}`)})}return t.$SUBSCRIPTION=!0,t.$subscribe=c,t.$static=function(){return t.map(r=>r.$static())},t.$mutate=function(r){r(t);for(let n=0;n<t.length;n++)t[n].$SUBSCRIPTION||(t[n]=$(t[n]));i(t.$static())},t}function S(s={}){let{$subscribe:c,$publish:i}=e(),t={};for(let r in s)t[r]=$(s[r]),t[r].$subscribe((n,o)=>{i(n,o?`${r}.${o}`:r)});return t.$SUBSCRIPTION=!0,t.$subscribe=c,t.$static=function(){let r={};for(let n in t)n.startsWith("$")||(r[n]=t[n].$static());return r},t.$set=function(r,n){t[r]=$(n),t[r].$subscribe((o,u)=>{i(o,u?`${r}.${u}`:r)}),i(t[r].$static(),r)},t}0&&(module.exports={group,makeSubscription,reactive,store,subscription}); |
+1
-1
@@ -1,2 +0,2 @@ | ||
| (()=>{var i=class{constructor(){this._subscribers=[]}subscribe(t){this._subscribers.push(t)}publish(...t){for(let r of this._subscribers)r(...t)}};function a(){return new Subscriber}var n=class extends i{constructor(t=[]){super(),this._internal=[];for(let r of t)this.push(r)}_makeSubscriberFunc(t){return(r,s=null)=>{this.publish(r,s?`${t}.${s}`:`${t}`)}}_makeAndSubscribe(t,r){let s=c(t);return s.subscribe(this._makeSubscriberFunc(r)),s}static(){return this._internal.map(t=>t.static())}set(t){this._internal.length=0;for(let r of t)this.publish(r)}push(t){let r=this._internal.length,s=this._makeAndSubscribe(t,r);this._internal.push(s),this.publish(t,`${r}`)}};function f(e){return new n(e)}var o=class extends i{constructor(t={}){super(),this._internal={};for(let r in t)this.set(r,t[r])}_makeSubscriberFunc(t){return(r,s=null)=>{this.publish(r,s?`${t}.${s}`:t)}}_makeAndSubscribe(t,r){let s=c(t);return s.subscribe(this._makeSubscriberFunc(r)),s}static(){let t={};for(let r in this._internal)t[r]=this._internal[r].static()}set(t,r){if(r===void 0){for(let s in this._internal)delete this._internal[s];for(let s in t)this.set(s,t[s])}else if(this._internal[t]!==void 0)this._internal[t].set(r);else{let s=this._makeAndSubscribe(r,t);this._internal[t]=s,this.publish(r,t)}}key(t){return this._internal[t]}};function h(e){return new o(e)}function c(e){return typeof e=="object"?Array.isArray(e)?new n(e):new o(e):new u(e)}var u=class extends i{constructor(t){super(),this._value=t}get(){return this._value}set(t){this._value=t,this.publish(this._value)}static(){return this._value}};function b(e){return new u(e)}})(); | ||
| (()=>{function e(){let s=[];return{$SUBSCRIPTION:!0,$subscribe(i){s.push(i)},$publish(...i){s.forEach(n=>n(...i))}}}function $(s){return typeof s=="object"?Array.isArray(s)?f(s):p(s):b(s)}function b(s){let{$subscribe:i,$publish:n}=e();return{$SUBSCRIPTION:!0,$subscribe:i,$static(){return s},$get(){return s},$set(t){s=t,n(t)}}}function f(s=[]){let{$subscribe:i,$publish:n}=e(),t=[];for(let r=0;r<s.length;r++){let c=$(s[r]);t.push(c),c.$subscribe((o,u)=>{n(o,u?`${r}.${u}`:`${r}`)})}return t.$SUBSCRIPTION=!0,t.$subscribe=i,t.$static=function(){return t.map(r=>r.$static())},t.$mutate=function(r){r(t);for(let c=0;c<t.length;c++)t[c].$SUBSCRIPTION||(t[c]=$(t[c]));n(t.$static())},t}function p(s={}){let{$subscribe:i,$publish:n}=e(),t={};for(let r in s)t[r]=$(s[r]),t[r].$subscribe((c,o)=>{n(c,o?`${r}.${o}`:r)});return t.$SUBSCRIPTION=!0,t.$subscribe=i,t.$static=function(){let r={};for(let c in t)c.startsWith("$")||(r[c]=t[c].$static());return r},t.$set=function(r,c){t[r]=$(c),t[r].$subscribe((o,u)=>{n(o,u?`${r}.${u}`:r)}),n(t[r].$static(),r)},t}})(); | ||
| //# sourceMappingURL=index.js.map |
| { | ||
| "version": 3, | ||
| "sources": ["../lib/subscription.js", "../lib/array.js", "../lib/object.js", "../lib/shared.js", "../lib/value.js"], | ||
| "sourcesContent": ["export default class Subscription {\n constructor() {\n this._subscribers = []\n }\n\n subscribe(subscriber) {\n this._subscribers.push(subscriber)\n }\n\n publish(...values) {\n for (let subscriber of this._subscribers) {\n subscriber(...values)\n }\n }\n}\n\nexport function factory() {\n return new Subscriber()\n}\n", "import Subscription from \"./subscription\";\nimport { makeSubscription } from \"./shared\";\n\nexport default class ReactiveArray extends Subscription {\n constructor(initialArray = []) {\n super()\n\n this._internal = []\n\n for (const value of initialArray) {\n this.push(value)\n }\n }\n\n _makeSubscriberFunc(key) {\n return (value, oldKey = null) => {\n this.publish(value, oldKey ? `${key}.${oldKey}` : `${key}`)\n }\n }\n\n _makeAndSubscribe(value, key) {\n const item = makeSubscription(value)\n item.subscribe(this._makeSubscriberFunc(key))\n return item\n }\n\n static() {\n return this._internal.map(item => item.static())\n }\n\n set(newArr) {\n this._internal.length = 0\n for (const value of newArr) {\n this.publish(value)\n }\n }\n\n /* Array methods */\n push(value) {\n const key = this._internal.length\n const item = this._makeAndSubscribe(value, key)\n this._internal.push(item)\n this.publish(value, `${key}`)\n }\n}\n\nexport function factory(initialArray) {\n return new ReactiveArray(initialArray)\n}\n", "import Subscription from \"./subscription\";\nimport { makeSubscription } from \"./shared\";\n\nexport default class ReactiveObject extends Subscription {\n constructor(initialObject = {}) {\n super()\n\n this._internal = {}\n\n for (const key in initialObject) {\n this.set(key, initialObject[key])\n }\n }\n\n _makeSubscriberFunc(key) {\n return (value, oldKey = null) => {\n this.publish(value, oldKey ? `${key}.${oldKey}` : key)\n }\n }\n\n _makeAndSubscribe(value, key) {\n const item = makeSubscription(value)\n item.subscribe(this._makeSubscriberFunc(key))\n return item\n }\n\n static() {\n const _static = {}\n for (const key in this._internal) {\n _static[key] = this._internal[key].static()\n }\n }\n\n /* Object methods */\n set(key, value) {\n if (value === undefined) {\n // Replace current object with new object\n for (const key in this._internal) delete this._internal[key]\n for (const _key in key) this.set(_key, key[_key])\n } else {\n // Set a sub-part of current object\n if (this._internal[key] !== undefined) {\n this._internal[key].set(value)\n } else {\n const item = this._makeAndSubscribe(value, key)\n this._internal[key] = item\n this.publish(value, key)\n }\n }\n }\n\n key(key) {\n return this._internal[key]\n }\n}\n\nexport function factory(initialObject) {\n return new ReactiveObject(initialObject)\n}\n", "import ReactiveValue from \"./value\"\nimport ReactiveArray from \"./array\"\nimport ReactiveObject from \"./object\"\n\nexport function makeSubscription(item) {\n if (typeof item === \"object\") {\n if (Array.isArray(item)) {\n return new ReactiveArray(item)\n } else {\n return new ReactiveObject(item)\n }\n }\n\n return new ReactiveValue(item)\n}\n", "import Subscription from \"./subscription\";\nimport { makeSubscription } from \"./shared\";\n\nexport default class ReactiveValue extends Subscription {\n constructor(initialValue) {\n super()\n this._value = initialValue\n }\n\n get() {\n return this._value\n }\n\n set(value) {\n this._value = value\n this.publish(this._value)\n }\n\n static() {\n return this._value\n }\n}\n\nexport function factory(initialValue) {\n return new ReactiveValue(initialValue)\n}\n"], | ||
| "mappings": "MAAA,IAAqBA,EAArB,KAAkC,CAC9B,aAAc,CACV,KAAK,aAAe,CAAC,CACzB,CAEA,UAAUC,EAAY,CAClB,KAAK,aAAa,KAAKA,CAAU,CACrC,CAEA,WAAWC,EAAQ,CACf,QAASD,KAAc,KAAK,aACxBA,EAAW,GAAGC,CAAM,CAE5B,CACJ,EAEO,SAASC,GAAU,CACtB,OAAO,IAAI,UACf,CCfA,IAAqBC,EAArB,cAA2CC,CAAa,CACpD,YAAYC,EAAe,CAAC,EAAG,CAC3B,MAAM,EAEN,KAAK,UAAY,CAAC,EAElB,QAAWC,KAASD,EAChB,KAAK,KAAKC,CAAK,CAEvB,C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,CAElD,CAGA,IAAIA,EAAKC,EAAO,CACZ,GAAIA,IAAU,OAAW,CAErB,QAAWD,KAAO,KAAK,UAAW,OAAO,KAAK,UAAUA,CAAG,EAC3D,QAAWM,KAAQN,EAAK,KAAK,IAAIM,EAAMN,EAAIM,CAAI,CAAC,UAG5C,KAAK,UAAUN,CAAG,IAAM,OACxB,KAAK,UAAUA,CAAG,EAAE,IAAIC,CAAK,MAC1B,CACH,IAAME,EAAO,KAAK,kBAAkBF,EAAOD,CAAG,EAC9C,KAAK,UAAUA,CAAG,EAAIG,EACtB,KAAK,QAAQF,EAAOD,CAAG,EAGnC,CAEA,IAAIA,EAAK,CACL,OAAO,KAAK,UAAUA,CAAG,CAC7B,CACJ,EAEO,SAASO,EAAQR,EAAe,CACnC,OAAO,IAAIF,EAAeE,CAAa,CAC3C,CCtDO,SAASS,EAAiBC,EAAM,CACnC,OAAI,OAAOA,GAAS,SACZ,MAAM,QAAQA,CAAI,EACX,IAAIC,EAAcD,CAAI,EAEtB,IAAIE,EAAeF,CAAI,EAI/B,IAAIG,EAAcH,CAAI,CACjC,CCXA,IAAqBI,EAArB,cAA2CC,CAAa,CACpD,YAAYC,EAAc,CACtB,MAAM,EACN,KAAK,OAASA,CAClB,CAEA,KAAM,CACF,OAAO,KAAK,MAChB,CAEA,IAAIC,EAAO,CACP,KAAK,OAASA,EACd,KAAK,QAAQ,KAAK,MAAM,CAC5B,CAEA,QAAS,CACL,OAAO,KAAK,MAChB,CACJ,EAEO,SAASC,EAAQF,EAAc,CAClC,OAAO,IAAIF,EAAcE,CAAY,CACzC", | ||
| "names": ["Subscription", "subscriber", "values", "factory", "ReactiveArray", "Subscription", "initialArray", "value", "key", "oldKey", "item", "makeSubscription", "newArr", "factory", "ReactiveObject", "Subscription", "initialObject", "key", "value", "oldKey", "item", "makeSubscription", "_static", "_key", "factory", "makeSubscription", "item", "ReactiveArray", "ReactiveObject", "ReactiveValue", "ReactiveValue", "Subscription", "initialValue", "value", "factory"] | ||
| "sources": ["../lib/base.js"], | ||
| "sourcesContent": ["export function subscription() {\n const subscribers = []\n return {\n $SUBSCRIPTION: true,\n $subscribe(subscriber) {\n subscribers.push(subscriber)\n },\n $publish(...data) {\n subscribers.forEach(subscriber => subscriber(...data))\n },\n }\n}\n\nexport function makeSubscription(value) {\n if (typeof value === \"object\") {\n if (Array.isArray(value)) {\n return group(value)\n } else {\n return store(value)\n }\n }\n\n return reactive(value)\n}\n\nexport function reactive(value) {\n const { $subscribe, $publish } = subscription()\n return {\n $SUBSCRIPTION: true,\n $subscribe,\n $static() {\n return value\n },\n $get() {\n return value\n },\n $set(newValue) {\n value = newValue\n $publish(newValue)\n }\n }\n}\n\nexport function group(initial = []) {\n const { $subscribe, $publish } = subscription()\n\n const _group = []\n\n for (let i = 0; i < initial.length; i++) {\n const item = makeSubscription(initial[i])\n _group.push(item)\n item.$subscribe((newValue, keyParts) => {\n $publish(newValue, keyParts ? `${i}.${keyParts}` : `${i}`)\n })\n }\n\n _group.$SUBSCRIPTION = true\n _group.$subscribe = $subscribe\n _group.$static = function () {\n return _group.map(item => item.$static())\n }\n _group.$mutate = function (fn) {\n fn(_group)\n for (let i = 0; i < _group.length; i++) {\n if (!_group[i].$SUBSCRIPTION) {\n _group[i] = makeSubscription(_group[i])\n }\n }\n $publish(_group.$static())\n }\n\n return _group\n}\n\nexport function store(initial = {}) {\n const { $subscribe, $publish } = subscription()\n\n const _store = {}\n\n for (const key in initial) {\n _store[key] = makeSubscription(initial[key])\n _store[key].$subscribe((newValue, keyParts) => {\n $publish(newValue, keyParts ? `${key}.${keyParts}` : key)\n })\n }\n\n _store.$SUBSCRIPTION = true\n _store.$subscribe = $subscribe\n _store.$static = function () {\n const _static = {}\n for (const key in _store) {\n if (!key.startsWith(\"$\")) {\n _static[key] = _store[key].$static()\n }\n }\n return _static\n }\n _store.$set = function (key, value) {\n _store[key] = makeSubscription(value)\n _store[key].$subscribe((newValue, keyParts) => {\n $publish(newValue, keyParts ? `${key}.${keyParts}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AO,OAAQC,IAC1BD,EAAOC,CAAC,EAAE,gBACXD,EAAOC,CAAC,EAAIV,EAAiBS,EAAOC,CAAC,CAAC,GAG9CJ,EAASG,EAAO,QAAQ,CAAC,CAC7B,EAEOA,CACX,CAEO,SAASN,EAAMK,EAAU,CAAC,EAAG,CAChC,GAAM,CAAE,WAAAH,EAAY,SAAAC,CAAS,EAAIV,EAAa,EAExCkB,EAAS,CAAC,EAEhB,QAAWC,KAAOP,EACdM,EAAOC,CAAG,EAAIf,EAAiBQ,EAAQO,CAAG,CAAC,EAC3CD,EAAOC,CAAG,EAAE,WAAW,CAACR,EAAUK,IAAa,CAC3CN,EAASC,EAAUK,EAAW,GAAGG,KAAOH,IAAaG,CAAG,CAC5D,CAAC,EAGL,OAAAD,EAAO,cAAgB,GACvBA,EAAO,WAAaT,EACpBS,EAAO,QAAU,UAAY,CACzB,IAAME,EAAU,CAAC,EACjB,QAAWD,KAAOD,EACTC,EAAI,WAAW,GAAG,IACnBC,EAAQD,CAAG,EAAID,EAAOC,CAAG,EAAE,QAAQ,GAG3C,OAAOC,CACX,EACAF,EAAO,KAAO,SAAUC,EAAKd,EAAO,CAChCa,EAAOC,CAAG,EAAIf,EAAiBC,CAAK,EACpCa,EAAOC,CAAG,EAAE,WAAW,CAACR,EAAUK,IAAa,CAC3CN,EAASC,EAAUK,EAAW,GAAGG,KAAOH,IAAaG,CAAG,CAC5D,CAAC,EACDT,EAASQ,EAAOC,CAAG,EAAE,QAAQ,EAAGA,CAAG,CACvC,EAEOD,CACX", | ||
| "names": ["subscription", "subscribers", "subscriber", "data", "makeSubscription", "value", "group", "store", "reactive", "$subscribe", "$publish", "newValue", "initial", "_group", "i", "item", "keyParts", "fn", "_store", "key", "_static"] | ||
| } |
+1
-4
@@ -1,4 +0,1 @@ | ||
| export { default as Subscription, factory as s } from "./subscription" | ||
| export { default as ReactiveValue, factory as v } from "./value" | ||
| export { default as ReactiveArray, factory as a } from "./array" | ||
| export { default as ReactiveObject, factory as o } from "./object" | ||
| export * from "./base" |
+3
-3
| { | ||
| "name": "@coderkearns/reactivity", | ||
| "version": "2.0.0", | ||
| "version": "3.0.0", | ||
| "description": "My personal reactivity library", | ||
@@ -28,4 +28,4 @@ "main": "lib/index.js", | ||
| "clean": "rm -rf dist cjs", | ||
| "build:web": "esbuild . --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/index.js", | ||
| "build:cjs": "esbuild . --bundle --platform=node --target=node10.4 --outfile=cjs/index.js", | ||
| "build:web": "esbuild . --bundle --minify --sourcemap --target=es2015 --outfile=dist/index.js", | ||
| "build:cjs": "esbuild . --bundle --minify --platform=node --target=node10.4 --outfile=cjs/index.js", | ||
| "build": "pnpm clean; pnpm build:web && pnpm build:cjs", | ||
@@ -32,0 +32,0 @@ "publish-package": "pnpm build && pnpm publish --access=public" |
-49
| import Subscription from "./subscription"; | ||
| import { makeSubscription } from "./shared"; | ||
| export default class ReactiveArray extends Subscription { | ||
| constructor(initialArray = []) { | ||
| super() | ||
| this._internal = [] | ||
| for (const value of initialArray) { | ||
| this.push(value) | ||
| } | ||
| } | ||
| _makeSubscriberFunc(key) { | ||
| return (value, oldKey = null) => { | ||
| this.publish(value, oldKey ? `${key}.${oldKey}` : `${key}`) | ||
| } | ||
| } | ||
| _makeAndSubscribe(value, key) { | ||
| const item = makeSubscription(value) | ||
| item.subscribe(this._makeSubscriberFunc(key)) | ||
| return item | ||
| } | ||
| static() { | ||
| return this._internal.map(item => item.static()) | ||
| } | ||
| set(newArr) { | ||
| this._internal.length = 0 | ||
| for (const value of newArr) { | ||
| this.publish(value) | ||
| } | ||
| } | ||
| /* Array methods */ | ||
| push(value) { | ||
| const key = this._internal.length | ||
| const item = this._makeAndSubscribe(value, key) | ||
| this._internal.push(item) | ||
| this.publish(value, `${key}`) | ||
| } | ||
| } | ||
| export function factory(initialArray) { | ||
| return new ReactiveArray(initialArray) | ||
| } |
| import Subscription from "./subscription"; | ||
| import { makeSubscription } from "./shared"; | ||
| export default class ReactiveObject extends Subscription { | ||
| constructor(initialObject = {}) { | ||
| super() | ||
| this._internal = {} | ||
| for (const key in initialObject) { | ||
| this.set(key, initialObject[key]) | ||
| } | ||
| } | ||
| _makeSubscriberFunc(key) { | ||
| return (value, oldKey = null) => { | ||
| this.publish(value, oldKey ? `${key}.${oldKey}` : key) | ||
| } | ||
| } | ||
| _makeAndSubscribe(value, key) { | ||
| const item = makeSubscription(value) | ||
| item.subscribe(this._makeSubscriberFunc(key)) | ||
| return item | ||
| } | ||
| static() { | ||
| const _static = {} | ||
| for (const key in this._internal) { | ||
| _static[key] = this._internal[key].static() | ||
| } | ||
| } | ||
| /* Object methods */ | ||
| set(key, value) { | ||
| if (value === undefined) { | ||
| // Replace current object with new object | ||
| for (const key in this._internal) delete this._internal[key] | ||
| for (const _key in key) this.set(_key, key[_key]) | ||
| } else { | ||
| // Set a sub-part of current object | ||
| if (this._internal[key] !== undefined) { | ||
| this._internal[key].set(value) | ||
| } else { | ||
| const item = this._makeAndSubscribe(value, key) | ||
| this._internal[key] = item | ||
| this.publish(value, key) | ||
| } | ||
| } | ||
| } | ||
| key(key) { | ||
| return this._internal[key] | ||
| } | ||
| } | ||
| export function factory(initialObject) { | ||
| return new ReactiveObject(initialObject) | ||
| } |
| import ReactiveValue from "./value" | ||
| import ReactiveArray from "./array" | ||
| import ReactiveObject from "./object" | ||
| export function makeSubscription(item) { | ||
| if (typeof item === "object") { | ||
| if (Array.isArray(item)) { | ||
| return new ReactiveArray(item) | ||
| } else { | ||
| return new ReactiveObject(item) | ||
| } | ||
| } | ||
| return new ReactiveValue(item) | ||
| } |
| export default class Subscription { | ||
| constructor() { | ||
| this._subscribers = [] | ||
| } | ||
| subscribe(subscriber) { | ||
| this._subscribers.push(subscriber) | ||
| } | ||
| publish(...values) { | ||
| for (let subscriber of this._subscribers) { | ||
| subscriber(...values) | ||
| } | ||
| } | ||
| } | ||
| export function factory() { | ||
| return new Subscriber() | ||
| } |
-26
| import Subscription from "./subscription"; | ||
| import { makeSubscription } from "./shared"; | ||
| export default class ReactiveValue extends Subscription { | ||
| constructor(initialValue) { | ||
| super() | ||
| this._value = initialValue | ||
| } | ||
| get() { | ||
| return this._value | ||
| } | ||
| set(value) { | ||
| this._value = value | ||
| this.publish(this._value) | ||
| } | ||
| static() { | ||
| return this._value | ||
| } | ||
| } | ||
| export function factory(initialValue) { | ||
| return new ReactiveValue(initialValue) | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
12939
-35.74%8
-33.33%108
-67.17%4
33.33%1
Infinity%