@vue-modeler/model 2.2.5-beta.1 → 2.2.5

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # vue-modeler/model
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@vue-modeler/model.svg)](https://www.npmjs.com/package/@vue-modeler/model) [![codecov](https://codecov.io/gh/vue-modeler/model/branch/main/graph/badge.svg?token=BFQI22MBZ1)](https://codecov.io/gh/vue-modeler/model)
3
+ [![npm version](https://img.shields.io/npm/v/@vue-modeler/model.svg)](https://www.npmjs.com/package/@vue-modeler/model) [![codecov](https://codecov.io/gh/vue-modeler/model/branch/main/graph/badge.svg?token=BFQI22MBZ1)](https://codecov.io/gh/vue-modeler/model) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=vue-modeler_model&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=vue-modeler_model)
4
4
 
5
5
  ## What is @vue-modeler/model
6
6
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("vue");class v extends Error{constructor(t,e){super(e.cause.message),this.actionName=t,this.options=e,this.name=this.constructor.name}get cause(){return this.options.cause}throwCause(){throw this.cause}toString(){return this.options.cause.message}}class l extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class p extends l{constructor(t,e,r){super(`Trying to update state of ${t} from ${e} to ${r}`),this.name=this.constructor.name}}class A extends l{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const k=a=>a instanceof DOMException&&a.name==="AbortError"||typeof a=="object"&&a!==null&&"message"in a&&a.message==="canceled";class s{constructor(t,e,r,i,c){this._model=t,this.actionFunction=e,this.ownerGetter=r,this.setStateCb=i,this._validateArgs=c;const o=e.name;if(!(o in this._model&&typeof this._model[o]=="function"))throw new l(`Model does not contain method ${o}`);if(typeof e[s.actionFlag]!="function")throw new l(`Method ${o} is not action`);this.name=o}static actionFlag=Symbol("__action_original_method__");static possibleState={pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"};static abortedByLock=Symbol("lock");name;_state=s.possibleState.ready;_value=null;_args=null;static create(t,e,r,i,c){return h.shallowReactive(new s(t,e,r,i,c))}toString(){return this.name}get owner(){return this.ownerGetter()}get possibleStates(){return Object.values(s.possibleState)}get state(){return this._state}set state(t){const e=this._state;this._state=t,this.setStateCb(this,e,t)}get abortController(){return this.isPending?this._value.abortController:null}get args(){return this._args||[]}get promise(){return this.isPending?this._value.promise:null}get error(){return this.isError?this._value:null}get abortReason(){return this.isAbort?this._value.abortController.signal.reason:null}get isPending(){return this.state===s.possibleState.pending}get isError(){return this.state===s.possibleState.error}get isReady(){return this.state===s.possibleState.ready}get isLock(){return this.state===s.possibleState.lock}get isAbort(){return this.state===s.possibleState.abort}is(...t){return!!t.find(e=>this.state===e)}validate(...t){return this._validateArgs(this,...t)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new p(this.name,this.state,s.possibleState.pending);const e=[...t];let r=t.length&&t[t.length-1];r instanceof AbortController||(r=new AbortController,e.push(r)),this.state=s.possibleState.pending,this._args=t;const c=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(c instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const o=c.then(()=>{this.ready()}).catch(n=>{if(n instanceof l||n instanceof RangeError||n instanceof ReferenceError||n instanceof SyntaxError||n instanceof TypeError||n instanceof URIError||n instanceof EvalError)throw n;const S=k(n);if(S&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new A(this.name,this.state);if(S&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(S){this.state=s.possibleState.abort;return}this.setError(new v(this.name,{cause:n}))});return this._value={promise:o,abortController:r},o}abort(t){return this.isPending?(this._value.abortController.abort(t),this._value.promise):Promise.resolve()}lock(){return this.isPending?this.abort(s.abortedByLock):(this.state=s.possibleState.lock,this._value=null,Promise.resolve())}unlock(){if(!this.isLock)throw new p(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new p(this.name,this.state,s.possibleState.error);return this.state=s.possibleState.error,this._value=t,this}ready(){return this.state=s.possibleState.ready,this}resetError(){if(!this.error)throw new p(this.name,this.state,s.possibleState.ready);return this.ready()}}const u=Symbol("scope"),b=Symbol("model"),g=Symbol("actions"),w=Symbol("actionIds"),m=Symbol("actionStates"),y=Symbol("actionsSize"),d=Symbol("watchStopHandlers"),M=Proxy;class f{[u]=h.effectScope(!0);[b]=null;[g]=new WeakMap;[w]=new WeakMap;[m]=new Map;[y]=0;[d]=new Set;static createModel(t){if(!(t instanceof f))throw new TypeError("ProtoModel instance is required");const e=new M(h.shallowReactive(t),{get(r,i,c){const o=Reflect.get(r,i,c),n=typeof o=="function";return n&&s.actionFlag in o&&typeof o[s.actionFlag]=="function"?r.action(o):n?o.bind(r):o}});return t[b]=e,e}static model(...t){if(this.prototype===f.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t);return f.createModel(e)}get hasPendingActions(){return!!this.getActionStatesRef(s.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(s.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[u].run(()=>h.watchEffect(t[0])):this[u].run(()=>h.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[d].add(e),()=>{e(),this[d].delete(e)}}computed(t,e){return this[u].run(()=>h.computed(t,e))}updateBit(t,e,r){const i=r?1:0,c=~(1<<e);return t&c|i<<e}createAction(t){const e=()=>{if(!this[b])throw new Error("Model not set");return this[b]},r=s.create(this,t,e,this.setActionState.bind(this),this.validateArgs.bind(this));return this[g].set(t,r),this[w].set(r,++this[y]),this.setActionState(r),r}getActionStatesRef(t){const e=this[m].get(t)||h.ref(0);return this[m].get(t)===void 0&&this[m].set(t,e),e}action(t){if(!(s.actionFlag in t&&typeof t[s.actionFlag]=="function"))throw new l("Action decorator is not applied to the method");return this[g].get(t)||this.createAction(t)}setActionState(t){const e=this[w].get(t);if(!e)throw new Error("Action not found");for(const r of t.possibleStates)r!==t.state&&(this.getActionStatesRef(r).value=this.updateBit(this.getActionStatesRef(r).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}validateArgs(t,...e){const r=`validateArgs is not implemented for action ${t.name} in the model`;return[new Error(r)]}isModelOf(t){return this instanceof t}destructor(){this[d].forEach(t=>{t()}),this[d]=new Set,this[u].stop()}}function P(a,t){if(t.static)throw new Error("Action decorator is not supported for static methods");if(t.private)throw new Error("Action decorator is not supported for private methods");let e;try{e=t.name.toString()}catch(i){const o=`Invalid context. Can\`t get name of the method: ${i instanceof Error?i.message:"unknown error"}`;throw new Error(o)}const r={[e]:function(...i){return this.action(r[e]).exec(...i)}};return r[e][s.actionFlag]=a,r[e]}exports.Action=s;exports.ActionError=v;exports.ActionInternalError=l;exports.ActionStatusConflictError=p;exports.ActionUnexpectedAbortError=A;exports.ProtoModel=f;exports.action=P;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("vue");class v extends Error{constructor(t,e){super(e.cause.message),this.actionName=t,this.options=e,this.name=this.constructor.name}get cause(){return this.options.cause}throwCause(){throw this.cause}toString(){return this.options.cause.message}}class l extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class p extends l{constructor(t,e,r){super(`Trying to update state of ${t} from ${e} to ${r}`),this.name=this.constructor.name}}class A extends l{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const k=a=>a instanceof DOMException&&a.name==="AbortError"||typeof a=="object"&&a!==null&&"message"in a&&a.message==="canceled";class s{constructor(t,e,r,i,c){this._model=t,this.actionFunction=e,this.ownerGetter=r,this.setStateCb=i,this._validateArgs=c;const o=e.name;if(!(o in this._model&&typeof this._model[o]=="function"))throw new l(`Model does not contain method ${o}`);if(typeof e[s.actionFlag]!="function")throw new l(`Method ${o} is not action`);this.name=o}static actionFlag=Symbol("__action_original_method__");static possibleState={pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"};static abortedByLock=Symbol("lock");name;_state=s.possibleState.ready;_value=null;_args=null;static create(t,e,r,i,c){return h.shallowReactive(new s(t,e,r,i,c))}toString(){return this.name}get owner(){return this.ownerGetter()}get possibleStates(){return Object.values(s.possibleState)}get state(){return this._state}set state(t){const e=this._state;this._state=t,this.setStateCb(this,e,t)}get abortController(){return this.isPending?this._value.abortController:null}get args(){return this._args??[]}get promise(){return this.isPending?this._value.promise:null}get error(){return this.isError?this._value:null}get abortReason(){return this.isAbort?this._value.abortController.signal.reason:null}get isPending(){return this.state===s.possibleState.pending}get isError(){return this.state===s.possibleState.error}get isReady(){return this.state===s.possibleState.ready}get isLock(){return this.state===s.possibleState.lock}get isAbort(){return this.state===s.possibleState.abort}is(...t){return t.includes(this.state)}validate(...t){return this._validateArgs(this,...t)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new p(this.name,this.state,s.possibleState.pending);const e=[...t];let r=t.length&&t.at(-1);r instanceof AbortController||(r=new AbortController,e.push(r)),this.state=s.possibleState.pending,this._args=t;const c=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(c instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const o=c.then(()=>{this.ready()}).catch(n=>{if(n instanceof l||n instanceof RangeError||n instanceof ReferenceError||n instanceof SyntaxError||n instanceof TypeError||n instanceof URIError||n instanceof EvalError)throw n;const S=k(n);if(S&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new A(this.name,this.state);if(S&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(S){this.state=s.possibleState.abort;return}this.setError(new v(this.name,{cause:n}))});return this._value={promise:o,abortController:r},o}abort(t){return this.isPending?(this._value.abortController.abort(t),this._value.promise):Promise.resolve()}lock(){return this.isPending?this.abort(s.abortedByLock):(this.state=s.possibleState.lock,this._value=null,Promise.resolve())}unlock(){if(!this.isLock)throw new p(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new p(this.name,this.state,s.possibleState.error);return this.state=s.possibleState.error,this._value=t,this}ready(){return this.state=s.possibleState.ready,this}resetError(){if(!this.error)throw new p(this.name,this.state,s.possibleState.ready);return this.ready()}}const u=Symbol("scope"),b=Symbol("model"),g=Symbol("actions"),w=Symbol("actionIds"),m=Symbol("actionStates"),y=Symbol("actionsSize"),d=Symbol("watchStopHandlers"),M=Proxy;class f{[u]=h.effectScope(!0);[b]=null;[g]=new WeakMap;[w]=new WeakMap;[m]=new Map;[y]=0;[d]=new Set;static createModel(t){if(!(t instanceof f))throw new TypeError("ProtoModel instance is required");const e=new M(h.shallowReactive(t),{get(r,i,c){const o=Reflect.get(r,i,c),n=typeof o=="function";return n&&s.actionFlag in o&&typeof o[s.actionFlag]=="function"?r.action(o):n?o.bind(r):o}});return t[b]=e,e}static model(...t){if(this.prototype===f.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t);return f.createModel(e)}get hasPendingActions(){return!!this.getActionStatesRef(s.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(s.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[u].run(()=>h.watchEffect(t[0])):this[u].run(()=>h.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[d].add(e),()=>{e(),this[d].delete(e)}}computed(t,e){return this[u].run(()=>h.computed(t,e))}updateBit(t,e,r){const i=r?1:0,c=~(1<<e);return t&c|i<<e}createAction(t){const e=()=>{if(!this[b])throw new Error("Model not set");return this[b]},r=s.create(this,t,e,this.setActionState.bind(this),this.validateArgs.bind(this));return this[g].set(t,r),this[w].set(r,++this[y]),this.setActionState(r),r}getActionStatesRef(t){const e=this[m].get(t)||h.ref(0);return this[m].get(t)===void 0&&this[m].set(t,e),e}action(t){if(!(s.actionFlag in t&&typeof t[s.actionFlag]=="function"))throw new l("Action decorator is not applied to the method");return this[g].get(t)||this.createAction(t)}setActionState(t){const e=this[w].get(t);if(!e)throw new Error("Action not found");for(const r of t.possibleStates)r!==t.state&&(this.getActionStatesRef(r).value=this.updateBit(this.getActionStatesRef(r).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}validateArgs(t,...e){const r=`validateArgs is not implemented for action ${t.name} in the model`;return[new Error(r)]}isModelOf(t){return this instanceof t}destructor(){this[d].forEach(t=>{t()}),this[d]=new Set,this[u].stop()}}function P(a,t){if(t.static)throw new Error("Action decorator is not supported for static methods");if(t.private)throw new Error("Action decorator is not supported for private methods");let e;try{e=t.name.toString()}catch(i){const o=`Invalid context. Can\`t get name of the method: ${i instanceof Error?i.message:"unknown error"}`;throw new Error(o)}const r={[e]:function(...i){return this.action(r[e]).exec(...i)}};return r[e][s.actionFlag]=a,r[e]}exports.Action=s;exports.ActionError=v;exports.ActionInternalError=l;exports.ActionStatusConflictError=p;exports.ActionUnexpectedAbortError=A;exports.ProtoModel=f;exports.action=P;
package/dist/index.d.ts CHANGED
@@ -26,7 +26,7 @@ export declare class Action<T extends object, Args extends any[] = unknown[]> im
26
26
  readonly ready: "ready";
27
27
  readonly abort: "abort";
28
28
  };
29
- static abortedByLock: symbol;
29
+ static readonly abortedByLock: unique symbol;
30
30
  readonly name: string;
31
31
  protected _state: ActionStateName;
32
32
  protected _value: ActionValue;
package/dist/index.js CHANGED
@@ -80,9 +80,8 @@ class s {
80
80
  get abortController() {
81
81
  return this.isPending ? this._value.abortController : null;
82
82
  }
83
- // TODO: add tests
84
83
  get args() {
85
- return this._args || [];
84
+ return this._args ?? [];
86
85
  }
87
86
  get promise() {
88
87
  return this.isPending ? this._value.promise : null;
@@ -109,7 +108,7 @@ class s {
109
108
  return this.state === s.possibleState.abort;
110
109
  }
111
110
  is(...t) {
112
- return !!t.find((e) => this.state === e);
111
+ return t.includes(this.state);
113
112
  }
114
113
  validate(...t) {
115
114
  return this._validateArgs(this, ...t);
@@ -125,7 +124,7 @@ class s {
125
124
  s.possibleState.pending
126
125
  );
127
126
  const e = [...t];
128
- let r = t.length && t[t.length - 1];
127
+ let r = t.length && t.at(-1);
129
128
  r instanceof AbortController || (r = new AbortController(), e.push(r)), this.state = s.possibleState.pending, this._args = t;
130
129
  const c = this.actionFunction[s.actionFlag].apply(this._model, e);
131
130
  if (!(c instanceof Promise))
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@vue-modeler/model",
4
- "version": "2.2.5-beta.1",
4
+ "version": "2.2.5",
5
5
  "description": "A state management library based on models for Vue.js",
6
6
  "author": "abratko",
7
7
  "license": "MIT",