fix: createPubSubManager

This commit is contained in:
chenos 2024-07-25 21:40:12 +08:00 committed by mytharcher
parent a97240a10b
commit e9679b6f86
2 changed files with 13 additions and 13 deletions

View File

@ -60,7 +60,7 @@ import { dataTemplate } from './middlewares/data-template';
import validateFilterParams from './middlewares/validate-filter-params'; import validateFilterParams from './middlewares/validate-filter-params';
import { Plugin } from './plugin'; import { Plugin } from './plugin';
import { InstallOptions, PluginManager } from './plugin-manager'; import { InstallOptions, PluginManager } from './plugin-manager';
import { PubSubManager, PubSubManagerOptions } from './pub-sub-manager'; import { createPubSubManager, PubSubManager, PubSubManagerOptions } from './pub-sub-manager';
import { SyncManager } from './sync-manager'; import { SyncManager } from './sync-manager';
import { SyncMessageManager } from './sync-message-manager'; import { SyncMessageManager } from './sync-message-manager';
@ -1132,7 +1132,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
this._cli = this.createCLI(); this._cli = this.createCLI();
this._i18n = createI18n(options); this._i18n = createI18n(options);
this.syncManager = new SyncManager(this); this.syncManager = new SyncManager(this);
this.pubSubManager = PubSubManager.create(this, { this.pubSubManager = createPubSubManager(this, {
channelPrefix: this.name, channelPrefix: this.name,
...options.pubSubManager, ...options.pubSubManager,
}); });

View File

@ -25,23 +25,23 @@ export interface PubSubManagerSubscribeOptions {
debounce?: number; debounce?: number;
} }
export const createPubSubManager = (app: Application, options: PubSubManagerOptions) => {
const pubSubManager = new PubSubManager(options);
app.on('afterStart', async () => {
await pubSubManager.connect();
});
app.on('afterStop', async () => {
await pubSubManager.close();
});
return pubSubManager;
};
export class PubSubManager { export class PubSubManager {
adapter: IPubSubAdapter; adapter: IPubSubAdapter;
messageHandlers = new Map(); messageHandlers = new Map();
subscribes = new Map(); subscribes = new Map();
publisherId: string; publisherId: string;
static create(app: Application, options: PubSubManagerOptions) {
const pubSubManager = new PubSubManager(options);
app.on('afterStart', async () => {
await pubSubManager.connect();
});
app.on('afterStop', async () => {
await pubSubManager.close();
});
return pubSubManager;
}
constructor(protected options: PubSubManagerOptions = {}) { constructor(protected options: PubSubManagerOptions = {}) {
this.publisherId = uid(); this.publisherId = uid();
} }