@vue-modeler/model 2.2.0-beta.2 → 2.2.0-beta.4
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 +1 -1
- package/dist/index.d.ts +14 -20
- package/dist/index.js +1 -0
- package/package.json +1 -1
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,
|
|
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,11 +12,11 @@ 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[]
|
|
15
|
+
export declare class Action<T extends object, Args extends any[] = unknown[]> implements ActionLike<T, Args> {
|
|
16
16
|
protected _model: T;
|
|
17
17
|
protected actionFunction: OriginalMethodWrapper<Args>;
|
|
18
|
-
protected ownerGetter: () =>
|
|
19
|
-
protected setStateCb?: ((action:
|
|
18
|
+
protected ownerGetter: () => Model<T>;
|
|
19
|
+
protected setStateCb?: ((action: ActionLike<T, Args>, oldState: ActionStateName, newState: ActionStateName) => void) | undefined;
|
|
20
20
|
static readonly actionFlag: unique symbol;
|
|
21
21
|
static readonly possibleState: {
|
|
22
22
|
readonly pending: "pending";
|
|
@@ -30,10 +30,10 @@ declare class Action<T extends object, Args extends any[] = unknown[], Owner ext
|
|
|
30
30
|
protected _state: ActionStateName;
|
|
31
31
|
protected _value: ActionValue;
|
|
32
32
|
protected _args: Args | null;
|
|
33
|
-
constructor(_model: T, actionFunction: OriginalMethodWrapper<Args>, ownerGetter: () =>
|
|
34
|
-
static create<T extends ProtoModel, Args extends unknown[] = unknown[]
|
|
33
|
+
constructor(_model: T, actionFunction: OriginalMethodWrapper<Args>, ownerGetter: () => Model<T>, setStateCb?: ((action: ActionLike<T, Args>, oldState: ActionStateName, newState: ActionStateName) => void) | undefined);
|
|
34
|
+
static create<T extends ProtoModel, Args extends unknown[] = unknown[]>(model: T, actionFunction: OriginalMethodWrapper<Args>, ownerGetter: () => Model<T>, setStateCb?: (action: ActionLike<T, Args>, oldState: ActionStateName, newState: ActionStateName) => void): ActionLike<T, Args>;
|
|
35
35
|
toString(): string;
|
|
36
|
-
get owner():
|
|
36
|
+
get owner(): Model<T>;
|
|
37
37
|
get possibleStates(): ActionStateName[];
|
|
38
38
|
get state(): ActionStateName;
|
|
39
39
|
protected set state(newState: ActionStateName);
|
|
@@ -106,9 +106,9 @@ 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<
|
|
109
|
+
export declare interface ActionLike<T extends object, Args extends any[] = unknown[]> {
|
|
110
110
|
readonly name: string;
|
|
111
|
-
readonly owner:
|
|
111
|
+
readonly owner: Model<T>;
|
|
112
112
|
readonly possibleStates: ActionStateName[];
|
|
113
113
|
readonly state: ActionStateName;
|
|
114
114
|
readonly abortController: null | AbortController;
|
|
@@ -181,7 +181,7 @@ declare type ActionValue = ActionPendingValue | ActionError | null;
|
|
|
181
181
|
declare function createModel<Target extends ProtoModel>(protoModel: Target): Model<Target>;
|
|
182
182
|
|
|
183
183
|
export declare type Model<T extends object = object> = ShallowReactive<{
|
|
184
|
-
[K in keyof T]: T[K] extends ((...args: infer Args) => Promise<void>) ?
|
|
184
|
+
[K in keyof T]: T[K] extends ((...args: infer Args) => Promise<void>) ? ActionLike<T, Args> : K extends ProtectedMethodInModel ? never : T[K];
|
|
185
185
|
}>;
|
|
186
186
|
|
|
187
187
|
export declare type ModelAdapterProxyConstructor = new <Target extends ProtoModel>(target: Target, handler: ProxyHandler<Target>) => Model<Target>;
|
|
@@ -204,8 +204,8 @@ export declare type ProtectedMethodInModel = 'action' | 'setActionState';
|
|
|
204
204
|
export declare abstract class ProtoModel {
|
|
205
205
|
protected [scopeKey]: EffectScope;
|
|
206
206
|
protected [modelKey]: Model<this> | null;
|
|
207
|
-
protected [actionsKey]: WeakMap<OriginalMethodWrapper<unknown[]>,
|
|
208
|
-
protected [actionIdsKey]: WeakMap<
|
|
207
|
+
protected [actionsKey]: WeakMap<OriginalMethodWrapper<unknown[]>, ActionLike<this, unknown[]>>;
|
|
208
|
+
protected [actionIdsKey]: WeakMap<ActionLike<this, unknown[]>, number>;
|
|
209
209
|
protected [actionStatesKey]: Map<"pending" | "error" | "lock" | "ready" | "abort", Ref<number>>;
|
|
210
210
|
protected [actionsSizeKey]: number;
|
|
211
211
|
protected [watchStopHandlersKey]: Set<WatchStopHandle>;
|
|
@@ -279,7 +279,7 @@ export declare abstract class ProtoModel {
|
|
|
279
279
|
protected watch(...args: unknown[]): WatchStopHandle;
|
|
280
280
|
protected computed<T>(getter: ComputedGetter<T>, debugOptions?: DebuggerOptions): ComputedRef<T>;
|
|
281
281
|
protected updateBit(number: number, bitPosition: number, bitValue: boolean): number;
|
|
282
|
-
protected createAction(actionFunction: OriginalMethodWrapper):
|
|
282
|
+
protected createAction(actionFunction: OriginalMethodWrapper): ActionLike<this>;
|
|
283
283
|
protected getActionStatesRef(stateName: ActionStateName): Ref<number>;
|
|
284
284
|
/**
|
|
285
285
|
* Gets Action instance by wrapped original method or create Action instance.
|
|
@@ -324,26 +324,20 @@ export declare abstract class ProtoModel {
|
|
|
324
324
|
* @param originalMethod - defined as OriginalMethod or OriginalMethodWrapper.
|
|
325
325
|
* @returns action
|
|
326
326
|
*/
|
|
327
|
-
protected action(originalMethod: OriginalMethod | OriginalMethodWrapper):
|
|
327
|
+
protected action(originalMethod: OriginalMethod | OriginalMethodWrapper): ActionLike<this>;
|
|
328
328
|
/**
|
|
329
329
|
* It is public method in context ProtoModel,
|
|
330
330
|
* but in Model<ProtoModel> context it is protected method
|
|
331
331
|
*
|
|
332
332
|
* @see type Model<T>
|
|
333
333
|
*/
|
|
334
|
-
setActionState(action:
|
|
334
|
+
setActionState(action: ActionLike<this>): void;
|
|
335
335
|
isModelOf<T extends ProtoModel>(typeModel: ModelConstructor_2<T>): boolean;
|
|
336
336
|
destructor(): void;
|
|
337
337
|
}
|
|
338
338
|
|
|
339
339
|
declare const scopeKey: unique symbol;
|
|
340
340
|
|
|
341
|
-
/**
|
|
342
|
-
* Reactive ActionLike type - ActionLike wrapped in ShallowReactive.
|
|
343
|
-
* This is the type used throughout the codebase for action instances.
|
|
344
|
-
*/
|
|
345
|
-
declare type SrActionLike<Owner extends object, Args extends any[] = unknown[]> = ShallowReactive<ActionLike<Owner, Args>>;
|
|
346
|
-
|
|
347
341
|
declare const watchStopHandlersKey: unique symbol;
|
|
348
342
|
|
|
349
343
|
export { }
|
package/dist/index.js
CHANGED