micro-message
Version:
global data store of micro front-end / multi app
7 lines (6 loc) • 2.87 kB
JavaScript
/*!
* micro-message v1.2.15
* (c) 2020-2023 fz6m && qiutian00
* Released under the MIT License.
*/
import n from"lodash/get";import t from"lodash/set";import r from"lodash/isEqual";var e="_$store",o="__$$",c="__subscribe__",i="__value_subscribe__",u=[c,i],a="_$all";n(window,e)||t(window,e,{});var f=function(n){return"function"==typeof n},l=function(n){return~u.indexOf(n)},d=function(n){var t=l(n);return"".concat(t?o:"_$").concat(n)},p=function(n,t){var r=l(n);return"".concat(r?o:"_$").concat(n,".").concat(t)},s=function(){return s=Object.assign||function(n){for(var t,r=1,e=arguments.length;r<e;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n},s.apply(this,arguments)};function v(n,t,r){if(r||2===arguments.length)for(var e,o=0,c=t.length;o<c;o++)!e&&o in t||(e||(e=Array.prototype.slice.call(t,0,o)),e[o]=t[o]);return n.concat(e||Array.prototype.slice.call(t))}var h=function(n,t){return E(n,t)||[]},y=function(n,t,r){var e=h(n,r);if(!~e.indexOf(t)){var o=v(v([],e,!0),[t],!1);x(n,o,r)}},g=function(n,t){for(var r=[],e=2;e<arguments.length;e++)r[e-2]=arguments[e];var o=h(n,t);o.forEach((function(n){try{n.apply(void 0,r)}catch(n){}}))},_=function(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];var e=h(a,n);e.forEach((function(n){try{n.apply(void 0,t)}catch(n){}}))},$=function(n){return{get:function(t){return h(t,n)},add:function(t,r){return y(t,r,n)},addToAll:function(t){return function(n,t){y(a,n,t)}(t,n)},destory:function(t){return function(n,t){x(n,[],t)}(t,n)},delete:function(t,r){return function(n,t,r){var e=h(n,r),o=e.indexOf(t);return!!~o&&(e.splice(o,1),x(n,e,r),!0)}(t,r,n)},dispatch:function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return g.apply(void 0,v([t,n],r,!1))},dispatchAll:function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return _.apply(void 0,v([n],t,!1))}}},w=$(i),m=function(n){var t=n.key,e=n.newValue,o=n.oldValue,c=n.newStoreValue,i=function(n){return u.some((function(t){return n.startsWith(d(t))}))}(t);i||r(o,e)||(w.dispatchAll(c),w.dispatch(t,e,o))};var S={$on:function(n,t){!f(n)||t?"string"==typeof n&&f(t)&&w.add(n,t):w.addToAll(n)},$destory:w.destory,$delete:w.delete},b=function(){return n(window,e)||{}},A=function(){return function(n){var t=s({},n);return u.forEach((function(n){delete t[d(n)]})),t}(b())},O=function(t){var r=b();return n(r,t)},V=function(n,r){var e=O(n),o=b();t(o,n,r),m({key:n,newValue:r,oldValue:e,newStoreValue:A()})},x=function(n,t,r){var e=p(r,n);V(e,t)},E=function(n,t){var r=p(t,n);return O(r)},W={getStore:A,get:O,set:V,getWithScope:E,setWithScope:x,getScope:function(n){var t=d(n);return O(t)||{}},setScope:function(n,t){var r=d(t);V(r,n)}},j=$(c),k={$on:j.add,$off:j.destory,$emit:function(n,t){j.dispatch(n,t)},$delete:j.delete};export{W as Store,k as StoreBus,S as StoreChange};