8000 refactor: plugin/eventbus by fengmk2 · Pull Request #289 · eggjs/tegg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: plugin/eventbus #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions plugin/eventbus/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Application } from 'egg';
import { EventHandlerProtoManager } from './lib/EventHandlerProtoManager';
import { EventbusLoadUnitHook } from './lib/EventbusLoadUnitHook';
import { EventbusProtoHook } from './lib/EventbusProtoHook';
import { EggCore as Application } from '@eggjs/core';
import { EventHandlerProtoManager } from './lib/EventHandlerProtoManager.js';
import { EventbusLoadUnitHook } from './lib/EventbusLoadUnitHook.js';
import { EventbusProtoHook } from './lib/EventbusProtoHook.js';

export default class EventbusAppHook {
private readonly app: Application;
private readonly eventHandlerProtoManager: EventHandlerProtoManager;
private readonly eventbusLoadUnitHook: EventbusLoadUnitHook;
private readonly eventbusProtoHook: EventbusProtoHook;

constructor(app) {
constructor(app: Application) {
this.app = app;
this.eventHandlerProtoManager = new EventHandlerProtoManager(app);
this.eventbusLoadUnitHook = new EventbusLoadUnitHook();
Expand Down
11 changes: 9 additions & 2 deletions plugin/eventbus/app/extend/application.unittest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application } from 'egg';
import { PrototypeUtil, EventBus, EventWaiter } from '@eggjs/tegg';
import { EggCore as Application } from '@eggjs/core';
import { PrototypeUtil, type EventBus, type EventWaiter } from '@eggjs/tegg';
import { SingletonEventBus } from '@eggjs/tegg-eventbus-runtime';
import { EggPrototype } from '@eggjs/tegg-metadata';

Expand All @@ -16,3 +16,10 @@ export default {
return eggObject.obj as EventWaiter;
},
};

declare module '@eggjs/core' {
interface EggCore {
getEventbus(): Promise<EventBus>;
getEventWaiter(): Promise<EventWaiter>;
}
}
20 changes: 14 additions & 6 deletions plugin/eventbus/app/extend/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Context } from 'egg';
import { EggContextEventBus } from '../../lib/EggContextEventBus';
import { Context } from '@eggjs/core';
import { EggContextEventBus } from '../../lib/EggContextEventBus.js';

const EVENT_BUS = Symbol.for('context#eventBus');

export default {
export default class EventBusContext extends Context {
[EVENT_BUS]: EggContextEventBus;

Check warning on line 8 in plugin/eventbus/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

plugin/eventbus/app/extend/context.ts#L7-L8

Added lines #L7 - L8 were not covered by tests
get eventBus() {
if (!this[EVENT_BUS]) {
this[EVENT_BUS] = new EggContextEventBus(this as unknown as Context);
this[EVENT_BUS] = new EggContextEventBus(this);
}
return this[EVENT_BUS];
},
};
}
}

Check warning on line 15 in plugin/eventbus/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

plugin/eventbus/app/extend/context.ts#L15

Added line #L15 was not covered by tests

declare module '@eggjs/core' {
interface Context {
get eventBus(): EggContextEventBus;
}
}
4 changes: 4 additions & 0 deletions plugin/eventbus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '@eggjs/tegg-plugin';
import '@eggjs/tegg-config';
import './app/extend/application.unittest.js';
import './app/extend/context.js';

Check warning on line 4 in plugin/eventbus/index.ts

View check run for this annotation

Codecov / codecov/patch

plugin/eventbus/index.ts#L1-L4

Added lines #L1 - L4 were not covered by tests
5 changes: 2 additions & 3 deletions plugin/eventbus/lib/EggContextEventBus.ts
ED4F
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import { Context } from 'egg';
import type { Context } from '@eggjs/core';
import { Events, PrototypeUtil, CORK_ID, ContextEventBus } from '@eggjs/tegg';
import { SingletonEventBus } from '@eggjs/tegg-eventbus-runtime';
import { EggPrototype } from '@eggjs/tegg-metadata';
Expand Down Expand Up @@ -32,11 +32,10 @@ export class EggContextEventBus implements ContextEventBus {
this.context.set(CORK_ID, null);
this.corkId = undefined;
}
return true;
}

emit<E extends keyof Events>(event: E, ...args: Arguments<Events[E]>): boolean {
return this.eventBus.emitWithContext(this.context, event, args);
}


}
6 changes: 3 additions & 3 deletions plugin/eventbus/lib/EggEventContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Application } from 'egg';
import { Context, EggCore as Application } from '@eggjs/core';
import { AbstractEggContext, EggContext } from '@eggjs/tegg-runtime';
import { IdenticalUtil } from '@eggjs/tegg';
import { EGG_CONTEXT, TEGG_CONTEXT } from '@eggjs/egg-module-common';
Expand All @@ -14,12 +14,12 @@ export function eggEventContextFactory(AbstractEggContextClazz: typeof AbstractE
constructor(context: Context) {
super();
this.set(EGG_CONTEXT, context);
(context as any)[TEGG_CONTEXT] = this;
context[TEGG_CONTEXT] = this;
// In chair application mode,
// Plugin event may install in app dir,
// Plugin tegg may install in layer dir,
// Will has multi IdenticalUtil instance.
this.id = identicalUtil.createContextId(context.tracer?.traceId);
this.id = identicalUtil.createContextId((context.tracer as { traceId: string } | undefined)?.traceId);
}

static createContextFactory(app: Application): ContextCreator {
Expand Down
4 changes: 2 additions & 2 deletions plugin/eventbus/lib/EventHandlerProtoManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Application } from 'egg';
import { EggCore as Application } from '@eggjs/core';
import { EggPrototype } from '@eggjs/tegg-metadata';
import { EventContextFactory, EventHandlerFactory } from '@eggjs/tegg-eventbus-runtime';
import { EVENT_NAME, EventName } from '@eggjs/tegg';
import { eggEventContextFactory } from './EggEventContext';
import { eggEventContextFactory } from './EggEventContext.js';

export class EventHandlerProtoManager {
private readonly protos: Set<EggPrototype> = new Set();
Expand Down
2 changes: 1 addition & 1 deletion
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LifecycleHook, EVENT_NAME } from '@eggjs/tegg';
import { EggPrototype, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata';
import { EventHandlerProtoManager } from './EventHandlerProtoManager';
import { EventHandlerProtoManager } from './EventHandlerProtoManager.js';

export class EventbusProtoHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
private eventHandlerProtoManager: EventHandlerProtoManager;
Expand Down
41 changes: 24 additions & 17 deletions plugin/eventbus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@
"lib/**/*.d.ts",
"app/**/*.js",
"app/**/*.d.ts",
"typings/*.d.ts",
"config/*.js",
"config/*.d.ts"
"config/*.d.ts",
"index.js",
"index.d.ts"
],
"types": "typings/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
"test": "egg-bin test",
"cov": "egg-bin cov",
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
"prepublishOnly": "npm run tsc:pub"
"tsc": "npm run clean && tsc -p ./tsconfig.pub.json",
"tsc:pub": "npm run tsc",
"prepublishOnly": "npm run tsc"
},
"homepage": "https://github.com/eggjs/tegg",
"bugs": {
Expand All @@ -45,29 +54,27 @@
"directory": "plugin/eventbus"
},
"engines": {
"node": ">=14.0.0"
"node": ">=20.0.0"
},
"dependencies": {
"@eggjs/core": "^6.4.0",
"@eggjs/egg-module-common": "^3.52.0",
"@eggjs/tegg": "^3.52.0",
"@eggjs/tegg-eventbus-runtime": "^3.52.0",
"@eggjs/tegg": "^3.52.0",
"@eggjs/tegg-metadata": "^3.52.0",
"@eggjs/tegg-runtime": "^3.52.0"
},
"devDependencies": {
"@eggjs/tegg-common-util": "^3.52.0",
"@eggjs/tegg-config": "^3.52.0",
"@eggjs/tegg-plugin": "^3.52.0",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"await-event": "^2.1.0",
"cross-env": "^7.0.3",
"@types/mocha": "10",
"@types/node": "22",
"egg": "4",
"@eggjs/mock": "6",
"@eggjs/tracer": "^3.0.0",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"@eggjs/tracer": "3",
"ts-node": "10",
"typescript": "5"
},
"publishConfig": {
"access": "public"
Expand Down
23 changes: 9 additions & 14 deletions plugin/eventbus/test/eventbus.test.ts
10000
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import mm, { MockApplication } from 'egg-mock';
import { mm, MockApplication } from '@eggjs/mock';
import { TimerUtil } from '@eggjs/tegg-common-util';
import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService';
import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger';
import { MultiEventHandler } from './fixtures/apps/event-app/app/event-module/MultiEventHandler';
import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService.js';
import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger.js';
import { MultiEventHandler } from './fixtures/apps/event-app/app/event-module/MultiEventHandler.js';
import { IEventContext } from '@eggjs/tegg';

describe('plugin/eventbus/test/eventbus.test.ts', () => {
let app: MockApplication;

afterEach(async () => {
mm.restore();
return mm.restore();
});

before(async () => {
mm(process.env, 'EGG_TYPESCRIPT', true);
mm(process, 'cwd', () => {
return path.join(__dirname, '../');
});
app = mm.app({
baseDir: path.join(__dirname, './fixtures/apps/event-app'),
framework: require.resolve('egg'),
baseDir: 'apps/event-app',
});
await app.ready();
});
Expand All @@ -34,7 +29,7 @@ describe('plugin/eventbus/test/eventbus.test.ts', () => {
const helloService = await ctx.getEggObject(HelloService);
let msg: string | undefined;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', m => {
mm(HelloLogger.prototype, 'handle', (m: string) => {
msg = m;
});
const eventWaiter = await app.getEventWaiter();
Expand Down Expand Up @@ -146,7 +141,7 @@ describe('plugin/eventbus/test/eventbus.test.ts', () => {
const helloService = await ctx.getEggObject(HelloService);
let eventName = '';
let msg = '';
mm(MultiEventHandler.prototype, 'handle', (ctx, m) => {
mm(MultiEventHandler.prototype, 'handle', (ctx: IEventContext, m: string) => {
eventName = ctx.eventName;
msg = m;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventContext, IEventContext } from '@eggjs/tegg';

@Event('helloEgg')
@Event('hiEgg')
export class MultiEventHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "multi-module-common",
"eggModule": {
"name": "multi-module-common"
}
},
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

module.exports = function() {
export default () => {
const config = {
keys: 'test key',
security: {
csrf: {
ignoreJSON: false,
}
},
},
};
return config;
Expand Down
14 changes: 0 additions & 14 deletions plugin/eventbus/test/fixtures/apps/event-app/config/plugin.js

This file was deleted.

16 changes: 16 additions & 0 deletions plugin/eventbus/test/fixtures/apps/event-app/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
tracer: {
package: '@eggjs/tracer',
enable: true,
},

tegg: {
package: '@eggjs/tegg-plugin',
enable: true,
},

teggConfig: {
package: '@eggjs/tegg-config',
enable: true,
},
};
3 changes: 2 additions & 1 deletion plugin/eventbus/test/fixtures/apps/event-app/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name": "event-app"
"name": "event-app",
"type": "module"
}
2 changes: 0 additions & 2 deletions plugin/eventbus/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
"baseUrl": "./"
},
"exclude": [
"node_modules",
"test"
]
}
1 change: 0 additions & 1 deletion plugin/eventbus/tsconfig.pub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"baseUrl": "./"
},
"exclude": [
"node_modules",
"test"
]
}
13 changes: 0 additions & 13 deletions plugin/eventbus/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
import 'egg';
import '@eggjs/tegg-plugin';
import '@eggjs/tegg-config'
import { EventBus, EventWaiter } from '@eggjs/tegg';

declare module 'egg' {
interface EventbusApplication {
getEventbus(): Promise<EventBus>;
getEventWaiter(): Promise<EventWaiter>;
}

interface Application extends EventbusApplication {
}
}
Loading
0