smartuniverse/ts/smartuniverse.classes.clientuniversechannel.ts

107 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './smartuniverse.plugins';
2019-04-11 15:52:01 +00:00
import * as interfaces from './interfaces';
2019-04-11 15:52:01 +00:00
import { ClientUniverse } from './';
2019-09-09 23:19:10 +00:00
import { ClientUniverseMessage } from './smartuniverse.classes.clientuniversemessage';
2019-09-09 23:39:38 +00:00
import { ReactionRequest } from './smartuniverse.classes.reactionrequest';
import { ReactionResponse } from './smartuniverse.classes.reactionresponse';
2019-04-11 15:52:01 +00:00
export class ClientUniverseChannel implements interfaces.IUniverseChannel {
// ======
// STATIC
// ======
2019-04-22 21:11:51 +00:00
/**
* creates a channel and adds it to the cache of clientUniverseArg
* @param clientUniverseArg
2019-04-22 22:28:57 +00:00
* @param channelNameArg
* @param passphraseArg
2019-04-22 21:11:51 +00:00
*/
2019-08-13 16:41:27 +00:00
public static createClientUniverseChannel(
clientUniverseArg: ClientUniverse,
2019-04-22 21:11:51 +00:00
channelNameArg: string,
2019-04-22 07:58:36 +00:00
passphraseArg: string
2019-08-13 16:41:27 +00:00
): ClientUniverseChannel {
2019-04-22 22:28:57 +00:00
const clientChannel = new ClientUniverseChannel(
clientUniverseArg,
channelNameArg,
passphraseArg
);
2019-08-13 13:48:20 +00:00
clientUniverseArg.clientUniverseCache.channelMap.add(clientChannel);
return clientChannel;
}
// ========
// INSTANCE
// ========
2019-04-22 20:04:52 +00:00
// properties
public name: string;
2019-04-22 07:58:36 +00:00
public passphrase: string;
2019-09-09 22:29:08 +00:00
public status: 'subscribed' | 'unsubscribed' = 'unsubscribed';
2019-09-10 21:55:20 +00:00
private subject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage<any>>();
2019-04-22 22:28:57 +00:00
2019-04-22 20:04:52 +00:00
// refs
2019-08-12 15:23:10 +00:00
public clientUniverseRef: ClientUniverse;
2019-04-22 22:28:57 +00:00
constructor(clientUniverseArg: ClientUniverse, nameArg: string, passphraseArg: string) {
2019-08-12 15:23:10 +00:00
this.clientUniverseRef = clientUniverseArg;
2019-04-22 20:04:52 +00:00
this.name = nameArg;
2019-04-22 11:06:01 +00:00
this.passphrase = passphraseArg;
}
/**
2019-04-22 11:06:01 +00:00
* subscribes to a channel
* tells the universe about this instances interest into a channel
*/
2019-09-10 21:55:20 +00:00
public subscribe(observingFunctionArg: (messageArg: ClientUniverseMessage<any>) => void) {
2019-09-10 17:36:10 +00:00
return this.subject.subscribe(
messageArg => {
observingFunctionArg(messageArg);
},
error => console.log(error)
);
}
2019-11-03 19:23:22 +00:00
public unsubscribe() {
// TODO: unsubscribe all users
}
2019-09-10 17:36:10 +00:00
public async populateSubscriptionToServer() {
2019-09-09 23:19:10 +00:00
// lets make sure the channel is connected
2019-09-09 22:29:08 +00:00
if (this.status === 'unsubscribed') {
2019-09-10 08:55:10 +00:00
const response = await this.clientUniverseRef.smartsocketClient.serverCall<
interfaces.ISocketRequest_SubscribeChannel
>('subscribeChannel', {
name: this.name,
passphrase: this.passphrase
});
2019-09-09 22:29:08 +00:00
this.status = response.subscriptionStatus;
}
2019-09-09 23:19:10 +00:00
}
2019-09-10 21:55:20 +00:00
public async emitMessageLocally(messageArg: ClientUniverseMessage<any>) {
2019-09-09 23:39:38 +00:00
this.subject.next(messageArg);
}
2019-08-12 15:23:10 +00:00
/**
* sends a message towards the server
* @param messageArg
*/
public async sendMessage(messageArg: interfaces.IMessageCreator) {
await this.clientUniverseRef.checkConnection();
const universeMessageToSend: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),
passphrase: this.passphrase,
targetChannelName: this.name,
messageText: messageArg.messageText,
2019-09-10 21:55:20 +00:00
payload: messageArg.payload
2019-08-12 15:23:10 +00:00
};
2019-09-01 15:04:25 +00:00
await this.clientUniverseRef.smartsocketClient.serverCall(
'processMessage',
universeMessageToSend
);
}
}