@vue-modeler/model 2.2.0-beta.2 → 2.2.0-beta.3

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.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var L=Object.defineProperty;var $=(r,t,e)=>t in r?L(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>$(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue");class I 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 u extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class b extends u{constructor(t,e,o){super(`Trying to update state of ${t} from ${e} to ${o}`),this.name=this.constructor.name}}class K extends u{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const B=r=>r instanceof DOMException&&r.name==="AbortError"||typeof r=="object"&&r!==null&&"message"in r&&r.message==="canceled",s=class s{constructor(t,e,o,c){i(this,"name");i(this,"_state",s.possibleState.ready);i(this,"_value",null);i(this,"_args",null);this._model=t,this.actionFunction=e,this.ownerGetter=o,this.setStateCb=c;const n=e.name;if(!(n in this._model&&typeof this._model[n]=="function"))throw new u(`Model does not contain method ${n}`);if(typeof e[s.actionFlag]!="function")throw new u(`Method ${n} is not action`);this.name=n}static create(t,e,o,c){return l.shallowReactive(new s(t,e,o,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.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)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new b(this.name,this.state,s.possibleState.pending);const e=[...t];let o=t.length&&t[t.length-1];o instanceof AbortController||(o=new AbortController,e.push(o)),this.state=s.possibleState.pending,this._args=t;const n=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(n instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const d=n.then(()=>{this.ready()}).catch(a=>{if(a instanceof u||a instanceof RangeError||a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError||a instanceof URIError||a instanceof EvalError)throw a;const y=B(a);if(y&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new K(this.name,this.state);if(y&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(y){this.state=s.possibleState.abort;return}this.setError(new I(this.name,{cause:a}))});return this._value={promise:d,abortController:o},d}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 b(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new b(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 b(this.name,this.state,s.possibleState.ready);return this.ready()}};i(s,"actionFlag",Symbol("__action_original_method__")),i(s,"possibleState",{pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"}),i(s,"abortedByLock",Symbol("lock"));let h=s;const T=Proxy;function D(r){if(!(r instanceof g))throw new Error("ProtoModel instance is required");return new T(l.shallowReactive(r),{get(e,o,c){const n=Reflect.get(e,o,c),d=typeof n=="function";return d&&h.actionFlag in n&&typeof n[h.actionFlag]=="function"?e.action(n):d?n.bind(e):n}})}const p=Symbol("scope"),m=Symbol("model"),A=Symbol("actions"),v=Symbol("actionIds"),w=Symbol("actionStates"),E=Symbol("actionsSize"),f=Symbol("watchStopHandlers");var k,M,P,C,R,x,F;F=p,x=m,R=A,C=v,P=w,M=E,k=f;const S=class S{constructor(){i(this,F,l.effectScope(!0));i(this,x,null);i(this,R,new WeakMap);i(this,C,new WeakMap);i(this,P,new Map);i(this,M,0);i(this,k,new Set)}static model(...t){if(this.prototype===S.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t),o=S.createModel(e);return e[m]=o,o}get hasPendingActions(){return!!this.getActionStatesRef(h.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(h.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[p].run(()=>l.watchEffect(t[0])):this[p].run(()=>l.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[f].add(e),()=>{e(),this[f].delete(e)}}computed(t,e){return this[p].run(()=>l.computed(t,e))}updateBit(t,e,o){const c=o?1:0,n=~(1<<e);return t&n|c<<e}createAction(t){const e=()=>{if(!this[m])throw new Error("Model not set");return this[m]},o=h.create(this,t,e,this.setActionState.bind(this));return this[A].set(t,o),this[v].set(o,++this[E]),this.setActionState(o),o}getActionStatesRef(t){const e=this[w].get(t)||l.ref(0);return this[w].get(t)===void 0&&this[w].set(t,e),e}action(t){if(!(h.actionFlag in t&&typeof t[h.actionFlag]=="function"))throw new u("Action decorator is not applied to the method");return this[A].get(t)||this.createAction(t)}setActionState(t){const e=this[v].get(t);if(!e)throw new Error("Action not found");for(const o of t.possibleStates)o!==t.state&&(this.getActionStatesRef(o).value=this.updateBit(this.getActionStatesRef(o).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}isModelOf(t){return this instanceof t}destructor(){this[f].forEach(t=>{t()}),this[f]=new Set,this[p].stop()}};i(S,"createModel",D);let g=S;function O(r,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");const e=t.name.toString(),o={[e]:function(...c){return this.action(o[e]).exec(...c)}};return o[e][h.actionFlag]=r,o[e]}exports.ActionError=I;exports.ActionInternalError=u;exports.ActionStatusConflictError=b;exports.ActionUnexpectedAbortError=K;exports.ProtoModel=g;exports.action=O;
1
+ "use strict";var L=Object.defineProperty;var $=(r,t,e)=>t in r?L(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>$(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue");class I 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 u extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class b extends u{constructor(t,e,o){super(`Trying to update state of ${t} from ${e} to ${o}`),this.name=this.constructor.name}}class K extends u{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const B=r=>r instanceof DOMException&&r.name==="AbortError"||typeof r=="object"&&r!==null&&"message"in r&&r.message==="canceled",s=class s{constructor(t,e,o,h){i(this,"name");i(this,"_state",s.possibleState.ready);i(this,"_value",null);i(this,"_args",null);this._model=t,this.actionFunction=e,this.ownerGetter=o,this.setStateCb=h;const n=e.name;if(!(n in this._model&&typeof this._model[n]=="function"))throw new u(`Model does not contain method ${n}`);if(typeof e[s.actionFlag]!="function")throw new u(`Method ${n} is not action`);this.name=n}static create(t,e,o,h){return l.shallowReactive(new s(t,e,o,h))}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.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)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new b(this.name,this.state,s.possibleState.pending);const e=[...t];let o=t.length&&t[t.length-1];o instanceof AbortController||(o=new AbortController,e.push(o)),this.state=s.possibleState.pending,this._args=t;const n=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(n instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const d=n.then(()=>{this.ready()}).catch(a=>{if(a instanceof u||a instanceof RangeError||a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError||a instanceof URIError||a instanceof EvalError)throw a;const y=B(a);if(y&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new K(this.name,this.state);if(y&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(y){this.state=s.possibleState.abort;return}this.setError(new I(this.name,{cause:a}))});return this._value={promise:d,abortController:o},d}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 b(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new b(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 b(this.name,this.state,s.possibleState.ready);return this.ready()}};i(s,"actionFlag",Symbol("__action_original_method__")),i(s,"possibleState",{pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"}),i(s,"abortedByLock",Symbol("lock"));let c=s;const T=Proxy;function D(r){if(!(r instanceof g))throw new Error("ProtoModel instance is required");return new T(l.shallowReactive(r),{get(e,o,h){const n=Reflect.get(e,o,h),d=typeof n=="function";return d&&c.actionFlag in n&&typeof n[c.actionFlag]=="function"?e.action(n):d?n.bind(e):n}})}const p=Symbol("scope"),m=Symbol("model"),A=Symbol("actions"),v=Symbol("actionIds"),w=Symbol("actionStates"),E=Symbol("actionsSize"),f=Symbol("watchStopHandlers");var k,M,P,C,R,x,F;F=p,x=m,R=A,C=v,P=w,M=E,k=f;const S=class S{constructor(){i(this,F,l.effectScope(!0));i(this,x,null);i(this,R,new WeakMap);i(this,C,new WeakMap);i(this,P,new Map);i(this,M,0);i(this,k,new Set)}static model(...t){if(this.prototype===S.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t),o=S.createModel(e);return e[m]=o,o}get hasPendingActions(){return!!this.getActionStatesRef(c.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(c.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[p].run(()=>l.watchEffect(t[0])):this[p].run(()=>l.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[f].add(e),()=>{e(),this[f].delete(e)}}computed(t,e){return this[p].run(()=>l.computed(t,e))}updateBit(t,e,o){const h=o?1:0,n=~(1<<e);return t&n|h<<e}createAction(t){const e=()=>{if(!this[m])throw new Error("Model not set");return this[m]},o=c.create(this,t,e,this.setActionState.bind(this));return this[A].set(t,o),this[v].set(o,++this[E]),this.setActionState(o),o}getActionStatesRef(t){const e=this[w].get(t)||l.ref(0);return this[w].get(t)===void 0&&this[w].set(t,e),e}action(t){if(!(c.actionFlag in t&&typeof t[c.actionFlag]=="function"))throw new u("Action decorator is not applied to the method");return this[A].get(t)||this.createAction(t)}setActionState(t){const e=this[v].get(t);if(!e)throw new Error("Action not found");for(const o of t.possibleStates)o!==t.state&&(this.getActionStatesRef(o).value=this.updateBit(this.getActionStatesRef(o).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}isModelOf(t){return this instanceof t}destructor(){this[f].forEach(t=>{t()}),this[f]=new Set,this[p].stop()}};i(S,"createModel",D);let g=S;function O(r,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");const e=t.name.toString(),o={[e]:function(...h){return this.action(o[e]).exec(...h)}};return o[e][c.actionFlag]=r,o[e]}exports.Action=c;exports.ActionError=I;exports.ActionInternalError=u;exports.ActionStatusConflictError=b;exports.ActionUnexpectedAbortError=K;exports.ProtoModel=g;exports.action=O;
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ import { WatchStopHandle } from 'vue';
12
12
  * For example, `setActionState` method.
13
13
  * @see `ProtoModel.setActionState`
14
14
  */
15
- declare class Action<T extends object, Args extends any[] = unknown[], Owner extends object = Model<T>> implements ActionLike<Owner, Args> {
15
+ export declare class Action<T extends object, Args extends any[] = unknown[], Owner extends object = Model<T>> implements ActionLike<Owner, Args> {
16
16
  protected _model: T;
17
17
  protected actionFunction: OriginalMethodWrapper<Args>;
18
18
  protected ownerGetter: () => Owner;
@@ -106,7 +106,7 @@ export declare class ActionInternalError extends Error {
106
106
  * Public API interface for Action instances.
107
107
  * Describes only the public contract without implementation details.
108
108
  */
109
- declare interface ActionLike<Owner extends object, Args extends any[] = unknown[]> {
109
+ export declare interface ActionLike<Owner extends object, Args extends any[] = unknown[]> {
110
110
  readonly name: string;
111
111
  readonly owner: Owner;
112
112
  readonly possibleStates: ActionStateName[];
@@ -342,7 +342,7 @@ declare const scopeKey: unique symbol;
342
342
  * Reactive ActionLike type - ActionLike wrapped in ShallowReactive.
343
343
  * This is the type used throughout the codebase for action instances.
344
344
  */
345
- declare type SrActionLike<Owner extends object, Args extends any[] = unknown[]> = ShallowReactive<ActionLike<Owner, Args>>;
345
+ export declare type SrActionLike<Owner extends object, Args extends any[] = unknown[]> = ShallowReactive<ActionLike<Owner, Args>>;
346
346
 
347
347
  declare const watchStopHandlersKey: unique symbol;
348
348
 
package/dist/index.js CHANGED
@@ -415,6 +415,7 @@ function V(r, t) {
415
415
  return o[e][h.actionFlag] = r, o[e];
416
416
  }
417
417
  export {
418
+ h as Action,
418
419
  H as ActionError,
419
420
  u as ActionInternalError,
420
421
  b as ActionStatusConflictError,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@vue-modeler/model",
4
- "version": "2.2.0-beta.2",
4
+ "version": "2.2.0-beta.3",
5
5
  "description": "A state management library based on models for Vue.js",
6
6
  "author": "abratko",
7
7
  "license": "MIT",