Skip to content

Commit

Permalink
refactor: open context extend (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrysShan authored May 26, 2022
1 parent 3e638ba commit 018384b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
13 changes: 5 additions & 8 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { ExecutionContainer } from '@artus/injection';
import {
BaseContext, BaseInput, BaseOutput,
ContextStorage, ParamsDictionary, Next
} from './types';
import { BaseContext, BaseInput, BaseOutput, ContextStorage, ParamsDictionary, Next } from './types';

const ContextStorageSymbol = Symbol('ARTUS::ContextStorage');

Expand All @@ -14,7 +11,7 @@ export class Output implements BaseOutput {
public data: ParamsDictionary = new Map();
}

export class Storage implements ContextStorage<any>{
export class Storage implements ContextStorage<any> {
private storageMap = new Map();

get(key?: string | symbol): any {
Expand All @@ -29,8 +26,8 @@ export class Storage implements ContextStorage<any>{
}

export class Context implements BaseContext {
public input: BaseInput = new Input();
public output: BaseOutput = new Output();
public input: Input = new Input();
public output: Output = new Output();

private _container!: ExecutionContainer;
private storageMap = new Map<string, ContextStorage<any>>();
Expand Down Expand Up @@ -66,7 +63,7 @@ export class Context implements BaseContext {
}
}

export type Middleware = (context: Context, next: Next) => void;
export type Middleware<T extends BaseContext = any> = (context: T, next: Next) => void;
export type Middlewares = Middleware[];
export type PipelineLike = { middlewares: Middlewares };
export type MiddlewareInput = Middleware | Middlewares | PipelineLike;
8 changes: 4 additions & 4 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { Context, Middlewares, MiddlewareInput, PipelineLike } from "./base";
import { Middlewares, MiddlewareInput, PipelineLike } from './base';
import { BaseContext } from './types';

export class Pipeline implements PipelineLike {
middlewares: Middlewares = [];
Expand All @@ -26,7 +26,7 @@ export class Pipeline implements PipelineLike {
throw new Error(`${middleware} isn't type MiddlewareInput`);
}

dispatch(i: number, ctx: Context, execution = { index: -1 }): Promise<any> {
dispatch(i: number, ctx: BaseContext, 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];
Expand All @@ -38,7 +38,7 @@ export class Pipeline implements PipelineLike {
}
}

run(ctx: Context): Promise<any> {
run(ctx: BaseContext): Promise<any> {
return this.dispatch(0, ctx);
}
}
22 changes: 11 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ export interface ParamsDictionary {
}

export interface BaseInput<P = ParamsDictionary> {
params: P
};
params?: P;
}

export interface BaseOutput<P = ParamsDictionary> {
data: P
};
data?: P;
}

export interface BaseContext extends ParamsDictionary {
input: BaseInput,
output: BaseOutput,
namespace: (namespace: string) => ContextStorage<any>,
restore?: () => void,
};
input: BaseInput;
output: BaseOutput;
namespace?: (namespace: string) => ContextStorage<any>;
restore?: () => void;
}

export type ContextStorage<T> = {
set: (value: T, key?: string | symbol) => void,
get: (key?: string | symbol) => T
set: (value: T, key?: string | symbol) => void;
get: (key?: string | symbol) => T;
};
export type Next = () => Promise<any>;
46 changes: 23 additions & 23 deletions test/context_pool.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { ContextPool } from '../src/context_pool';

describe("test/context_pool.test.ts", () => {
it('should get ok', () => {
const ctxPool = new ContextPool(1);
const container = new Map();
const ctx = ctxPool.get(container as any);
expect(ctx.container).toBe(container);
expect(ctx.input.params).toBeInstanceOf(Map);
expect(ctx.output.data).toBeInstanceOf(Map);
});
describe('test/context_pool.test.ts', () => {
it('should get ok', () => {
const ctxPool = new ContextPool(1);
const container = new Map();
const ctx = ctxPool.get(container as any);
expect(ctx.container).toBe(container);
expect(ctx.input.params).toBeInstanceOf(Map);
expect(ctx.output.data).toBeInstanceOf(Map);
});

it('should release ok', () => {
const ctxPool = new ContextPool(1);
const container = new Map();
const ctx = ctxPool.get(container as any);
ctx.input.params.set('id', 12);
ctx.output.data.set('user', { id: 12 });
ctxPool.release(ctx);
const container2 = new Map();
const ctx2 = ctxPool.get(container2 as any);
expect(ctx2.container).toBe(container2);
expect(ctx.input.params.has('id')).toBe(false);
expect(ctx.output.data.has('user')).toBe(false);
});
});
it('should release ok', () => {
const ctxPool = new ContextPool(1);
const container = new Map();
const ctx = ctxPool.get(container as any);
ctx.input.params!.set('id', 12);
ctx.output.data!.set('user', { id: 12 });
ctxPool.release(ctx);
const container2 = new Map();
const ctx2 = ctxPool.get(container2 as any);
expect(ctx2.container).toBe(container2);
expect(ctx.input.params!.has('id')).toBe(false);
expect(ctx.output.data!.has('user')).toBe(false);
});
});

0 comments on commit 018384b

Please sign in to comment.