@maxzima/wa-communicator 0.0.8 → 0.0.9

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/README.md CHANGED
@@ -52,13 +52,22 @@ communicator.sendMessage({
52
52
 
53
53
  ```javascript
54
54
  const communicator = new CommunicatorReceiver({
55
- senderOrigin: 'clients.google.com',
56
- callback: (message) => {
57
- if (message.target === CommunicatorTargetEnum.SIGN_UP && message.action[CommunicatorActionEnum.GOTO]) {
58
- window.location.href = message.action[CommunicatorActionEnum.GOTO].url;
59
- }
60
- }
55
+ senderOrigin: 'clients.google.com'
56
+ });
57
+
58
+ // subscribe
59
+ communicator.follow('one', {
60
+ target: CommunicatorTargetEnum.SIGN_UP,
61
+ action: CommunicatorActionEnum.CLOSE,
62
+ callback: message => {
63
+ console.log('close', message);
64
+ },
65
+ });
66
+
67
+ // unsubscribe
68
+ receiver.unfollow({
69
+ code: 'one',
70
+ action: CommunicatorActionEnum.CLOSE
61
71
  });
62
72
 
63
- communicator.watch();
64
73
  ```
@@ -1,8 +1,11 @@
1
- import { TReceiverProps } from '../types';
1
+ import { TCommunicatorMessage, TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
2
2
  export declare class CommunicatorReceiver {
3
- private readonly callback;
4
3
  private readonly senderOrigin;
4
+ private queue;
5
5
  constructor(props: TReceiverProps);
6
- watch(): void;
7
6
  private onMessage;
7
+ private prepareCode;
8
+ isValidMessage: (message: TCommunicatorMessage) => boolean;
9
+ subscribe: (code: string, params: TReceiverSubscribeParams) => void;
10
+ unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
8
11
  }
@@ -1,6 +1,9 @@
1
1
  import { modifyOrigin } from "./utils";
2
+ import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
3
+ import { CommunicatorActionEnum_isCorrect } from '../enums/CommunicatorActionEnum';
2
4
  export class CommunicatorReceiver {
3
5
  constructor(props) {
6
+ this.queue = [];
4
7
  this.onMessage = (event) => {
5
8
  if (!event || !event.origin || !event.data) {
6
9
  return;
@@ -16,15 +19,80 @@ export class CommunicatorReceiver {
16
19
  if (this.senderOrigin !== event.origin) {
17
20
  return;
18
21
  }
19
- if (typeof this.callback !== 'function') {
22
+ if (!this.isValidMessage(message)) {
20
23
  return;
21
24
  }
22
- this.callback(message);
25
+ const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
26
+ if (queue.length) {
27
+ queue.forEach(item => {
28
+ if (typeof item.params.callback === 'function') {
29
+ item.params.callback(message);
30
+ }
31
+ });
32
+ }
33
+ };
34
+ this.prepareCode = (code) => {
35
+ if (typeof code === 'string' || code === "") {
36
+ throw new Error('WAC: code is not valid!');
37
+ }
38
+ return `wab_${code}`;
39
+ };
40
+ this.isValidMessage = (message) => {
41
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
42
+ const actions = message.action;
43
+ let isValidActions = typeof actions === 'object';
44
+ if (isValidTarget && isValidActions) {
45
+ for (const prop in actions) {
46
+ if (actions.hasOwnProperty(prop)) {
47
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
48
+ if (!isValidActions) {
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return isValidTarget && isValidActions;
55
+ };
56
+ this.subscribe = (code, params) => {
57
+ if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
58
+ (!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
59
+ throw new Error('WAC: target or action is not allowed!');
60
+ }
61
+ if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
62
+ console.log(`WAB: subscribe is exists!`);
63
+ }
64
+ this.queue.push({
65
+ code: this.prepareCode(code),
66
+ target: params.target,
67
+ action: params.action,
68
+ params: {
69
+ callback: params.callback,
70
+ }
71
+ });
72
+ };
73
+ this.unsubscribe = (params) => {
74
+ if (!params) {
75
+ this.queue = [];
76
+ return true;
77
+ }
78
+ const queueForRemove = this.queue.filter(item => {
79
+ return (!params.code || item.code === this.prepareCode(params.code)) &&
80
+ (!params.target || item.target === params.target) &&
81
+ (!params.action || item.action === params.action);
82
+ });
83
+ if (queueForRemove.length) {
84
+ const toRemove = new Set(queueForRemove);
85
+ this.queue = this.queue.filter(x => !toRemove.has(x));
86
+ return true;
87
+ }
88
+ return false;
23
89
  };
24
- this.callback = props.callback;
25
90
  this.senderOrigin = modifyOrigin(props.senderOrigin);
26
- }
27
- watch() {
28
- window.addEventListener('message', this.onMessage, false);
91
+ if (this.senderOrigin) {
92
+ window.addEventListener('message', this.onMessage, false);
93
+ }
94
+ else {
95
+ throw new Error('WAC: senderOrigin is broken!');
96
+ }
29
97
  }
30
98
  }
@@ -4,8 +4,17 @@ export class CommunicatorSender {
4
4
  constructor(props) {
5
5
  this.otherWindow = props.otherWindow;
6
6
  this.receiverOrigin = modifyOrigin(props.receiverOrigin);
7
+ if (!this.receiverOrigin) {
8
+ throw new Error('WAC: receiverOrigin is broken!');
9
+ }
7
10
  }
8
11
  sendMessage(message) {
12
+ if (!this.receiverOrigin) {
13
+ return;
14
+ }
15
+ if (!CommunicatorTargetEnum_isCorrect(message.target)) {
16
+ throw new Error(`WAC: Target "${message.target}" is not allowed!`);
17
+ }
9
18
  if (!this.receiverOrigin ||
10
19
  !CommunicatorTargetEnum_isCorrect(message.target)) {
11
20
  return;
@@ -5,7 +5,6 @@ export declare type TSenderProps = {
5
5
  otherWindow: Window;
6
6
  };
7
7
  export declare type TReceiverProps = {
8
- callback: TReceiverCallback;
9
8
  senderOrigin: string;
10
9
  };
11
10
  export declare type TReceiverCallback = (message: TCommunicatorMessage) => void;
@@ -27,3 +26,22 @@ export declare type TCommunicatorMessage = {
27
26
  target: CommunicatorTargetEnum;
28
27
  action: TCommunicatorSenderActionMap;
29
28
  };
29
+ export declare type TReceiverSubscribeParams = {
30
+ target: CommunicatorTargetEnum;
31
+ action: CommunicatorActionEnum;
32
+ callback: TReceiverCallback;
33
+ };
34
+ export declare type TReceiverMessageQueue = TReceiverMessageQueueItem[];
35
+ export declare type TReceiverMessageQueueItem = {
36
+ code: string;
37
+ target: CommunicatorTargetEnum;
38
+ action: CommunicatorActionEnum;
39
+ params?: {
40
+ callback: TReceiverCallback;
41
+ };
42
+ };
43
+ export declare type TReceiverUnsubscribeMessageParams = {
44
+ code?: string;
45
+ target?: CommunicatorTargetEnum;
46
+ action?: CommunicatorActionEnum;
47
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.8",
2
+ "version": "0.0.9",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
@@ -1,20 +1,27 @@
1
- import {TCommunicatorMessage, TReceiverCallback, TReceiverProps} from '../types';
1
+ import {
2
+ TCommunicatorMessage,
3
+ TReceiverMessageQueue,
4
+ TReceiverProps,
5
+ TReceiverSubscribeParams,
6
+ TReceiverUnsubscribeMessageParams,
7
+ } from '../types';
2
8
  import {modifyOrigin} from "./utils";
9
+ import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
10
+ import {CommunicatorActionEnum_isCorrect} from '../enums/CommunicatorActionEnum';
3
11
 
4
12
  export class CommunicatorReceiver {
5
- private readonly callback: TReceiverCallback;
6
13
  private readonly senderOrigin: string;
14
+ private queue: TReceiverMessageQueue = [];
7
15
 
8
16
  constructor(props: TReceiverProps) {
9
- this.callback = props.callback;
10
17
  this.senderOrigin = modifyOrigin(props.senderOrigin);
11
- }
12
18
 
13
- /**
14
- * Watch Messages
15
- */
16
- public watch() {
17
- window.addEventListener('message', this.onMessage, false);
19
+ if (this.senderOrigin) {
20
+ // Watch Messages
21
+ window.addEventListener('message', this.onMessage, false);
22
+ } else {
23
+ throw new Error('WAC: senderOrigin is broken!');
24
+ }
18
25
  }
19
26
 
20
27
  /**
@@ -39,11 +46,106 @@ export class CommunicatorReceiver {
39
46
  return;
40
47
  }
41
48
 
42
- if (typeof this.callback !== 'function') {
49
+ if (!this.isValidMessage(message)) {
43
50
  return;
44
51
  }
45
52
 
46
- this.callback(message);
53
+ // Find queue by target and action
54
+ const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
55
+
56
+ if (queue.length) {
57
+ queue.forEach(item => {
58
+ if (typeof item.params.callback === 'function') {
59
+ item.params.callback(message);
60
+ }
61
+ })
62
+ }
47
63
  };
64
+
65
+ /**
66
+ * Prepare code for queue
67
+ * @param code
68
+ */
69
+ private prepareCode = (code: string) => {
70
+ if (typeof code === 'string' || code === "") {
71
+ throw new Error('WAC: code is not valid!');
72
+ }
73
+ return `wab_${code}`
74
+ }
75
+
76
+ /**
77
+ * Check the message for validity
78
+ * @param message
79
+ */
80
+ public isValidMessage = (message: TCommunicatorMessage) => {
81
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
82
+ const actions = message.action;
83
+ let isValidActions = typeof actions === 'object';
84
+
85
+ if (isValidTarget && isValidActions) {
86
+ for (const prop in actions) {
87
+ if(actions.hasOwnProperty(prop)) {
88
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
89
+ if (!isValidActions) {
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ return isValidTarget && isValidActions;
97
+ }
98
+
99
+ /**
100
+ * Subscribe by target and action
101
+ * @param code
102
+ * @param params
103
+ */
104
+ public subscribe = (code: string, params: TReceiverSubscribeParams): void => {
105
+ if (
106
+ (!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
107
+ (!params.action || !CommunicatorActionEnum_isCorrect(params.action))
108
+ ) {
109
+ throw new Error('WAC: target or action is not allowed!');
110
+ }
111
+
112
+ if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
113
+ console.log(`WAB: subscribe is exists!`);
114
+ }
115
+
116
+ this.queue.push({
117
+ code: this.prepareCode(code),
118
+ target: params.target,
119
+ action: params.action,
120
+ params: {
121
+ callback: params.callback,
122
+ }
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Subscribe by code or target or action
128
+ * @param params
129
+ */
130
+ public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
131
+ if (!params) {
132
+ this.queue = [];
133
+ return true;
134
+ }
135
+
136
+ const queueForRemove = this.queue.filter(item => {
137
+ return (!params.code || item.code === this.prepareCode(params.code)) &&
138
+ (!params.target || item.target === params.target) &&
139
+ (!params.action || item.action === params.action);
140
+ });
141
+
142
+ if (queueForRemove.length) {
143
+ const toRemove = new Set(queueForRemove);
144
+ this.queue = this.queue.filter(x => !toRemove.has(x));
145
+ return true;
146
+ }
147
+
148
+ return false;
149
+ }
48
150
  }
49
151
 
@@ -9,6 +9,10 @@ export class CommunicatorSender {
9
9
  constructor(props: TSenderProps) {
10
10
  this.otherWindow = props.otherWindow;
11
11
  this.receiverOrigin = modifyOrigin(props.receiverOrigin);
12
+
13
+ if (!this.receiverOrigin) {
14
+ throw new Error('WAC: receiverOrigin is broken!');
15
+ }
12
16
  }
13
17
 
14
18
  /**
@@ -16,6 +20,14 @@ export class CommunicatorSender {
16
20
  * @param message
17
21
  */
18
22
  public sendMessage(message: TCommunicatorMessage) {
23
+ if (!this.receiverOrigin) {
24
+ return;
25
+ }
26
+
27
+ if (!CommunicatorTargetEnum_isCorrect(message.target)) {
28
+ throw new Error(`WAC: Target "${message.target}" is not allowed!`);
29
+ }
30
+
19
31
  if (
20
32
  !this.receiverOrigin ||
21
33
  !CommunicatorTargetEnum_isCorrect(message.target)
@@ -7,7 +7,6 @@ export type TSenderProps = {
7
7
  }
8
8
 
9
9
  export type TReceiverProps = {
10
- callback: TReceiverCallback,
11
10
  senderOrigin: string,
12
11
  }
13
12
 
@@ -34,3 +33,26 @@ export type TCommunicatorMessage = {
34
33
  target: CommunicatorTargetEnum,
35
34
  action: TCommunicatorSenderActionMap
36
35
  }
36
+
37
+ export type TReceiverSubscribeParams = {
38
+ target: CommunicatorTargetEnum,
39
+ action: CommunicatorActionEnum,
40
+ callback: TReceiverCallback,
41
+ }
42
+
43
+ export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
44
+
45
+ export type TReceiverMessageQueueItem = {
46
+ code: string;
47
+ target: CommunicatorTargetEnum,
48
+ action: CommunicatorActionEnum,
49
+ params?: {
50
+ callback: TReceiverCallback,
51
+ }
52
+ }
53
+
54
+ export type TReceiverUnsubscribeMessageParams = {
55
+ code?: string;
56
+ target?: CommunicatorTargetEnum,
57
+ action?: CommunicatorActionEnum,
58
+ }
package/tsconfig.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "outDir": "dist",
17
17
  "alwaysStrict": true,
18
18
  "removeComments": true,
19
- "noImplicitReturns": true,
19
+ "noImplicitReturns": false,
20
20
  "noEmit": false,
21
21
  "noFallthroughCasesInSwitch": true,
22
22
  "noUnusedLocals": true,