feat: add broadcast notify when call config.setClientConfig

make all connection config should up to date
pull/109/head
moonrailgun 2 years ago
parent 798abeb1ec
commit 5ca5c765b8

@ -25,6 +25,8 @@ import {
import { appendUserDMConverse } from '../model/user'; import { appendUserDMConverse } from '../model/user';
import { sharedEvent } from '../event'; import { sharedEvent } from '../event';
import type { InboxItem } from '../model/inbox'; import type { InboxItem } from '../model/inbox';
import { useGlobalConfigStore } from '../store/globalConfig';
import type { GlobalConfig } from '../model/config';
/** /**
* Redux * Redux
@ -285,6 +287,16 @@ function listenNotify(socket: AppSocket, store: AppStore) {
}); });
}); });
socket.listen(
'config.updateClientConfig',
(config: Partial<GlobalConfig>) => {
useGlobalConfigStore.setState((state) => ({
...state,
...config,
}));
}
);
// 其他的额外的通知 // 其他的额外的通知
socketEventListeners.forEach(({ eventName, eventFn }) => { socketEventListeners.forEach(({ eventName, eventFn }) => {
socket.listen(eventName, eventFn); socket.listen(eventName, eventFn);

@ -43,13 +43,15 @@ export class Config implements Base {
/** /**
* set global client persist config from mongodb * set global client persist config from mongodb
*
* return all config from db
*/ */
static async setClientPersistConfig( static async setClientPersistConfig(
this: ReturnModelType<typeof Config>, this: ReturnModelType<typeof Config>,
key: string, key: string,
value: any value: any
): Promise<void> { ): Promise<Record<string, any>> {
await this.findOneAndUpdate( const newConfig = await this.findOneAndUpdate(
{ {
name: Config.globalClientConfigName, name: Config.globalClientConfigName,
}, },
@ -60,8 +62,11 @@ export class Config implements Base {
}, },
{ {
upsert: true, upsert: true,
new: true,
} }
); );
return newConfig.data;
} }
} }

@ -100,8 +100,12 @@ class ConfigService extends TcService {
}> }>
) { ) {
const { key, value } = ctx.params; const { key, value } = ctx.params;
await this.adapter.model.setClientPersistConfig(key, value); const newConfig = await this.adapter.model.setClientPersistConfig(
key,
value
);
await this.cleanActionCache('client', []); await this.cleanActionCache('client', []);
this.broadcastNotify(ctx, 'updateClientConfig', newConfig);
} }
async all(ctx: TcContext) { async all(ctx: TcContext) {

Loading…
Cancel
Save