@maxzima/wa-communicator 0.0.10 → 0.0.13
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/engine/CommunicatorReceiver.d.ts +5 -0
- package/dist/engine/CommunicatorReceiver.js +52 -4
- package/dist/enums/CommunicatorActionEnum.d.ts +4 -3
- package/dist/enums/CommunicatorActionEnum.js +5 -2
- package/dist/types/index.d.ts +10 -1
- package/package.json +1 -1
- package/src/engine/CommunicatorReceiver.ts +70 -5
- package/src/engine/utils.ts +0 -1
- package/src/enums/CommunicatorActionEnum.ts +6 -2
- package/src/types/index.ts +12 -1
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
|
|
2
|
+
import { CommunicatorTargetEnum } from '../enums/CommunicatorTargetEnum';
|
|
3
|
+
import { CommunicatorActionEnum } from '../enums/CommunicatorActionEnum';
|
|
2
4
|
export declare class CommunicatorReceiver {
|
|
3
5
|
private readonly senderOrigin;
|
|
4
6
|
private queue;
|
|
7
|
+
private scenario;
|
|
5
8
|
constructor(props: TReceiverProps);
|
|
6
9
|
private onMessage;
|
|
7
10
|
subscribe: (params: TReceiverSubscribeParams) => void;
|
|
8
11
|
unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
|
|
12
|
+
private getMessageScenario;
|
|
13
|
+
setMessageScenario: (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => void;
|
|
9
14
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { isValidMessage, modifyOrigin } from "./utils";
|
|
2
2
|
import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
|
|
3
|
-
import { CommunicatorActionEnum_isCorrect } from '../enums/CommunicatorActionEnum';
|
|
3
|
+
import { CommunicatorActionEnum_isCorrect, CommunicatorActionEnum_scenario } from '../enums/CommunicatorActionEnum';
|
|
4
4
|
export class CommunicatorReceiver {
|
|
5
5
|
constructor(props) {
|
|
6
6
|
this.queue = [];
|
|
7
|
+
this.scenario = {};
|
|
7
8
|
this.onMessage = (event) => {
|
|
8
9
|
if (!event || !event.origin || !event.data) {
|
|
9
10
|
return;
|
|
@@ -22,8 +23,33 @@ export class CommunicatorReceiver {
|
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
24
25
|
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
const defaultScenario = CommunicatorActionEnum_scenario();
|
|
27
|
+
let sortedQueue = queue.sort((a, b) => {
|
|
28
|
+
if (defaultScenario.indexOf(a.action) > defaultScenario.indexOf(b.action)) {
|
|
29
|
+
return -1;
|
|
30
|
+
}
|
|
31
|
+
if (defaultScenario.indexOf(a.action) < defaultScenario.indexOf(b.action)) {
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
34
|
+
return 0;
|
|
35
|
+
});
|
|
36
|
+
const scenario = this.getMessageScenario(message.target);
|
|
37
|
+
if (defaultScenario !== scenario) {
|
|
38
|
+
sortedQueue = queue.sort((a, b) => {
|
|
39
|
+
if (scenario.indexOf(a.action) === -1 || scenario.indexOf(b.action) === -1) {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
if (scenario.indexOf(a.action) > scenario.indexOf(b.action)) {
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
if (scenario.indexOf(a.action) < scenario.indexOf(b.action)) {
|
|
46
|
+
return -1;
|
|
47
|
+
}
|
|
48
|
+
return 0;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (sortedQueue.length) {
|
|
52
|
+
sortedQueue.forEach(item => {
|
|
27
53
|
if (typeof item.params.callback === 'function') {
|
|
28
54
|
item.params.callback(message);
|
|
29
55
|
}
|
|
@@ -44,6 +70,7 @@ export class CommunicatorReceiver {
|
|
|
44
70
|
item.action === params.action;
|
|
45
71
|
})) {
|
|
46
72
|
console.log(`WAB: subscribe is exists!`);
|
|
73
|
+
return;
|
|
47
74
|
}
|
|
48
75
|
const newQueueItem = {
|
|
49
76
|
target: params.target,
|
|
@@ -58,7 +85,7 @@ export class CommunicatorReceiver {
|
|
|
58
85
|
this.queue.push(newQueueItem);
|
|
59
86
|
};
|
|
60
87
|
this.unsubscribe = (params) => {
|
|
61
|
-
if (!params) {
|
|
88
|
+
if (!params || Object.keys(params).length === 0) {
|
|
62
89
|
this.queue = [];
|
|
63
90
|
return true;
|
|
64
91
|
}
|
|
@@ -74,6 +101,27 @@ export class CommunicatorReceiver {
|
|
|
74
101
|
}
|
|
75
102
|
return false;
|
|
76
103
|
};
|
|
104
|
+
this.getMessageScenario = (target) => {
|
|
105
|
+
let scenario = this.scenario[target];
|
|
106
|
+
if (scenario) {
|
|
107
|
+
return scenario;
|
|
108
|
+
}
|
|
109
|
+
return CommunicatorActionEnum_scenario();
|
|
110
|
+
};
|
|
111
|
+
this.setMessageScenario = (target, scenario) => {
|
|
112
|
+
if (!CommunicatorTargetEnum_isCorrect(target) ||
|
|
113
|
+
!Array.isArray(scenario) ||
|
|
114
|
+
!scenario.length) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
scenario = [...new Set(scenario)];
|
|
118
|
+
scenario.forEach(item => {
|
|
119
|
+
if (!CommunicatorActionEnum_isCorrect(item)) {
|
|
120
|
+
throw new Error('WAC: failed scenario action!');
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
this.scenario[target] = scenario;
|
|
124
|
+
};
|
|
77
125
|
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
78
126
|
if (this.senderOrigin) {
|
|
79
127
|
window.addEventListener('message', this.onMessage, false);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export declare enum CommunicatorActionEnum {
|
|
2
|
+
TOKEN = "token",
|
|
3
|
+
RESIZE = "resize",
|
|
2
4
|
CLOSE = "close",
|
|
3
5
|
OPEN = "open",
|
|
4
|
-
GOTO = "goto"
|
|
5
|
-
RESIZE = "resize",
|
|
6
|
-
TOKEN = "token"
|
|
6
|
+
GOTO = "goto"
|
|
7
7
|
}
|
|
8
8
|
export declare function CommunicatorActionEnum_isCorrect(item: string): boolean;
|
|
9
|
+
export declare function CommunicatorActionEnum_scenario(): string[];
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export var CommunicatorActionEnum;
|
|
2
2
|
(function (CommunicatorActionEnum) {
|
|
3
|
+
CommunicatorActionEnum["TOKEN"] = "token";
|
|
4
|
+
CommunicatorActionEnum["RESIZE"] = "resize";
|
|
3
5
|
CommunicatorActionEnum["CLOSE"] = "close";
|
|
4
6
|
CommunicatorActionEnum["OPEN"] = "open";
|
|
5
7
|
CommunicatorActionEnum["GOTO"] = "goto";
|
|
6
|
-
CommunicatorActionEnum["RESIZE"] = "resize";
|
|
7
|
-
CommunicatorActionEnum["TOKEN"] = "token";
|
|
8
8
|
})(CommunicatorActionEnum || (CommunicatorActionEnum = {}));
|
|
9
9
|
export function CommunicatorActionEnum_isCorrect(item) {
|
|
10
10
|
return Object.values(CommunicatorActionEnum).includes(item);
|
|
11
11
|
}
|
|
12
|
+
export function CommunicatorActionEnum_scenario() {
|
|
13
|
+
return Object.values(CommunicatorActionEnum);
|
|
14
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export declare type TReceiverProps = {
|
|
|
8
8
|
senderOrigin: string;
|
|
9
9
|
};
|
|
10
10
|
export declare type TReceiverCallback = (message: TCommunicatorMessage) => void;
|
|
11
|
+
export declare type TCommunicatorActionTokenParams = {
|
|
12
|
+
name: string;
|
|
13
|
+
value: string;
|
|
14
|
+
domain: string;
|
|
15
|
+
expires?: number;
|
|
16
|
+
};
|
|
11
17
|
export declare type TCommunicatorActionResizeParams = {
|
|
12
18
|
width?: number;
|
|
13
19
|
height?: number;
|
|
@@ -17,11 +23,11 @@ export declare type TCommunicatorActionGotoParams = {
|
|
|
17
23
|
isTargetBlank?: boolean;
|
|
18
24
|
};
|
|
19
25
|
export declare type TCommunicatorSenderActionMap = {
|
|
26
|
+
[CommunicatorActionEnum.TOKEN]?: TCommunicatorActionTokenParams;
|
|
20
27
|
[CommunicatorActionEnum.RESIZE]?: TCommunicatorActionResizeParams;
|
|
21
28
|
[CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams;
|
|
22
29
|
[CommunicatorActionEnum.CLOSE]?: true;
|
|
23
30
|
[CommunicatorActionEnum.OPEN]?: CommunicatorTargetEnum;
|
|
24
|
-
[CommunicatorActionEnum.TOKEN]?: string;
|
|
25
31
|
};
|
|
26
32
|
export declare type TCommunicatorMessage = {
|
|
27
33
|
target: CommunicatorTargetEnum;
|
|
@@ -47,3 +53,6 @@ export declare type TReceiverUnsubscribeMessageParams = {
|
|
|
47
53
|
target?: CommunicatorTargetEnum;
|
|
48
54
|
action?: CommunicatorActionEnum;
|
|
49
55
|
};
|
|
56
|
+
export declare type TReceiverTargetScenario = {
|
|
57
|
+
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
58
|
+
};
|
package/package.json
CHANGED
|
@@ -3,15 +3,21 @@ import {
|
|
|
3
3
|
TReceiverMessageQueue,
|
|
4
4
|
TReceiverProps,
|
|
5
5
|
TReceiverSubscribeParams,
|
|
6
|
+
TReceiverTargetScenario,
|
|
6
7
|
TReceiverUnsubscribeMessageParams,
|
|
7
8
|
} from '../types';
|
|
8
9
|
import {isValidMessage, modifyOrigin} from "./utils";
|
|
9
|
-
import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
|
|
10
|
-
import {
|
|
10
|
+
import {CommunicatorTargetEnum, CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
|
|
11
|
+
import {
|
|
12
|
+
CommunicatorActionEnum,
|
|
13
|
+
CommunicatorActionEnum_isCorrect,
|
|
14
|
+
CommunicatorActionEnum_scenario
|
|
15
|
+
} from '../enums/CommunicatorActionEnum';
|
|
11
16
|
|
|
12
17
|
export class CommunicatorReceiver {
|
|
13
18
|
private readonly senderOrigin: string;
|
|
14
19
|
private queue: TReceiverMessageQueue = [];
|
|
20
|
+
private scenario: TReceiverTargetScenario = {};
|
|
15
21
|
|
|
16
22
|
constructor(props: TReceiverProps) {
|
|
17
23
|
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
@@ -52,8 +58,37 @@ export class CommunicatorReceiver {
|
|
|
52
58
|
// Find queue by target and action
|
|
53
59
|
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
// default sort queue
|
|
62
|
+
const defaultScenario = CommunicatorActionEnum_scenario();
|
|
63
|
+
let sortedQueue = queue.sort((a, b) => {
|
|
64
|
+
if (defaultScenario.indexOf(a.action) > defaultScenario.indexOf(b.action)) {
|
|
65
|
+
return -1;
|
|
66
|
+
}
|
|
67
|
+
if (defaultScenario.indexOf(a.action) < defaultScenario.indexOf(b.action)) {
|
|
68
|
+
return 1;
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// sort queue
|
|
74
|
+
const scenario = this.getMessageScenario(message.target);
|
|
75
|
+
if (defaultScenario !== scenario) {
|
|
76
|
+
sortedQueue = queue.sort((a, b) => {
|
|
77
|
+
if (scenario.indexOf(a.action) === -1 || scenario.indexOf(b.action) === -1) {
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
if (scenario.indexOf(a.action) > scenario.indexOf(b.action)) {
|
|
81
|
+
return 1;
|
|
82
|
+
}
|
|
83
|
+
if (scenario.indexOf(a.action) < scenario.indexOf(b.action)) {
|
|
84
|
+
return -1;
|
|
85
|
+
}
|
|
86
|
+
return 0;
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (sortedQueue.length) {
|
|
91
|
+
sortedQueue.forEach(item => {
|
|
57
92
|
if (typeof item.params.callback === 'function') {
|
|
58
93
|
item.params.callback(message);
|
|
59
94
|
}
|
|
@@ -83,6 +118,7 @@ export class CommunicatorReceiver {
|
|
|
83
118
|
item.action === params.action;
|
|
84
119
|
})) {
|
|
85
120
|
console.log(`WAB: subscribe is exists!`);
|
|
121
|
+
return;
|
|
86
122
|
}
|
|
87
123
|
|
|
88
124
|
const newQueueItem = {
|
|
@@ -105,7 +141,7 @@ export class CommunicatorReceiver {
|
|
|
105
141
|
* @param params
|
|
106
142
|
*/
|
|
107
143
|
public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
|
|
108
|
-
if (!params) {
|
|
144
|
+
if (!params || Object.keys(params).length === 0) {
|
|
109
145
|
this.queue = [];
|
|
110
146
|
return true;
|
|
111
147
|
}
|
|
@@ -124,5 +160,34 @@ export class CommunicatorReceiver {
|
|
|
124
160
|
|
|
125
161
|
return false;
|
|
126
162
|
}
|
|
163
|
+
|
|
164
|
+
private getMessageScenario = (target: CommunicatorTargetEnum) => {
|
|
165
|
+
let scenario = this.scenario[target];
|
|
166
|
+
|
|
167
|
+
if (scenario) {
|
|
168
|
+
return scenario;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return CommunicatorActionEnum_scenario();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public setMessageScenario = (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => {
|
|
175
|
+
if (
|
|
176
|
+
!CommunicatorTargetEnum_isCorrect(target) ||
|
|
177
|
+
!Array.isArray(scenario) ||
|
|
178
|
+
!scenario.length) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
scenario = [...new Set(scenario)];
|
|
183
|
+
|
|
184
|
+
scenario.forEach(item => {
|
|
185
|
+
if (!CommunicatorActionEnum_isCorrect(item)) {
|
|
186
|
+
throw new Error('WAC: failed scenario action!');
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
this.scenario[target] = scenario;
|
|
191
|
+
}
|
|
127
192
|
}
|
|
128
193
|
|
package/src/engine/utils.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export enum CommunicatorActionEnum {
|
|
2
|
+
TOKEN = 'token',
|
|
3
|
+
RESIZE = 'resize',
|
|
2
4
|
CLOSE = 'close',
|
|
3
5
|
OPEN = 'open',
|
|
4
6
|
GOTO = 'goto',
|
|
5
|
-
RESIZE = 'resize',
|
|
6
|
-
TOKEN = 'token',
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function CommunicatorActionEnum_isCorrect(item: string): boolean {
|
|
10
10
|
return Object.values(CommunicatorActionEnum).includes(item as CommunicatorActionEnum);
|
|
11
11
|
}
|
|
12
|
+
|
|
13
|
+
export function CommunicatorActionEnum_scenario(): string[] {
|
|
14
|
+
return Object.values(CommunicatorActionEnum);
|
|
15
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -12,6 +12,13 @@ export type TReceiverProps = {
|
|
|
12
12
|
|
|
13
13
|
export type TReceiverCallback = (message: TCommunicatorMessage) => void;
|
|
14
14
|
|
|
15
|
+
export type TCommunicatorActionTokenParams = {
|
|
16
|
+
name: string,
|
|
17
|
+
value: string,
|
|
18
|
+
domain: string,
|
|
19
|
+
expires?: number,
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
export type TCommunicatorActionResizeParams = {
|
|
16
23
|
width?: number,
|
|
17
24
|
height?: number,
|
|
@@ -23,11 +30,11 @@ export type TCommunicatorActionGotoParams = {
|
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
export type TCommunicatorSenderActionMap = {
|
|
33
|
+
[CommunicatorActionEnum.TOKEN]?: TCommunicatorActionTokenParams,
|
|
26
34
|
[CommunicatorActionEnum.RESIZE]?: TCommunicatorActionResizeParams,
|
|
27
35
|
[CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams,
|
|
28
36
|
[CommunicatorActionEnum.CLOSE]?: true,
|
|
29
37
|
[CommunicatorActionEnum.OPEN]?: CommunicatorTargetEnum,
|
|
30
|
-
[CommunicatorActionEnum.TOKEN]?: string,
|
|
31
38
|
}
|
|
32
39
|
|
|
33
40
|
export type TCommunicatorMessage = {
|
|
@@ -58,3 +65,7 @@ export type TReceiverUnsubscribeMessageParams = {
|
|
|
58
65
|
target?: CommunicatorTargetEnum,
|
|
59
66
|
action?: CommunicatorActionEnum,
|
|
60
67
|
}
|
|
68
|
+
|
|
69
|
+
export type TReceiverTargetScenario = {
|
|
70
|
+
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
71
|
+
}
|