@artus/pipeline
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -25,7 +25,7 @@ import type { ExecutionContainer } from '@artus/injection'; | ||
} | ||
export declare type Middleware<T extends BaseContext = any> = (context: T, next: Next) => void; | ||
export declare type Middlewares = Middleware[]; | ||
export declare type PipelineLike = { | ||
export type Middleware<T extends BaseContext = any> = (context: T, next: Next) => void; | ||
export type Middlewares = Middleware[]; | ||
export type PipelineLike = { | ||
middlewares: Middlewares; | ||
}; | ||
export declare type MiddlewareInput = Middleware | Middlewares | PipelineLike; | ||
export type MiddlewareInput = Middleware | Middlewares | PipelineLike; |
@@ -30,3 +30,3 @@ "use strict"; | ||
execution.index = i; | ||
let fn = this.middlewares[i]; | ||
const fn = this.middlewares[i]; | ||
if (!fn) | ||
@@ -33,0 +33,0 @@ return Promise.resolve(); |
@@ -14,3 +14,3 @@ "use strict"; | ||
get() { | ||
let current = this.head; | ||
const current = this.head; | ||
if (current[next]) { | ||
@@ -17,0 +17,0 @@ this.head = current[next]; |
@@ -16,6 +16,6 @@ export interface ParamsDictionary { | ||
} | ||
export declare type ContextStorage<T> = { | ||
export type ContextStorage<T> = { | ||
set: (value: T, key?: string | symbol) => void; | ||
get: (key?: string | symbol) => T; | ||
}; | ||
export declare type Next = () => Promise<any>; | ||
export type Next = () => Promise<any>; |
{ | ||
"name": "@artus/pipeline", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"main": "dist/index.js", | ||
@@ -9,6 +9,9 @@ "scripts": { | ||
"test": "tsc --version && jest --coverage", | ||
"ci": "npm run test" | ||
"ci": "npm run lint && npm run test", | ||
"lint:fix": "eslint . --ext .ts --fix", | ||
"lint": "eslint . --ext .ts" | ||
}, | ||
"devDependencies": { | ||
"@artus/injection": "^0.2.0", | ||
"@artus/tsconfig": "0.0.1", | ||
"@types/jest": "^27.4.1", | ||
@@ -18,2 +21,5 @@ "egg-ci": "^1.19.0", | ||
"ts-jest": "^27.1.3", | ||
"@artus/eslint-config-artus": "0.0.1", | ||
"eslint": "^8.19.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"typescript": "^4.6.2" | ||
@@ -30,3 +36,4 @@ }, | ||
"version": "16" | ||
} | ||
}, | ||
"license": "MIT" | ||
} |
@@ -7,23 +7,23 @@ import { Reusify } from "./reusify"; | ||
export class ContextPool { | ||
private pool: Reusify<BaseContext>; | ||
private contextFactory: () => BaseContext; | ||
constructor(max?: number, ctxFactory?: () => BaseContext) { | ||
this.contextFactory = ctxFactory ?? (() => { | ||
return new Context(); | ||
}); | ||
this.pool = new Reusify({ max: max || 100, factory: this.contextFactory }); | ||
} | ||
private pool: Reusify<BaseContext>; | ||
private contextFactory: () => BaseContext; | ||
constructor(max?: number, ctxFactory?: () => BaseContext) { | ||
this.contextFactory = ctxFactory ?? (() => { | ||
return new Context(); | ||
}); | ||
this.pool = new Reusify({ max: max || 100, factory: this.contextFactory }); | ||
} | ||
get(container: ExecutionContainer, input?: BaseInput, output?: BaseOutput): BaseContext { | ||
const ctx = this.pool.get(); | ||
ctx.container = container; | ||
ctx.input = input ?? ctx.input; | ||
ctx.output = output ?? ctx.output; | ||
return ctx; | ||
} | ||
get(container: ExecutionContainer, input?: BaseInput, output?: BaseOutput): BaseContext { | ||
const ctx = this.pool.get(); | ||
ctx.container = container; | ||
ctx.input = input ?? ctx.input; | ||
ctx.output = output ?? ctx.output; | ||
return ctx; | ||
} | ||
release(ctx: BaseContext) { | ||
ctx.restore?.(); | ||
this.pool.release(ctx); | ||
} | ||
release(ctx: BaseContext) { | ||
ctx.restore?.(); | ||
this.pool.release(ctx); | ||
} | ||
} |
@@ -32,3 +32,3 @@ import { Middlewares, MiddlewareInput, PipelineLike } from './base'; | ||
execution.index = i; | ||
let fn = this.middlewares[i]; | ||
const fn = this.middlewares[i]; | ||
if (!fn) return Promise.resolve(); | ||
@@ -35,0 +35,0 @@ try { |
@@ -5,48 +5,48 @@ | ||
export interface ReusifyOptions<T> { | ||
max: number; | ||
factory: () => T; | ||
max: number; | ||
factory: () => T; | ||
} | ||
export class Reusify<T = any> { | ||
public max: number; | ||
public count: number; | ||
public max: number; | ||
public count: number; | ||
private head: T; | ||
private tail: T; | ||
private factory: () => T; | ||
private head: T; | ||
private tail: T; | ||
private factory: () => T; | ||
constructor(options: ReusifyOptions<T>) { | ||
this.factory = options.factory; | ||
this.max = options.max; | ||
this.count = 0; | ||
this.head = this.getInstance(); | ||
this.tail = this.head; | ||
} | ||
constructor(options: ReusifyOptions<T>) { | ||
this.factory = options.factory; | ||
this.max = options.max; | ||
this.count = 0; | ||
this.head = this.getInstance(); | ||
this.tail = this.head; | ||
} | ||
public get() { | ||
let current = this.head | ||
if (current[next]) { | ||
this.head = current[next]; | ||
} else { | ||
this.head = this.getInstance(); | ||
this.tail = this.head; | ||
} | ||
current[next] = null; | ||
this.count--; | ||
return current; | ||
public get() { | ||
const current = this.head; | ||
if (current[next]) { | ||
this.head = current[next]; | ||
} else { | ||
this.head = this.getInstance(); | ||
this.tail = this.head; | ||
} | ||
current[next] = null; | ||
this.count--; | ||
return current; | ||
} | ||
public release(obj: T) { | ||
if (this.count >= this.max) { | ||
return; | ||
} | ||
this.tail[next] = obj; | ||
this.tail = obj; | ||
this.count++; | ||
public release(obj: T) { | ||
if (this.count >= this.max) { | ||
return; | ||
} | ||
this.tail[next] = obj; | ||
this.tail = obj; | ||
this.count++; | ||
} | ||
private getInstance() { | ||
this.count++; | ||
return this.factory(); | ||
} | ||
private getInstance() { | ||
this.count++; | ||
return this.factory(); | ||
} | ||
} |
@@ -52,3 +52,3 @@ import assert from 'assert'; | ||
data.set('responseValue', 1); | ||
} | ||
}, | ||
]); | ||
@@ -96,3 +96,3 @@ | ||
pipeline.use([ | ||
async function (ctx: Context, next: Next): Promise<void> { | ||
async function (_: Context, next: Next): Promise<void> { | ||
await next(); | ||
@@ -105,3 +105,3 @@ await next(); | ||
data.set('responseValue', 1); | ||
} | ||
}, | ||
]); | ||
@@ -123,3 +123,3 @@ | ||
pipeline.use([ | ||
async function (ctx: Context, next: Next): Promise<void> { | ||
async function (_: Context, next: Next): Promise<void> { | ||
await next(); | ||
@@ -132,3 +132,3 @@ throw new Error('mock error'); | ||
data.set('responseValue', 1); | ||
} | ||
}, | ||
]); | ||
@@ -135,0 +135,0 @@ |
import { Reusify } from '../src/reusify'; | ||
describe("test/reusify.test.ts", () => { | ||
it("should init ok", () => { | ||
const pool = new Reusify({ max: 2, factory: jest.fn(() => ({})) }); | ||
expect(pool.max).toBe(2); | ||
expect(pool.count).toBe(1); | ||
}); | ||
it("should init ok", () => { | ||
const pool = new Reusify({ max: 2, factory: jest.fn(() => ({})) }); | ||
expect(pool.max).toBe(2); | ||
expect(pool.count).toBe(1); | ||
}); | ||
it('should get ok', () => { | ||
class A { } | ||
const pool = new Reusify({ | ||
max: 1, | ||
factory: jest.fn(() => { | ||
return new A(); | ||
}) | ||
}); | ||
expect(pool.get()).toBeInstanceOf(A); | ||
expect(pool.count).toBe(1); | ||
it('should get ok', () => { | ||
class A { } | ||
const pool = new Reusify({ | ||
max: 1, | ||
factory: jest.fn(() => { | ||
return new A(); | ||
}), | ||
}); | ||
expect(pool.get()).toBeInstanceOf(A); | ||
expect(pool.count).toBe(1); | ||
}); | ||
it('should release ok', () => { | ||
const pool = new Reusify({ | ||
max: 2, | ||
factory: jest.fn(() => ({})) | ||
}); | ||
const obj2 = pool.get(); | ||
pool.release(obj2); | ||
expect(pool.count).toBe(2); | ||
it('should release ok', () => { | ||
const pool = new Reusify({ | ||
max: 2, | ||
factory: jest.fn(() => ({})), | ||
}); | ||
const obj2 = pool.get(); | ||
pool.release(obj2); | ||
expect(pool.count).toBe(2); | ||
}); | ||
it('should release fail when count greater than max', () => { | ||
const pool = new Reusify({ max: 2, factory: jest.fn(() => ({})) }); | ||
const obj1 = pool.get(); | ||
const obj2 = pool.get(); | ||
const obj3 = pool.get(); | ||
pool.release(obj1); | ||
pool.release(obj2); | ||
pool.release(obj3); | ||
expect(pool.count).toBe(2); | ||
pool.get(); | ||
expect(pool.count).toBe(1); | ||
}); | ||
}) | ||
it('should release fail when count greater than max', () => { | ||
const pool = new Reusify({ max: 2, factory: jest.fn(() => ({})) }); | ||
const obj1 = pool.get(); | ||
const obj2 = pool.get(); | ||
const obj3 = pool.get(); | ||
pool.release(obj1); | ||
pool.release(obj2); | ||
pool.release(obj3); | ||
expect(pool.count).toBe(2); | ||
pool.get(); | ||
expect(pool.count).toBe(1); | ||
}); | ||
}); |
{ | ||
"extends": "@artus/tsconfig", | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"declaration": true, | ||
"downlevelIteration": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"importHelpers": true, | ||
"module": "CommonJS", | ||
"moduleResolution": "node", | ||
"newLine": "LF", | ||
"allowJs": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"suppressImplicitAnyIndexErrors": true, | ||
"suppressExcessPropertyErrors": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"target": "ES2017", | ||
"sourceMap": true, | ||
"esModuleInterop": true, | ||
"stripInternal": true, | ||
"lib": [ | ||
@@ -23,0 +5,0 @@ "ESNext" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37
30837
10
653