@wildix/xbees-connect 1.2.0-alpha.1 → 1.2.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-es/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildix/xbees-connect",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.3",
|
|
4
4
|
"description": "This library provides easy communication between x-bees and integrated web applications",
|
|
5
5
|
"author": "dimitri.chernykh <dimitri.chernykh@wildix.com>",
|
|
6
6
|
"homepage": "",
|
package/dist-es/src/Client.js
CHANGED
|
@@ -2,6 +2,7 @@ import packageJson from '../package.json';
|
|
|
2
2
|
import { ClientEventType, EventType } from './enums';
|
|
3
3
|
import ClientParams from './helpers/ClientParams';
|
|
4
4
|
import LocalStorageManager from './helpers/LocalStorageManager';
|
|
5
|
+
import { MessageListener } from './helpers/MessageListener';
|
|
5
6
|
import PostMessageController from './helpers/PostMessageController';
|
|
6
7
|
/**
|
|
7
8
|
* Client provides functionality of communication between xBees and integrated web applications via iFrame or ReactNative WebView
|
|
@@ -28,52 +29,14 @@ export class Client {
|
|
|
28
29
|
void this.getInstance().ready();
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
|
-
listeners = [];
|
|
32
|
-
useSubscription = false;
|
|
33
32
|
localStorageManager = LocalStorageManager.getInstance();
|
|
33
|
+
messageListener = MessageListener.getInstance();
|
|
34
34
|
constructor() {
|
|
35
35
|
this.addEventListener(EventType.PBX_TOKEN, (token) => (ClientParams.getInstance().userToken = token));
|
|
36
36
|
}
|
|
37
37
|
sendAsync(data) {
|
|
38
38
|
return PostMessageController.getInstance().sendAsync(data);
|
|
39
39
|
}
|
|
40
|
-
parseMessage(message) {
|
|
41
|
-
try {
|
|
42
|
-
const data = typeof message.data === 'string' ? JSON.parse(message.data) : message.data;
|
|
43
|
-
if (!data?.type) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
return data;
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
console.error('parse message error', error);
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
onMessage(message) {
|
|
54
|
-
if (window.location.host === message.origin || window === message.source) {
|
|
55
|
-
// skip events started from integration itself if any
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const data = this.parseMessage(message);
|
|
59
|
-
if (!data) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const { type, payload } = data;
|
|
63
|
-
this.listeners.forEach(({ eventName, callback }) => {
|
|
64
|
-
if (eventName === type) {
|
|
65
|
-
if (type === EventType.ADD_CALL) {
|
|
66
|
-
const callbackFn = callback;
|
|
67
|
-
callbackFn(payload);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
// @ts-expect-error TODO: check the type for Callback<?>
|
|
71
|
-
const callbackFn = callback;
|
|
72
|
-
callbackFn(payload);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
40
|
ready(platform = 'all') {
|
|
78
41
|
return this.sendAsync({
|
|
79
42
|
type: ClientEventType.READY,
|
|
@@ -165,24 +128,10 @@ export class Client {
|
|
|
165
128
|
return this.sendAsync({ type: ClientEventType.AUTHORIZED });
|
|
166
129
|
}
|
|
167
130
|
addEventListener(eventName, callback) {
|
|
168
|
-
|
|
169
|
-
this.useSubscription = true;
|
|
170
|
-
window.addEventListener('message', this.onMessage.bind(this));
|
|
171
|
-
}
|
|
172
|
-
const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
|
|
173
|
-
if (!foundThisEvent) {
|
|
174
|
-
this.listeners.push({ eventName, callback });
|
|
175
|
-
}
|
|
176
|
-
return () => {
|
|
177
|
-
this.removeEventListener(eventName, callback);
|
|
178
|
-
};
|
|
131
|
+
return this.messageListener.addEventListener(eventName, callback);
|
|
179
132
|
}
|
|
180
133
|
removeEventListener(eventName, callback) {
|
|
181
|
-
this.
|
|
182
|
-
if (this.useSubscription && !this.listeners.length) {
|
|
183
|
-
this.useSubscription = false;
|
|
184
|
-
window.removeEventListener('message', this.onMessage.bind(this));
|
|
185
|
-
}
|
|
134
|
+
this.messageListener.removeEventListener(eventName, callback);
|
|
186
135
|
}
|
|
187
136
|
off(callback) {
|
|
188
137
|
this.localStorageManager.removeOnStorage(callback);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { EventType } from '../enums';
|
|
2
|
+
export class MessageListener {
|
|
3
|
+
static instance = null;
|
|
4
|
+
static getInstance() {
|
|
5
|
+
if (!this.instance) {
|
|
6
|
+
this.instance = new MessageListener();
|
|
7
|
+
}
|
|
8
|
+
return this.instance;
|
|
9
|
+
}
|
|
10
|
+
listeners = [];
|
|
11
|
+
useSubscription = false;
|
|
12
|
+
// eslint-disable-next-line
|
|
13
|
+
constructor() { }
|
|
14
|
+
parseMessage(message) {
|
|
15
|
+
try {
|
|
16
|
+
const data = typeof message.data === 'string' ? this.parseJSON(message.data) : message.data;
|
|
17
|
+
if (!data?.type) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error('parse message error', error);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
parseJSON(messageData) {
|
|
28
|
+
const trimmedData = messageData.trim();
|
|
29
|
+
if (!trimmedData || !trimmedData.startsWith('{') || !trimmedData.endsWith('}')) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return JSON.parse(trimmedData);
|
|
33
|
+
}
|
|
34
|
+
onMessage = (message) => {
|
|
35
|
+
if (window.location.host === message.origin || window === message.source) {
|
|
36
|
+
// skip events started from integration itself if any
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const data = this.parseMessage(message);
|
|
40
|
+
if (!data) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const { type, payload } = data;
|
|
44
|
+
this.listeners.forEach(({ eventName, callback }) => {
|
|
45
|
+
if (eventName === type) {
|
|
46
|
+
if (type === EventType.ADD_CALL) {
|
|
47
|
+
const callbackFn = callback;
|
|
48
|
+
callbackFn(payload);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// @ts-expect-error TODO: check the type for Callback<?>
|
|
52
|
+
const callbackFn = callback;
|
|
53
|
+
callbackFn(payload);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
addEventListener(eventName, callback) {
|
|
59
|
+
if (!this.useSubscription) {
|
|
60
|
+
this.useSubscription = true;
|
|
61
|
+
window.addEventListener('message', this.onMessage);
|
|
62
|
+
}
|
|
63
|
+
const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
|
|
64
|
+
if (!foundThisEvent) {
|
|
65
|
+
this.listeners.push({ eventName, callback });
|
|
66
|
+
}
|
|
67
|
+
return () => {
|
|
68
|
+
this.removeEventListener(eventName, callback);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
removeEventListener(eventName, callback) {
|
|
72
|
+
this.listeners = this.listeners.filter(({ eventName: _eventName, callback: _callback }) => !(Object.is(callback, _callback) && (!eventName ? true : eventName === _eventName)));
|
|
73
|
+
if (this.useSubscription && !this.listeners.length) {
|
|
74
|
+
this.useSubscription = false;
|
|
75
|
+
window.removeEventListener('message', this.onMessage);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -16,13 +16,10 @@ export declare class Client implements ConnectClient {
|
|
|
16
16
|
private static instance;
|
|
17
17
|
static getInstance(): ConnectClient;
|
|
18
18
|
static initialize(renderer: () => Promise<void>): void;
|
|
19
|
-
private listeners;
|
|
20
|
-
private useSubscription;
|
|
21
19
|
private readonly localStorageManager;
|
|
22
|
-
|
|
20
|
+
private readonly messageListener;
|
|
21
|
+
private constructor();
|
|
23
22
|
private sendAsync;
|
|
24
|
-
private parseMessage;
|
|
25
|
-
private onMessage;
|
|
26
23
|
ready(platform?: SupportedPlatformVariant | undefined): Promise<ResponseMessage>;
|
|
27
24
|
version(): string;
|
|
28
25
|
isPlatformNative(): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Callback } from '../../types/Callback';
|
|
2
|
+
import { RemoveEventListener } from '../../types/Listener';
|
|
3
|
+
import { EventType } from '../enums';
|
|
4
|
+
export declare class MessageListener {
|
|
5
|
+
private static instance;
|
|
6
|
+
static getInstance(): MessageListener;
|
|
7
|
+
private listeners;
|
|
8
|
+
private useSubscription;
|
|
9
|
+
private constructor();
|
|
10
|
+
private parseMessage;
|
|
11
|
+
private parseJSON;
|
|
12
|
+
private onMessage;
|
|
13
|
+
addEventListener<T extends EventType = EventType>(eventName: T, callback: Callback<T>): RemoveEventListener;
|
|
14
|
+
removeEventListener<T extends EventType = EventType>(eventName: T | null, callback: Callback<T>): void;
|
|
15
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventType } from '../src/enums';
|
|
1
|
+
import { ClientEventType, EventType } from '../src/enums';
|
|
2
2
|
import { Callback } from './Callback';
|
|
3
3
|
import { Contact, ContactQuery } from './Contact';
|
|
4
4
|
import { RemoveEventListener } from './Listener';
|
|
@@ -53,25 +53,25 @@ export interface ConnectClient {
|
|
|
53
53
|
isActivationOnly: () => boolean;
|
|
54
54
|
/**
|
|
55
55
|
* Retrieves current x-bees context data */
|
|
56
|
-
getContext: () => Promise<ResponseMessage
|
|
56
|
+
getContext: () => Promise<ResponseMessage<ClientEventType.CONTEXT>>;
|
|
57
57
|
/**
|
|
58
58
|
* Retrieves current opened in x-bees contact data */
|
|
59
|
-
getCurrentContact: () => Promise<ResponseMessage
|
|
59
|
+
getCurrentContact: () => Promise<ResponseMessage<ClientEventType.CURRENT_CONTACT>>;
|
|
60
60
|
/**
|
|
61
61
|
* Retrieves current opened in x-bees conversation data or undefined if the conversation is temporary */
|
|
62
|
-
getCurrentConversation: () => Promise<ResponseMessage
|
|
62
|
+
getCurrentConversation: () => Promise<ResponseMessage<ClientEventType.CURRENT_CONVERSATION>>;
|
|
63
63
|
/**
|
|
64
64
|
* Sends notification to x-bees about contact data updated */
|
|
65
|
-
contactUpdated(query: ContactQuery, contact: Contact): Promise<ResponseMessage
|
|
65
|
+
contactUpdated(query: ContactQuery, contact: Contact): Promise<ResponseMessage<ClientEventType.CONTACT_CREATE_OR_UPDATE>>;
|
|
66
66
|
/**
|
|
67
67
|
* Sends notification to x-bees about contact match was updated */
|
|
68
|
-
contactMatchUpdated(query: ContactQuery, contact: Contact): Promise<ResponseMessage
|
|
68
|
+
contactMatchUpdated(query: ContactQuery, contact: Contact): Promise<ResponseMessage<ClientEventType.CONTACT_MATCH_UPDATE>>;
|
|
69
69
|
/**
|
|
70
70
|
* Retrieves current theme mode (light or dark) */
|
|
71
|
-
getThemeMode: () => Promise<ResponseMessage
|
|
71
|
+
getThemeMode: () => Promise<ResponseMessage<ClientEventType.THEME_MODE>>;
|
|
72
72
|
/**
|
|
73
73
|
* Retrieves current theme with mode (light or dark) and theme options like typography settings and palette */
|
|
74
|
-
getTheme: () => Promise<ResponseMessage
|
|
74
|
+
getTheme: () => Promise<ResponseMessage<ClientEventType.THEME>>;
|
|
75
75
|
/**
|
|
76
76
|
* Sends request to x-bees to start a call with the number */
|
|
77
77
|
startCall: (phoneNumber: string) => Promise<ResponseMessage>;
|
|
@@ -107,7 +107,7 @@ export interface ConnectClient {
|
|
|
107
107
|
onPbxTokenChange: (callback: Callback<EventType.PBX_TOKEN>) => RemoveEventListener;
|
|
108
108
|
/**
|
|
109
109
|
* Retrieves current x-bees token */
|
|
110
|
-
getXBeesToken: () => Promise<ResponseMessage
|
|
110
|
+
getXBeesToken: () => Promise<ResponseMessage<ClientEventType.TOKEN>>;
|
|
111
111
|
/**
|
|
112
112
|
* Starts listen for the events of searching contacts and handle autosuggestion with the provided callback */
|
|
113
113
|
onSuggestContacts: (callback: (query: string, resolve: SuggestContactsResolver, reject: Reject) => void) => RemoveEventListener;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildix/xbees-connect",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.3",
|
|
4
4
|
"description": "This library provides easy communication between x-bees and integrated web applications",
|
|
5
5
|
"author": "dimitri.chernykh <dimitri.chernykh@wildix.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=16"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "53d26ea455e12585a72b9e4c831927afa241f7a5"
|
|
46
46
|
}
|