@wildix/xbees-connect 1.2.0-alpha.9 → 1.2.1

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-connect",
3
- "version": "1.2.0-alpha.9",
3
+ "version": "1.2.0",
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": "",
@@ -1,6 +1,7 @@
1
1
  import packageJson from '../package.json';
2
2
  import { ClientEventType, DeprecatedUrlParams, EventType, UrlParams } from './enums';
3
3
  import LocalStorageManager from './helpers/LocalStorageManager';
4
+ import { MessageListener } from './helpers/MessageListener';
4
5
  import PostMessageControllerNative from './helpers/PostMessageControllerNative';
5
6
  import PostMessageControllerWeb from './helpers/PostMessageControllerWeb';
6
7
  import TechnicalSupport from './helpers/TechnicalSupport';
@@ -70,7 +71,7 @@ export class Client {
70
71
  }
71
72
  parseMessage(message) {
72
73
  try {
73
- const data = typeof message.data === 'string' ? JSON.parse(message.data) : message.data;
74
+ const data = typeof message.data === 'string' ? MessageListener.parseJSON(message.data) : message.data;
74
75
  if (!data?.type) {
75
76
  return null;
76
77
  }
@@ -0,0 +1,81 @@
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' ? MessageListener.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
+ static 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
+ console.debug(`onMessage skipped ${window.location.host} - ${message.origin} - ${window === message.source} - ${message}`);
37
+ // skip events started from integration itself if any
38
+ return;
39
+ }
40
+ const data = this.parseMessage(message);
41
+ if (!data) {
42
+ console.debug('onMessage skipped', message);
43
+ return;
44
+ }
45
+ const { type, payload } = data;
46
+ console.debug(`onMessage call - ${type} - ${payload}`);
47
+ this.listeners.forEach(({ eventName, callback }) => {
48
+ if (eventName === type) {
49
+ if (type === EventType.ADD_CALL) {
50
+ const callbackFn = callback;
51
+ callbackFn(payload);
52
+ }
53
+ else {
54
+ // @ts-expect-error TODO: check the type for Callback<?>
55
+ const callbackFn = callback;
56
+ callbackFn(payload);
57
+ }
58
+ }
59
+ });
60
+ };
61
+ listen(eventName, callback) {
62
+ if (!this.useSubscription) {
63
+ this.useSubscription = true;
64
+ window.addEventListener('message', this.onMessage);
65
+ }
66
+ const foundThisEvent = this.listeners.find(({ eventName: _eventName, callback: _callback }) => eventName === _eventName && Object.is(callback, _callback));
67
+ if (!foundThisEvent) {
68
+ this.listeners.push({ eventName, callback });
69
+ }
70
+ return () => {
71
+ this.off(eventName, callback);
72
+ };
73
+ }
74
+ off(eventName, callback) {
75
+ this.listeners = this.listeners.filter(({ eventName: _eventName, callback: _callback }) => !(Object.is(callback, _callback) && (!eventName ? true : eventName === _eventName)));
76
+ if (this.useSubscription && !this.listeners.length) {
77
+ this.useSubscription = false;
78
+ window.removeEventListener('message', this.onMessage);
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,14 @@
1
+ import { Callback, RemoveEventListener } from '../../types';
2
+ import { EventType } from '../enums';
3
+ export declare class MessageListener {
4
+ private static instance;
5
+ static getInstance(): MessageListener;
6
+ private listeners;
7
+ private useSubscription;
8
+ private constructor();
9
+ private parseMessage;
10
+ static parseJSON(messageData: string): any;
11
+ private onMessage;
12
+ listen<T extends EventType = EventType>(eventName: T, callback: Callback<T>): RemoveEventListener;
13
+ off<T extends EventType = EventType>(eventName: T | null, callback: Callback<T>): void;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-connect",
3
- "version": "1.2.0-alpha.9",
3
+ "version": "1.2.1",
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": "a73073032bd9d6fe2986345f8c6b9ae3e08690c1"
45
+ "gitHead": "a2732739df49717b258c3a6b97a5cce3f333f43a"
46
46
  }