From b00c24d5c69a3ada8134ca99e901d66f69e3ee91 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 11 Nov 2020 16:32:18 +0800 Subject: [PATCH] feat: support register and call partial actions (#7) --- .../resourcer/src/__tests__/resourcer.test.ts | 47 +++++++++++++++++++ packages/resourcer/src/resourcer.ts | 4 ++ 2 files changed, 51 insertions(+) diff --git a/packages/resourcer/src/__tests__/resourcer.test.ts b/packages/resourcer/src/__tests__/resourcer.test.ts index 667cacdb1..10c510bad 100644 --- a/packages/resourcer/src/__tests__/resourcer.test.ts +++ b/packages/resourcer/src/__tests__/resourcer.test.ts @@ -123,6 +123,53 @@ describe('resourcer', () => { expect(context.arr).toStrictEqual([11,22]); }); + it('registerHandlers()', async () => { + const resourcer = new Resourcer(); + + resourcer.registerHandlers({ + 'test.list': async(ctx, next) => { + ctx.arr.push(1); + await next(); + ctx.arr.push(2); + }, + 'list': async(ctx, next) => { + ctx.arr.push(11); + await next(); + ctx.arr.push(22); + }, + }); + + resourcer.define({ + name: 'test', + }); + + resourcer.define({ + name: 'test2', + }); + + let context = { + arr: [], + }; + + await resourcer.execute({ + resource: 'test', + action: 'list', + }, context); + + expect(context.arr).toStrictEqual([1,2]); + + context = { + arr: [], + }; + + await resourcer.execute({ + resource: 'test2', + action: 'list', + }, context); + + expect(context.arr).toStrictEqual([11,22]); + }); + it('only', async () => { const resourcer = new Resourcer(); diff --git a/packages/resourcer/src/resourcer.ts b/packages/resourcer/src/resourcer.ts index 77e97b931..66ac924fc 100644 --- a/packages/resourcer/src/resourcer.ts +++ b/packages/resourcer/src/resourcer.ts @@ -194,6 +194,10 @@ export class Resourcer { } getAction(name: string, action: ActionName): Action { + // 支持注册局部 action + if (this.handlers.has(`${name}.${action}`)) { + return this.getResource(name).getAction(`${name}.${action}`); + } return this.getResource(name).getAction(action); }