@talkjs/react-native
Version:
Official TalkJS SDK for React Native
134 lines (131 loc) • 3.96 kB
JavaScript
"use strict";
import { hasIdOnly } from './User';
import { getRandomWord } from './utils';
import { LEAVE_PROMISE_EVENT, SEND_MESSAGE_PROMISE_EVENT, SESSION } from './constants';
const VARIABLE_LENGTH = 10;
export class _ConversationBuilder {
#javaScript;
#variableName;
#conversationId;
#pendingMessages;
#sendMessageResolvers;
#leaveResolvers;
// Whether client synchronization needs to be enabled.
#usesSynchronization;
constructor(conversationId) {
this.#conversationId = conversationId;
this.#usesSynchronization = false;
this.#variableName = `window.conversation_${getRandomWord(VARIABLE_LENGTH)}`;
this.#javaScript = `
${this.#variableName} = ${SESSION}.getOrCreateConversation(
'${this.#conversationId}'
);
`;
this.#pendingMessages = [];
this._injectJavaScript = undefined;
this.#sendMessageResolvers = {};
this.#leaveResolvers = {};
}
static create(conversationId) {
return new _ConversationBuilder(conversationId);
}
get _conversationId() {
return this.#conversationId;
}
get _variableName() {
return this.#variableName;
}
get _usesSynchronization() {
return this.#usesSynchronization;
}
_setInjectJavaScript(injectJavaScript) {
this._injectJavaScript = injectJavaScript;
for (const javaScript of this.#pendingMessages) {
injectJavaScript(javaScript);
}
this.#pendingMessages.splice(0, this.#pendingMessages.length);
}
_resolve(event, key, value) {
if (event === LEAVE_PROMISE_EVENT) {
const resolve = this.#leaveResolvers[key];
if (resolve) {
resolve(value);
delete this.#leaveResolvers[key];
}
} else {
const resolve = this.#sendMessageResolvers[key];
if (resolve) {
resolve();
delete this.#sendMessageResolvers[key];
}
}
}
leave() {
return new Promise(resolve => {
const key = getRandomWord(VARIABLE_LENGTH);
this.#leaveResolvers[key] = resolve;
const javaScript = `
${this.#variableName}.leave().then((result) => {
sendToReactNative('${LEAVE_PROMISE_EVENT}', { result, key: '${key}' });
});
true;
`;
if (this._injectJavaScript) {
this._injectJavaScript(javaScript);
} else {
this.#pendingMessages.push(javaScript);
}
});
}
setParticipant(user, settings) {
this.#usesSynchronization = true;
const args = hasIdOnly(user) ? user.id : user;
const userObj = `new Talk.User(${JSON.stringify(args)})`;
this.#javaScript += `
${this.#variableName}.setParticipant(
${userObj}, ${JSON.stringify(settings)});
`;
}
setAttributes(attributes) {
this.#usesSynchronization = true;
this.#javaScript += `
${this.#variableName}.setAttributes(${JSON.stringify(attributes)});`;
for (let key in attributes) {
// @ts-ignore
this[key] = attributes[key];
}
}
sendMessage(text, options) {
return new Promise(resolve => {
const key = getRandomWord(VARIABLE_LENGTH);
this.#sendMessageResolvers[key] = resolve;
const javaScript = `
${this.#variableName}.sendMessage('${text}', ${JSON.stringify(options)})
.then(() => {
sendToReactNative(
'${SEND_MESSAGE_PROMISE_EVENT}',
${JSON.stringify({
key
})}
);
});
true;
`;
if (this._injectJavaScript) {
this._injectJavaScript(javaScript);
} else {
this.#pendingMessages.push(javaScript);
}
});
}
toString() {
return `${this.#javaScript} true;`;
}
toJSON() {
return this.#conversationId;
}
}
// We want to create a new conversationBuilder object every time the customer
// calls `getConversationBuilder`. That's what the JSSDK does.
export const getConversationBuilder = _ConversationBuilder.create;
//# sourceMappingURL=ConversationBuilder.js.map