@cyysummer/projector 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @cyysummer/projector v0.0.6
2
+ * @cyysummer/projector v0.0.7
3
3
  * (c) 2021-PRESENT Chris Liu
4
4
  * @license MIT
5
5
  **/
6
- import m from"on-change";import{set as g}from"lodash-es";import{withEvents as I}from"@cyysummer/core";var i=class extends I(){constructor(e){super();this._paused=!1;this._shadow=structuredClone(e)}receive(e,r){if(this._paused)return;g(this._shadow,e,r);let c=[{path:e,value:r}];this.emit("record",{next:this._shadow,patches:c}),this._ensureProjector(),this._projector?.project(this._shadow,...c)}sendTo(e){typeof e=="function"?this._projectorFactory=e:this._projector=e}pause(){this._paused=!0}resume(){this._paused=!1}_ensureProjector(){!this._projector&&this._projectorFactory&&(this._projector=this._projectorFactory())}};import{get as d}from"lodash-es";var l=u=>{},p=class{constructor(t){this._strategy=t}create(t){return(e,r)=>{let c=a(r);return this._strategy.execute(e,t,c)}}createWith(t,e){return(c,o)=>{let s=a(o),n=e(s);return this._strategy.execute(c,t,n)}}forEach(t){return(e,r)=>{}}fromArray(t){return(e,r)=>{let c=r.path?d(r.source,r.path):r.value;if(c&&Array.isArray(c)&&c.length>0){let o=Object.keys(c[0]);return this._strategy.executeArray(e,t,o,c)}}}fromSource(t,e){return(r,c)=>{let o=e(c.source);return this._strategy.execute(r,t,o)}}fromValue(t,e){return(r,c)=>this._strategy.execute(r,t,e)}sequence(t){return async(e,r)=>{let c=a(r);for(let o of t)await o(e,{value:c})}}sequenceWith(t,e){return async(c,o)=>{let s=a(o),n=e(s);for(let h of t)await h(c,{value:n})}}when(t,e,r=l){return(c,o)=>{let s=a(o);return(t(s)?e:r)(c,{value:s})}}whenFromSource(t,e,r=l){return(c,o)=>(t(o.source)?e:r)(c,o)}};function a(u){return u.source&&u.path?d(u.source,u.path):u.value}var T=class{constructor(t){this._schema=t;if(!t)throw new Error("Projector: Schema is required.")}project(t,...e){for(let r of e)this._dispatch(t,r)}_dispatch(t,e){let{path:r,value:c}=e,o=this._resolveEffect(r);o&&(this._ensureScheduler(),this._scheduler.enqueue({path:r.join("."),effect:o,ctx:{source:t,path:r,value:c}}))}_ensureScheduler(){if(!this._scheduler)throw new Error("Projector: no scheduler.")}_resolveEffect(t){let e=this._schema;for(let r of t){if(!e)return null;e=e[r]}return typeof e=="function"?e:null}},S=class extends T{constructor(t,e){super(t),this._scheduler=e}},y=class extends T{scheduleWith(t){return typeof t=="function"?this._schedulerFactory=t:this._scheduler=t,this}_ensureScheduler(){if(!this._scheduler&&this._schedulerFactory&&(this._scheduler=this._schedulerFactory()),!this._scheduler)throw new Error("Projector: no scheduler.")}};var f=class{constructor(t){this._target=t}withTarget(t){return typeof t=="function"?this._targetFactory=t:this._target=t,this}checkTarget(){if(!this._target&&this._targetFactory&&(this._target=this._targetFactory()),!this._target)throw new Error("Scheduler: target is not set")}},_=class extends f{constructor(){super(...arguments);this._queue=new Map}enqueue(e){return this._queue.set(e.path,e),this}async flush(){this.checkTarget(),log.trace("BufferedScheduler: flushing");let e=[...this._queue.values()];this._queue.clear();for(let r of e){let{effect:c,ctx:o}=r,s=c(this._target,o);s instanceof Promise&&await s}}};function R(u){let t=new i(u);return[m(u,t.receive.bind(t),{pathAsArray:!0}),t]}export{_ as BufferedScheduler,y as DynamicProjector,p as EffectFactory,S as Projector,T as ProjectorBase,i as Recorder,f as Scheduler,R as track};
6
+ import I from"on-change";import{set as E}from"lodash-es";import{withEvents as m}from"@cyysummer/core";var i=class extends m(){constructor(e){super();this._paused=!1;this._shadow=structuredClone(e)}receive(e,r){if(this._paused)return;E(this._shadow,e,r);let c=[{path:e,value:r}];this.emit("record",{next:this._shadow,patches:c}),this._ensureProjector(),this._projector?.project(this._shadow,...c)}sendTo(e){typeof e=="function"?this._projectorFactory=e:this._projector=e}pause(){this._paused=!0}resume(){this._paused=!1}_ensureProjector(){!this._projector&&this._projectorFactory&&(this._projector=this._projectorFactory())}};import{get as S}from"lodash-es";var p=u=>{},d=class{constructor(t){this._strategy=t}create(t){return(e,r)=>{let c=a(r);return this._strategy.execute(e,t,c)}}createWith(t,e){return(c,o)=>{let s=a(o),n=e(s);return this._strategy.execute(c,t,n)}}forEach(t){return async(e,r)=>{let c=a(r);if(Array.isArray(c)){let o=c;for(let s=0;s<o.length;s++){let n=o[s],l=t(n,s)(e,{value:n});l instanceof Promise&&await l}}else throw new Error(`Effect: Value at path '${r.path}' is not an array.`)}}fromArray(t){return(e,r)=>{let c=r.path?S(r.source,r.path):r.value;if(c&&Array.isArray(c)&&c.length>0){let o=Object.keys(c[0]);return this._strategy.executeArray(e,t,o,c)}}}fromPrimitive(t){return(e,r)=>{let c=a(r);return this._strategy.execute(e,t,c)}}fromSource(t,e){return(r,c)=>{let o=e(c.source);return this._strategy.execute(r,t,o)}}fromValue(t,e){return(r,c)=>this._strategy.execute(r,t,e)}sequence(t){return async(e,r)=>{let c=a(r);for(let o of t)await o(e,{value:c})}}sequenceWith(t,e){return async(c,o)=>{let s=a(o),n=e(s);for(let T of t)await T(c,{value:n})}}when(t,e,r=p){return(c,o)=>{let s=a(o);return(t(s)?e:r)(c,{value:s})}}whenFromSource(t,e,r=p){return(c,o)=>(t(o.source)?e:r)(c,o)}};function a(u){return u.source&&u.path?S(u.source,u.path):u.value}var f=class{constructor(t){this._schema=t;if(!t)throw new Error("Projector: Schema is required.")}project(t,...e){for(let r of e)this._dispatch(t,r)}_dispatch(t,e){let{path:r,value:c}=e,o=this._resolveEffect(r);o&&(this._ensureScheduler(),this._scheduler.enqueue({path:r.join("."),effect:o,ctx:{source:t,path:r,value:c}}))}_ensureScheduler(){if(!this._scheduler)throw new Error("Projector: no scheduler.")}_resolveEffect(t){let e=this._schema;for(let r of t){if(!e)return null;e=e[r]}return typeof e=="function"?e:null}},y=class extends f{constructor(t,e){super(t),this._scheduler=e}},_=class extends f{scheduleWith(t){return typeof t=="function"?this._schedulerFactory=t:this._scheduler=t,this}_ensureScheduler(){if(!this._scheduler&&this._schedulerFactory&&(this._scheduler=this._schedulerFactory()),!this._scheduler)throw new Error("Projector: no scheduler.")}};var h=class{constructor(t){this._target=t}withTarget(t){return typeof t=="function"?this._targetFactory=t:this._target=t,this}checkTarget(){if(!this._target&&this._targetFactory&&(this._target=this._targetFactory()),!this._target)throw new Error("Scheduler: target is not set")}},g=class extends h{constructor(){super(...arguments);this._queue=new Map}enqueue(e){return this._queue.set(e.path,e),this}async flush(){this.checkTarget(),log.trace("BufferedScheduler: flushing");let e=[...this._queue.values()];this._queue.clear();for(let r of e){let{effect:c,ctx:o}=r,s=c(this._target,o);s instanceof Promise&&await s}}};function k(u){let t=new i(u);return[I(u,t.receive.bind(t),{pathAsArray:!0}),t]}export{g as BufferedScheduler,_ as DynamicProjector,d as EffectFactory,y as Projector,f as ProjectorBase,i as Recorder,h as Scheduler,k as track};
7
7
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyysummer/projector",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "A powerful state projection library for creating anything.",
5
5
  "license": "MIT",
6
6
  "author": "Chris Liu",
package/types/lib.d.ts CHANGED
@@ -2,18 +2,16 @@
2
2
  * A Schema defines how to project changes from a source object to a target object.
3
3
  */
4
4
  // prettier-ignore
5
- type Schema<TSource, T = any> = TSource extends object ?
6
- Partial<{
7
- // If the property is an array, map whole array to effect.
8
- // Else map property to schema.
9
- [K in keyof T]:
10
- T[K] extends Array<infer U> ?
11
- Effect<TSource, U>
12
- // If TSource is an object, map each property to schema, or write effect for the property.
13
- : Schema<TSource, T[K]> | Effect<TSource, T[K]>;
14
- }>
15
- // Else map T to effect.
16
- : Effect<TSource, T>;
5
+ type Schema<TSource, T = TSource> = {
6
+ // If the property is an array, map to effect.
7
+ [K in keyof T]?: T[K] extends Array<infer U>
8
+ ? Effect<TSource, U[]>
9
+ // Else, if the property is an object, map to schema or effect.
10
+ : T[K] extends object
11
+ ? Schema<TSource, T[K]> | Effect<TSource, T[K]>
12
+ // Else, leaf property, map to effect.
13
+ : Effect<TSource, T[K]>;
14
+ };
17
15
 
18
16
  /**
19
17
  * An effect defines how to project a value from source to target.
@@ -128,13 +126,14 @@ declare class EffectFactory<TTarget, TSource extends object, TLocation = any> {
128
126
  * @returns
129
127
  */
130
128
  createWith<T, R>(location: TLocation, mapper: Func1<T, R>): Effect<TSource, R>;
131
- forEach<T extends Array<U>, U extends object = any>(location: Func1<number, TLocation>): Effect<TSource, any>;
129
+ forEach<T, U = T extends Array<infer K> ? K : never>(each: Func2<U, number, Effect<TSource, U>>): Effect<TSource, T>;
132
130
  /**
133
131
  * Create an effect that will applied at the specified location for array data.
134
132
  * @param location The location in `TTarget` where the effect is executed.
135
133
  * @returns
136
134
  */
137
135
  fromArray<T extends Array<U>, U extends object = any>(location: TLocation): Effect<TSource, T>;
136
+ fromPrimitive<T>(location: TLocation): Effect<TSource, T>;
138
137
  /**
139
138
  * Create an effect that will applied at the specified location. Use `mapper` to transform the source object.
140
139
  * @param location The location in `TTarget` where the effect is executed.
package/types/types.d.ts CHANGED
@@ -2,18 +2,16 @@
2
2
  * A Schema defines how to project changes from a source object to a target object.
3
3
  */
4
4
  // prettier-ignore
5
- export type Schema<TSource, T = any> = TSource extends object ?
6
- Partial<{
7
- // If the property is an array, map whole array to effect.
8
- // Else map property to schema.
9
- [K in keyof T]:
10
- T[K] extends Array<infer U> ?
11
- Effect<TSource, U>
12
- // If TSource is an object, map each property to schema, or write effect for the property.
13
- : Schema<TSource, T[K]> | Effect<TSource, T[K]>;
14
- }>
15
- // Else map T to effect.
16
- : Effect<TSource, T>;
5
+ export type Schema<TSource, T = TSource> = {
6
+ // If the property is an array, map to effect.
7
+ [K in keyof T]?: T[K] extends Array<infer U>
8
+ ? Effect<TSource, U[]>
9
+ // Else, if the property is an object, map to schema or effect.
10
+ : T[K] extends object
11
+ ? Schema<TSource, T[K]> | Effect<TSource, T[K]>
12
+ // Else, leaf property, map to effect.
13
+ : Effect<TSource, T[K]>;
14
+ };
17
15
 
18
16
  /**
19
17
  * An effect defines how to project a value from source to target.