command-action
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -47,2 +47,5 @@ "use strict"; | ||
this._context._executed(this); | ||
if (error instanceof Errors_1.SkipActionAndSequenceError) { | ||
return; | ||
} | ||
yield this._context._rollback(); | ||
@@ -49,0 +52,0 @@ if (error instanceof Context_1.ContextClass) { |
@@ -13,2 +13,3 @@ import { PropsType, ActionInstance } from '../types'; | ||
private _failureMeta; | ||
private _sequence; | ||
constructor(context: Props); | ||
@@ -20,5 +21,8 @@ static build<Props extends PropsType>(props?: Context<Props> | Props): Context<Props> & Props; | ||
fail({ type, message, force, ...rest }: FailParams): never; | ||
skip(): void; | ||
_executed(action: ActionInstance): void; | ||
_rollback(): Promise<boolean>; | ||
private _setSequence; | ||
private _removeSequence; | ||
} | ||
export default Context; |
@@ -30,2 +30,3 @@ "use strict"; | ||
this._rolledback = false; | ||
this._sequence = null; | ||
Object.assign(this, context); | ||
@@ -65,2 +66,8 @@ } | ||
} | ||
skip() { | ||
if (this._sequence) { | ||
this._sequence.skip = true; | ||
} | ||
throw new Errors_1.SkipActionAndSequenceError(); | ||
} | ||
_executed(action) { | ||
@@ -81,3 +88,9 @@ this._actions.push(action); | ||
} | ||
_setSequence(sequence) { | ||
this._sequence = sequence; | ||
} | ||
_removeSequence() { | ||
this._sequence = null; | ||
} | ||
} | ||
exports.default = Context; |
export { default as InternalActionError } from './InternalActionError'; | ||
export { default as ForcedActionError } from './ForcedActionError'; | ||
export { default as SkipActionAndSequenceError } from './SkipActionAndSequenceError'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ForcedActionError = exports.InternalActionError = void 0; | ||
exports.SkipActionAndSequenceError = exports.ForcedActionError = exports.InternalActionError = void 0; | ||
var InternalActionError_1 = require("./InternalActionError"); | ||
@@ -8,1 +8,3 @@ Object.defineProperty(exports, "InternalActionError", { enumerable: true, get: function () { return InternalActionError_1.default; } }); | ||
Object.defineProperty(exports, "ForcedActionError", { enumerable: true, get: function () { return ForcedActionError_1.default; } }); | ||
var SkipActionAndSequenceError_1 = require("./SkipActionAndSequenceError"); | ||
Object.defineProperty(exports, "SkipActionAndSequenceError", { enumerable: true, get: function () { return SkipActionAndSequenceError_1.default; } }); |
import { ActionClass, ActionMethod, Context, PropsType } from '../types'; | ||
declare class Sequence<Props extends PropsType, Result extends Props = Props> { | ||
protected actions: ActionClass[]; | ||
skip: boolean; | ||
static run<Props extends PropsType, Result extends Props = Props>(context: Props): Promise<Context<Result>>; | ||
@@ -5,0 +6,0 @@ static method<Props extends PropsType, Result extends Props = Props>(): ActionMethod<Props, Result>; |
@@ -12,5 +12,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Context_1 = require("../Context"); | ||
class Sequence { | ||
constructor() { | ||
this.actions = []; | ||
this.skip = false; | ||
} | ||
@@ -29,8 +31,11 @@ static run(context) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
context = Context_1.ContextClass.build(context); | ||
context._setSequence(this); | ||
for (let action of this.actions) { | ||
context = yield action.run(context); | ||
if (context.isFailure()) { | ||
if (context.isFailure() || this.skip) { | ||
break; | ||
} | ||
} | ||
context._removeSequence(); | ||
return context; | ||
@@ -37,0 +42,0 @@ }); |
@@ -5,3 +5,3 @@ import { ContextClass } from './Context'; | ||
export type ActionMethod<Props extends PropsType, Result extends Props = Props> = (context: Props) => Promise<Context<Result> & Result>; | ||
export type Context<Props extends PropsType> = Omit<ContextClass<Props>, '_executed' | '_rollback'> & Props & Record<string, any>; | ||
export type Context<Props extends PropsType> = Omit<ContextClass<Props>, '_executed' | '_rollback' | '_sequence'> & Props & Record<string, any>; | ||
export type ActionInstance = { | ||
@@ -8,0 +8,0 @@ rollback(): Promise<void> | void; |
{ | ||
"name": "command-action", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,3 +5,3 @@ import { Command } from '../instance' | ||
import { ActionInstance, Context, PropsType, ActionMethod, ActionHook } from '../types' | ||
import { ForcedActionError, InternalActionError } from '../Errors' | ||
import { ForcedActionError, InternalActionError, SkipActionAndSequenceError } from '../Errors' | ||
@@ -46,2 +46,5 @@ abstract class Action<Props extends PropsType = {}, Result extends Props = Props, Provider extends PropsType = {}> implements ActionInstance { | ||
this._context._executed(this) | ||
if (error instanceof SkipActionAndSequenceError) { | ||
return | ||
} | ||
await this._context._rollback() | ||
@@ -56,3 +59,3 @@ if (error instanceof ContextClass) { | ||
throw new InternalActionError<Props & Result>(error as Error, this._context) | ||
throw new InternalActionError<Props & Result>(error as Error, this._context as ContextClass<Props & Result>) | ||
} | ||
@@ -59,0 +62,0 @@ } |
import { Command } from '../instance' | ||
import { PropsType, ActionInstance, FailureType } from '../types' | ||
import { ForcedActionError } from '../Errors' | ||
import { ForcedActionError, SkipActionAndSequenceError } from '../Errors' | ||
import { Sequence } from '../Sequence' | ||
@@ -18,2 +19,3 @@ interface FailureMeta extends Record<string, any> { | ||
private _failureMeta!: FailureMeta | ||
private _sequence: Sequence<Props> | null = null | ||
@@ -65,2 +67,9 @@ constructor(context: Props) { | ||
public skip() { | ||
if (this._sequence) { | ||
this._sequence.skip = true | ||
} | ||
throw new SkipActionAndSequenceError() | ||
} | ||
public _executed(action: ActionInstance): void { | ||
@@ -83,2 +92,10 @@ this._actions.push(action) | ||
} | ||
private _setSequence(sequence: Sequence<Props>) { | ||
this._sequence = sequence | ||
} | ||
private _removeSequence() { | ||
this._sequence = null | ||
} | ||
} | ||
@@ -85,0 +102,0 @@ |
export { default as InternalActionError } from './InternalActionError' | ||
export { default as ForcedActionError } from './ForcedActionError' | ||
export { default as SkipActionAndSequenceError } from './SkipActionAndSequenceError' |
@@ -0,1 +1,2 @@ | ||
import { ContextClass } from '../Context' | ||
import { ActionClass, ActionMethod, Context, PropsType } from '../types' | ||
@@ -5,2 +6,3 @@ | ||
protected actions: ActionClass[] = [] | ||
public skip: boolean = false | ||
@@ -18,8 +20,11 @@ public static async run<Props extends PropsType, Result extends Props = Props>(context: Props) { | ||
private async run(context: Context<Props> | Props): Promise<Context<Result>> { | ||
context = ContextClass.build<Props>(context) | ||
context._setSequence(this) | ||
for (let action of this.actions) { | ||
context = await action.run(context) | ||
if (context.isFailure()) { | ||
if (context.isFailure() || this.skip) { | ||
break | ||
} | ||
} | ||
context._removeSequence() | ||
return context as Context<Result> | ||
@@ -26,0 +31,0 @@ } |
@@ -11,3 +11,3 @@ import { ContextClass } from './Context' | ||
export type Context<Props extends PropsType> = | ||
Omit<ContextClass<Props>, '_executed' | '_rollback'> & Props & Record<string, any> | ||
Omit<ContextClass<Props>, '_executed' | '_rollback' | '_sequence'> & Props & Record<string, any> | ||
@@ -14,0 +14,0 @@ export type ActionInstance = { |
@@ -7,3 +7,3 @@ { | ||
"strict": true, | ||
"lib": ["es2015"], | ||
"lib": ["es2015", "DOM"], | ||
"outDir": "lib" | ||
@@ -10,0 +10,0 @@ }, |
38587
69
1036