fix(types,client,server): improve type safety and harden client/server message handling
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as plugins from './smartuniverse.plugins.js';
|
||||
import { Smartsocket, SmartsocketClient } from '@push.rocks/smartsocket';
|
||||
import { SmartsocketClient } from '@push.rocks/smartsocket';
|
||||
|
||||
import * as interfaces from './interfaces/index.js';
|
||||
|
||||
@@ -18,7 +18,7 @@ export interface IClientOptions {
|
||||
*/
|
||||
export class ClientUniverse {
|
||||
public options: IClientOptions;
|
||||
public smartsocketClient: plugins.smartsocket.SmartsocketClient;
|
||||
public smartsocketClient?: plugins.smartsocket.SmartsocketClient;
|
||||
public messageRxjsSubject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage<any>>();
|
||||
public clientUniverseCache = new ClientUniverseCache();
|
||||
|
||||
@@ -64,7 +64,7 @@ export class ClientUniverse {
|
||||
* remove a a achannel
|
||||
* @param messageArg
|
||||
*/
|
||||
public removeChannel(channelNameArg, notifyServer = true) {
|
||||
public removeChannel(channelNameArg: string, notifyServer = true) {
|
||||
const clientUniverseChannel = this.clientUniverseCache.channelMap.findOneAndRemoveSync(
|
||||
(channelItemArg) => {
|
||||
return channelItemArg.name === channelNameArg;
|
||||
@@ -110,18 +110,19 @@ export class ClientUniverse {
|
||||
/**
|
||||
* should handle a forced unsubscription by the server
|
||||
*/
|
||||
const socketFunctionUnsubscribe = new plugins.smartsocket.SocketFunction({
|
||||
funcName: 'unsubscribe',
|
||||
funcDef: async (dataArg: interfaces.IServerUnsubscribeActionPayload) => {
|
||||
const channel = this.clientUniverseCache.channelMap.findSync((channelArg) => {
|
||||
return channelArg.name === dataArg.name;
|
||||
});
|
||||
if (channel) {
|
||||
channel.unsubscribe();
|
||||
}
|
||||
return {};
|
||||
},
|
||||
});
|
||||
const socketFunctionUnsubscribe =
|
||||
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_Unsubscribe>({
|
||||
funcName: 'unsubscribe',
|
||||
funcDef: async (dataArg) => {
|
||||
const channel = this.clientUniverseCache.channelMap.findSync((channelArg) => {
|
||||
return channelArg.name === dataArg.name;
|
||||
});
|
||||
if (channel) {
|
||||
channel.unsubscribe();
|
||||
}
|
||||
return {};
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* handles message reception
|
||||
@@ -169,7 +170,7 @@ export class ClientUniverse {
|
||||
const instructDisconnect = async () => {
|
||||
if (this.smartsocketClient) {
|
||||
const smartsocketToDisconnect = this.smartsocketClient;
|
||||
this.smartsocketClient = null; // making sure the upstreamEvent does not interfere
|
||||
this.smartsocketClient = undefined; // making sure the upstreamEvent does not interfere
|
||||
await smartsocketToDisconnect.disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user