@artus/pipeline
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -9,3 +9,3 @@ import { BaseContext, BaseInput, BaseOutput, ContextStorage, ParamsDictionary, Next } from "./types"; | ||
export declare class Storage implements ContextStorage<any> { | ||
#private; | ||
private storageMap; | ||
get(key?: string | symbol): any; | ||
@@ -15,5 +15,5 @@ set(value: any, key?: string | symbol): void; | ||
export declare class Context implements BaseContext { | ||
#private; | ||
input: BaseInput; | ||
output: BaseOutput; | ||
private stogrageMap; | ||
constructor(input?: Input, output?: Output); | ||
@@ -20,0 +20,0 @@ namespace(namespace: string): ContextStorage<any>; |
"use strict"; | ||
var _Storage_storageMap, _Context_stogrageMap; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Context = exports.Storage = exports.Output = exports.Input = void 0; | ||
const tslib_1 = require("tslib"); | ||
const ContextStorageSymbol = Symbol('ARTUS::ContextStorage'); | ||
@@ -21,15 +19,14 @@ class Input { | ||
constructor() { | ||
_Storage_storageMap.set(this, new Map()); | ||
this.storageMap = new Map(); | ||
} | ||
get(key) { | ||
key !== null && key !== void 0 ? key : (key = ContextStorageSymbol); | ||
return tslib_1.__classPrivateFieldGet(this, _Storage_storageMap, "f").get(key); | ||
return this.storageMap.get(key); | ||
} | ||
set(value, key) { | ||
key !== null && key !== void 0 ? key : (key = ContextStorageSymbol); | ||
tslib_1.__classPrivateFieldGet(this, _Storage_storageMap, "f").set(key, value); | ||
this.storageMap.set(key, value); | ||
} | ||
} | ||
exports.Storage = Storage; | ||
_Storage_storageMap = new WeakMap(); | ||
class Context { | ||
@@ -39,3 +36,3 @@ constructor(input, output) { | ||
this.output = new Output(); | ||
_Context_stogrageMap.set(this, new Map()); | ||
this.stogrageMap = new Map(); | ||
this.input = input !== null && input !== void 0 ? input : this.input; | ||
@@ -45,6 +42,6 @@ this.output = output !== null && output !== void 0 ? output : this.output; | ||
namespace(namespace) { | ||
let storage = tslib_1.__classPrivateFieldGet(this, _Context_stogrageMap, "f").get(namespace); | ||
let storage = this.stogrageMap.get(namespace); | ||
if (!storage) { | ||
storage = new Storage(); | ||
tslib_1.__classPrivateFieldGet(this, _Context_stogrageMap, "f").set(namespace, storage); | ||
this.stogrageMap.set(namespace, storage); | ||
} | ||
@@ -56,3 +53,2 @@ return storage; | ||
exports.Context = Context; | ||
_Context_stogrageMap = new WeakMap(); | ||
//# sourceMappingURL=base.js.map |
import { Context, Middlewares, MiddlewareInput, PipelineLike } from "./base"; | ||
export declare class Pipeline implements PipelineLike { | ||
#private; | ||
middlewares: Middlewares; | ||
use(middleware: MiddlewareInput): void; | ||
dispatch(i: number, ctx: Context, execution?: { | ||
index: number; | ||
}): Promise<any>; | ||
run(ctx: Context): Promise<any>; | ||
} |
"use strict"; | ||
var _Pipeline_instances, _Pipeline_index, _Pipeline_dispatch; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Pipeline = void 0; | ||
const tslib_1 = require("tslib"); | ||
class Pipeline { | ||
constructor() { | ||
_Pipeline_instances.add(this); | ||
this.middlewares = []; | ||
_Pipeline_index.set(this, -1); | ||
} | ||
@@ -30,21 +26,21 @@ use(middleware) { | ||
} | ||
dispatch(i, ctx, execution = { index: -1 }) { | ||
if (i <= execution.index) | ||
return Promise.reject(new Error('next() called multiple times')); | ||
execution.index = i; | ||
let fn = this.middlewares[i]; | ||
if (!fn) | ||
return Promise.resolve(); | ||
try { | ||
return Promise.resolve(fn(ctx, this.dispatch.bind(this, i + 1, ctx, execution))); | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
} | ||
run(ctx) { | ||
return tslib_1.__classPrivateFieldGet(this, _Pipeline_instances, "m", _Pipeline_dispatch).call(this, 0, ctx); | ||
return this.dispatch(0, ctx); | ||
} | ||
} | ||
exports.Pipeline = Pipeline; | ||
_Pipeline_index = new WeakMap(), _Pipeline_instances = new WeakSet(), _Pipeline_dispatch = function _Pipeline_dispatch(i, ctx) { | ||
if (i <= tslib_1.__classPrivateFieldGet(this, _Pipeline_index, "f")) | ||
return Promise.reject(new Error('next() called multiple times')); | ||
tslib_1.__classPrivateFieldSet(this, _Pipeline_index, i, "f"); | ||
let fn = this.middlewares[i]; | ||
if (!fn) | ||
return Promise.resolve(); | ||
try { | ||
return Promise.resolve(fn(ctx, tslib_1.__classPrivateFieldGet(this, _Pipeline_instances, "m", _Pipeline_dispatch).bind(this, i + 1, ctx))); | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
}; | ||
//# sourceMappingURL=pipeline.js.map |
{ | ||
"name": "@artus/pipeline", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -6,3 +6,2 @@ | ||
middlewares: Middlewares = []; | ||
private index: number = -1; | ||
@@ -31,9 +30,9 @@ use(middleware: MiddlewareInput) { | ||
dispatch(i: number, ctx: Context): Promise<any> { | ||
if (i <= this.index) return Promise.reject(new Error('next() called multiple times')); | ||
this.index = i; | ||
dispatch(i: number, ctx: Context, execution = { index: -1 }): Promise<any> { | ||
if (i <= execution.index) return Promise.reject(new Error('next() called multiple times')); | ||
execution.index = i; | ||
let fn = this.middlewares[i]; | ||
if (!fn) return Promise.resolve(); | ||
try { | ||
return Promise.resolve(fn(ctx, this.dispatch.bind(this, i + 1, ctx))); | ||
return Promise.resolve(fn(ctx, this.dispatch.bind(this, i + 1, ctx, execution))); | ||
} catch (err: any) { | ||
@@ -40,0 +39,0 @@ return Promise.reject(err); |
@@ -27,2 +27,3 @@ import assert from 'assert'; | ||
await pipeline.run(ctx); | ||
await pipeline.run(ctx); | ||
@@ -29,0 +30,0 @@ const { data } = ctx.output; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18796
416