UNPKG

@obsidize/rx-map

Version:

ES6 Map with rxjs extensions for change detection

1 lines 10.4 kB
(()=>{"use strict";var e={d:(t,r)=>{for(var s in r)e.o(r,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:r[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{ChangeDetectionResultType:()=>n,EntityMap:()=>A,ImmutableMap:()=>k,MapStateChangeEventType:()=>i,OneToManyContext:()=>V,OneToManyRelationship:()=>j,ProxyIterableIterator:()=>x,RxEntityMap:()=>R,RxMap:()=>P,RxStore:()=>I,Subsink:()=>w,accumulateChanges:()=>T,castArray:()=>y,deepDifferenceBetween:()=>p,detectAccumulatedChanges:()=>d,detectChanges:()=>g,extractChanges:()=>f,forKey:()=>b,forKeyIn:()=>S,isActionableChangeDetectionResultType:()=>a,ofType:()=>E,pluckChanges:()=>M,pluckValue:()=>v,pluckValueChanges:()=>O,spreadFilterBy:()=>m,storeEntityArrayIn:()=>C,storeEntityIn:()=>K});const r=require("rxjs/operators"),s=require("lodash");var n,i;function a(e){return e!==n.NO_CHANGE&&Object.values(n).includes(e)}function o(e,...t){return e}function u(e){return void 0===e}function h(e){return"function"==typeof e}function c(e){return"object"==typeof e&&!function(e){return null===typeof e}(e)}function y(e){return c(e)&&Array.isArray(e)?e:[e]}function l(e){return(0,s.cloneDeep)(e)}function p(e,t){return(0,s.transform)(e,((e,r,n)=>{const i=t[n];if((0,s.isEqual)(r,i))return;const a=c(r)&&c(i);e[n]=a?p(r,i):r}))}function g(e,t){const r=(e,t)=>{const r={type:e};return t&&(r.changes=t),r};return(0,s.isEqual)(e,t)?r(n.NO_CHANGE):e&&!t?r(n.CREATE):!e&&t?r(n.DELETE):r(n.UPDATE,p(e,t))}function f(e){const{value:t,previousValue:r}=e,s=g(t,r);return e.changes=s.changes,e.changeType=s.type,e}function d(e,t){const r=e?e.current:void 0,s=g(t,r);return Object.assign({current:t,previous:r},s)}function m(e,t){const s=new Set(e);return e=>e.pipe((0,r.filter)((e=>s.has(t(e)))))}function E(...e){return t=>t.pipe(m(e,(e=>e.type)))}function b(e){return t=>t.pipe((0,r.filter)((t=>t.key===e)))}function S(e){return t=>t.pipe(m(e,(e=>e.key)))}function O(e){const t=t=>t?e(t):void 0;return e=>e.pipe((0,r.filter)((e=>!!e)),(0,r.map)((e=>({entityId:e.key,currentValue:t(e.value),previousValue:t(e.previousValue)}))))}function v(){return e=>e.pipe((0,r.map)((e=>e.value)),(0,r.filter)(o))}function M(){return e=>e.pipe((0,r.map)((e=>e.changes)),(0,r.filter)(o))}function K(e){return t=>t.pipe((0,r.map)((t=>e.setOne(t))))}function C(e){return t=>t.pipe((0,r.map)((t=>e.setMany(t))))}function T(){return e=>e.pipe((0,r.scan)(((e,t)=>d(e,t))),(0,r.filter)((e=>!!e&&a(e.type))))}!function(e){e.CREATE="CREATE",e.UPDATE="UPDATE",e.DELETE="DELETE",e.NO_CHANGE="NO_CHANGE"}(n||(n={}));class w{constructor(){this.mSubscriptions=new Set}static unsubscribeSafe(e){try{e.unsubscribe()}catch(e){}}add(...e){return this.addMany(e)}unsubscribe(){this.mSubscriptions.forEach(w.unsubscribeSafe),this.mSubscriptions.clear()}addMany(e){return y(e).forEach((e=>this.mSubscriptions.add(e))),this}}!function(e){e.SET="SET",e.DELETE="DELETE"}(i||(i={}));class A{constructor(e,t){this.store=e,this.selectKey=t}onSetEntry(e,t){this.store.set(e,t)}onDeleteEntry(e){return this.store.delete(e)}onMergeEntries(e,t){return function(e,t){return(0,s.merge)(e,t)}(e,t)}get count(){return this.store.size}keyOf(e){return e?this.selectKey(e):void 0}isValidKey(e){return!u(e)}iterableKeys(){return this.store.keys()}keys(){return Array.from(this.iterableKeys())}iterableValues(){return this.store.values()}values(){return Array.from(this.iterableValues())}iterableEntries(){return this.store.entries()}entries(){return Array.from(this.iterableEntries())}getOne(e){return this.store.get(e)}getMany(e){return y(e).map((e=>this.getOne(e)))}getManyExisting(e){return this.getMany(e).filter(o)}hasOne(e){return this.store.has(e)}hasEvery(e){return y(e).every((e=>this.hasOne(e)))}hasSome(e){return y(e).some((e=>this.hasOne(e)))}addOne(e){return this.upsertOne(e)}addMany(e){return y(e).map((e=>this.addOne(e)))}setOne(e){const t=this.keyOf(e);return this.isValidKey(t)&&this.onSetEntry(t,e),e}setMany(e){return y(e).map((e=>this.setOne(e)))}setAll(e){return this.removeAll(),this.setMany(e)}removeOne(e){return this.onDeleteEntry(e)}removeMany(e){return y(e).map((e=>this.removeOne(e)))}removeWhere(e){let t=[];return h(e)?(this.store.forEach(((r,s)=>{e(r)&&(this.removeOne(s),t.push(r))})),t):t}removeAll(){this.store.clear()}updateOneByKey(e,t){const r=this.onMergeEntries(this.store.get(e),t);return this.onSetEntry(e,r),r}updateOne(e){if(!e)return;const{key:t,changes:r}=e;return this.updateOneByKey(t,r)}updateMany(e){return y(e).map((e=>this.updateOne(e)))}upsertOne(e){const t=this.keyOf(e);if(!this.isValidKey(t))return e;const r={key:t,changes:e};return this.updateOne(r)}upsertMany(e){return y(e).map((e=>this.upsertOne(e)))}transformOne(e,t){const r=this.getOne(e);if(!r||!h(t))return r;const s=t(r);return this.updateOne({key:e,changes:s})}transformMany(e){const t=[];return h(e)?(this.store.forEach(((r,s)=>{const n=e(r),i=this.updateOne({key:s,changes:n});t.push(i)})),t):t}}class x{constructor(e,t){this.source=e,this.transform=t}[Symbol.iterator](){return this}next(){const{value:e,done:t}=this.source.next();return{value:this.transform(e),done:t}}}class k{constructor(e){this.source=e}static standard(){return new k(new Map)}get size(){return this.source.size}get[Symbol.toStringTag](){return"ImmutableMap"}[Symbol.iterator](){return this.entries()}clear(){this.source.clear()}get(e){return l(this.source.get(e))}has(e){return this.source.has(e)}delete(e){return this.source.delete(e)}set(e,t){return this.source.set(l(e),l(t)),this}entries(){return new x(this.source.entries(),(e=>l(e)))}keys(){return new x(this.source.keys(),(e=>l(e)))}values(){return new x(this.source.values(),(e=>l(e)))}forEach(e){h(e)&&this.source.forEach(((t,r)=>e(l(t),l(r),this)))}}const D=require("rxjs");class P{constructor(e){this.source=e,this.mStateChangeSubject=new D.Subject,this.allChanges=this.mStateChangeSubject.asObservable().pipe((0,r.share)()),this.changes=this.allChanges.pipe((0,r.map)((e=>f(e))),(0,r.filter)((e=>a(e.changeType))),(0,r.share)())}static mutable(){return new P(new Map)}static immutable(){return new P(k.standard())}emit(e,t,r,s,n={source:"unknown"}){this.mStateChangeSubject.next({type:e,key:t,value:r,previousValue:s,context:n})}emitSet(e,t,r,s){this.emit(i.SET,e,t,r,s)}emitDelete(e,t,r){this.emit(i.DELETE,e,t,void 0,r)}get size(){return this.source.size}get[Symbol.toStringTag](){return"RxMap"}[Symbol.iterator](){return this.entries()}get(e){return this.source.get(e)}has(e){return this.source.has(e)}entries(){return this.source.entries()}keys(){return this.source.keys()}values(){return this.source.values()}forEach(e){this.source.forEach(e)}destroy(){const e=this.mStateChangeSubject;e.closed&&e.isStopped||(e.error("destroyed"),e.unsubscribe())}clear(){Array.from(this.keys()).forEach((e=>this.delete(e)))}delete(e,t){const r=this.get(e),s=this.source.delete(e);return this.emitDelete(e,r,t),s}set(e,t,r){const s=this.get(e);return this.source.set(e,t),this.emitSet(e,t,s,r),this}}class R extends A{constructor(e,t){super(t,e)}static mutable(e){return new R(e,P.mutable())}static immutable(e){return new R(e,P.immutable())}get allChanges(){return this.store.allChanges}get changes(){return this.store.changes}destroy(){this.store.destroy()}watchOne(e){return this.changes.pipe(b(e),v(),(0,r.startWith)(this.getOne(e)),(0,r.filter)(o))}watchMany(e){return this.changes.pipe(S(e),(0,r.map)((()=>this.getManyExisting(e))),(0,r.startWith)(this.getManyExisting(e)))}watchAll(){return this.changes.pipe((0,r.map)((()=>this.values())),(0,r.startWith)(this.values()))}}class V{constructor(e){this.id=e,this.foreignKeySet=new Set}clear(){this.foreignKeySet.clear()}getForeignKeys(){return Array.from(this.foreignKeySet)}setForeignKeys(e){this.clear(),y(e).forEach((e=>this.foreignKeySet.add(e)))}}class j{constructor(e,t){this.entityMap=e,this.selectForeignKey=t,this.store=new Map,this.changes=this.entityMap.changes.pipe(O((e=>this.selectForeignKey(e))),(0,r.filter)((e=>e.currentValue!==e.previousValue)),(0,r.tap)((e=>this.consume(e))),(0,r.share)())}getPrimaryKeys(){return Array.from(this.store.keys())}clear(){this.store.forEach((e=>e.clear())),this.store.clear()}associate(e,t){const r=this.getPrimaryKeyContext(e);null==r||r.foreignKeySet.add(t)}disassociate(e,t){const r=this.getPrimaryKeyContext(e);null==r||r.foreignKeySet.delete(t)}hasAssociation(e,t){const r=this.getPrimaryKeyContext(e);return!!r&&r.foreignKeySet.has(t)}getRelatedKeys(e){const t=this.getPrimaryKeyContext(e);return t?t.getForeignKeys():[]}getRelatedKeyCount(e){const t=this.getPrimaryKeyContext(e);return t?t.foreignKeySet.size:0}hasAnyAssociation(e){return this.getRelatedKeyCount(e)>0}getRelatedValues(e){return this.entityMap.getManyExisting(this.getRelatedKeys(e))}consume(e){e&&(this.disassociate(e.previousValue,e.entityId),this.associate(e.currentValue,e.entityId))}watchPrimaryKey(e){return this.changes.pipe((0,r.filter)((t=>!!t&&(t.previousValue===e||t.currentValue===e))),(0,r.map)((()=>this.getRelatedValues(e))),(0,r.startWith)(this.getRelatedValues(e)))}deletePrimaryKeyContext(e){const t=this.store.get(e);return t&&t.clear(),this.store.delete(e)}getPrimaryKeyContext(e){let t=this.store.get(e);return t||u(e)||(t=new V(e),this.store.set(e,t)),t}}class I{constructor(){this.effectSubscriptions=new w,this.properties=new Set,this.entityMaps=new Set,this.oneToManyRelationships=new Set}onDestroyProperty(e){e.error("store_property_destroyed"),e.unsubscribe()}onDestroyEntityMap(e){e.destroy()}onDestroyOneToManyRelationship(e){e.clear()}defineProperty(e){return this.registerProperty(new D.BehaviorSubject(e))}registerProperty(e){return this.properties.add(e),e}defineEntity(e){return this.registerEntity(R.immutable(e))}registerEntity(e){return this.entityMaps.add(e),e}registerEffect(e){return(0,D.isObservable)(e)&&this.effectSubscriptions.add(e.subscribe()),e}defineEntityForeignKey(e,t){const r=new j(e,t);return this.oneToManyRelationships.add(r),this.registerEffect(r.changes),r}destroy(){this.effectSubscriptions.unsubscribe(),this.oneToManyRelationships.forEach((e=>this.onDestroyOneToManyRelationship(e))),this.entityMaps.forEach((e=>this.onDestroyEntityMap(e))),this.properties.forEach((e=>this.onDestroyProperty(e)))}}module.exports=t})();