mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 06:29:25 +08:00
* feat: pub/sub manager * fix: test case * fix: test error * fix: test error * feat: skip self * feat: debounce * feat: improve code * fix: test error * feat: test cases * feat: test cases * fix: improve code * fix: improve code * feat: improve code * fix: improve code * fix: test case * fix: typo * fix: createPubSubManager * fix: delete messageHandlers * fix: test case * feat: improve code * fix: test error * fix: test error * refactor(server): adapt to new api and fix test * fix(plugin-data-source-main): fix changed api * fix: test error * fix: remove sync-manager test case * chore(server): remove legacy code * fix(plugin-workflow): fix send sync message with transaction * chore(server): remove legacy code * chore(server): remove legacy code * fix(plugin-workflow): fix test case * fix(plugin-workflow): fix test case * test(server): test skip-install parameter in cluster * test(server): avoid multiple installation in cluster * test(server): installation in cluster * feat: sync collection using sync manager (#4920) * chore: sync collection message * chore: sync acl * fix: typo * chore: sync data source * chore: remove collection * fix: typo * fix: test * chore: sync sub app event * chore: sync collection test * chore: collection test * chore: test * chore: data source sync message * chore: sync multi app * chore: test * chore: test * chore: test * chore: test * chore: test * chore: error message * fix(server): add type and remove log * fix(server): not to publish when adpater is not connected * refactor(server): refine types * chore: timeout * fix(server): fix pubSubManager options * test(ci): test ci checkout --------- Co-authored-by: mytharcher <mytharcher@gmail.com> Co-authored-by: ChengLei Shao <chareice@live.com>
83 lines
2.4 KiB
TypeScript
83 lines
2.4 KiB
TypeScript
/**
|
|
* This file is part of the NocoBase (R) project.
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
* Authors: NocoBase Team.
|
|
*
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
*/
|
|
|
|
import { Transactionable } from '@nocobase/database';
|
|
import Application from './application';
|
|
import { PubSubCallback, PubSubManager, PubSubManagerPublishOptions } from './pub-sub-manager';
|
|
|
|
export class SyncMessageManager {
|
|
protected versionManager: SyncMessageVersionManager;
|
|
protected pubSubManager: PubSubManager;
|
|
|
|
constructor(
|
|
protected app: Application,
|
|
protected options: any = {},
|
|
) {
|
|
this.versionManager = new SyncMessageVersionManager();
|
|
app.on('beforeLoadPlugin', async (plugin) => {
|
|
if (!plugin.name) {
|
|
return;
|
|
}
|
|
await this.subscribe(plugin.name, plugin.handleSyncMessage.bind(plugin));
|
|
});
|
|
}
|
|
|
|
get debounce() {
|
|
return this.options.debounce || 1000;
|
|
}
|
|
|
|
async publish(channel: string, message, options?: PubSubManagerPublishOptions & Transactionable) {
|
|
const { transaction, ...others } = options || {};
|
|
if (transaction) {
|
|
return await new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
reject(new Error(`Publish message to ${channel} timeout, message: ${JSON.stringify(message)}`));
|
|
}, 50000);
|
|
|
|
transaction.afterCommit(async () => {
|
|
try {
|
|
const r = await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
skipSelf: true,
|
|
...others,
|
|
});
|
|
resolve(r);
|
|
} catch (error) {
|
|
reject(error);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
return await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
skipSelf: true,
|
|
...options,
|
|
});
|
|
}
|
|
}
|
|
|
|
async subscribe(channel: string, callback: PubSubCallback) {
|
|
return await this.app.pubSubManager.subscribe(`${this.app.name}.sync.${channel}`, callback, {
|
|
debounce: this.debounce,
|
|
});
|
|
}
|
|
|
|
async unsubscribe(channel: string, callback: PubSubCallback) {
|
|
return this.app.pubSubManager.unsubscribe(`${this.app.name}.sync.${channel}`, callback);
|
|
}
|
|
|
|
async sync() {
|
|
// TODO
|
|
}
|
|
}
|
|
|
|
export class SyncMessageVersionManager {
|
|
// TODO
|
|
}
|