feat: support register and call partial actions (#7)

This commit is contained in:
chenos 2020-11-11 16:32:18 +08:00 committed by GitHub
parent dcdb21d398
commit b00c24d5c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 0 deletions

View File

@ -123,6 +123,53 @@ describe('resourcer', () => {
expect(context.arr).toStrictEqual([11,22]); 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 () => { it('only', async () => {
const resourcer = new Resourcer(); const resourcer = new Resourcer();

View File

@ -194,6 +194,10 @@ export class Resourcer {
} }
getAction(name: string, action: ActionName): Action { 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); return this.getResource(name).getAction(action);
} }