Files
smartuniverse/ts/smartuniverse.classes.client.universechannel.ts
T

116 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

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