@maxzima/wa-communicator 0.0.8 → 0.0.11
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 +15 -7
- package/dist/engine/CommunicatorReceiver.d.ts +9 -3
- package/dist/engine/CommunicatorReceiver.js +111 -9
- package/dist/engine/CommunicatorSender.js +8 -4
- package/dist/engine/utils.d.ts +2 -0
- package/dist/engine/utils.js +18 -0
- package/dist/enums/CommunicatorActionEnum.d.ts +4 -2
- package/dist/enums/CommunicatorActionEnum.js +5 -1
- package/dist/receiver.d.ts +2 -1
- package/dist/receiver.js +2 -1
- package/dist/types/index.d.ts +30 -1
- package/package.json +1 -1
- package/src/engine/CommunicatorReceiver.ts +166 -15
- package/src/engine/CommunicatorSender.ts +10 -6
- package/src/engine/utils.ts +30 -0
- package/src/enums/CommunicatorActionEnum.ts +6 -1
- package/src/receiver.ts +2 -0
- package/src/types/index.ts +36 -1
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -52,13 +52,21 @@ communicator.sendMessage({
|
|
|
52
52
|
|
|
53
53
|
```javascript
|
|
54
54
|
const communicator = new CommunicatorReceiver({
|
|
55
|
-
senderOrigin: 'clients.google.com'
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
senderOrigin: 'clients.google.com'
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// subscribe
|
|
59
|
+
communicator.subscribe({
|
|
60
|
+
target: CommunicatorTargetEnum.SIGN_UP,
|
|
61
|
+
action: CommunicatorActionEnum.CLOSE,
|
|
62
|
+
callback: message => {
|
|
63
|
+
console.log('close', message);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// unsubscribe
|
|
68
|
+
receiver.unsubscribe({
|
|
69
|
+
action: CommunicatorActionEnum.CLOSE
|
|
61
70
|
});
|
|
62
71
|
|
|
63
|
-
communicator.watch();
|
|
64
72
|
```
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { TReceiverProps } from '../types';
|
|
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
|
-
private readonly callback;
|
|
4
5
|
private readonly senderOrigin;
|
|
6
|
+
private queue;
|
|
7
|
+
private scenario;
|
|
5
8
|
constructor(props: TReceiverProps);
|
|
6
|
-
watch(): void;
|
|
7
9
|
private onMessage;
|
|
10
|
+
subscribe: (params: TReceiverSubscribeParams) => void;
|
|
11
|
+
unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
|
|
12
|
+
private getMessageScenario;
|
|
13
|
+
setMessageScenario: (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => void;
|
|
8
14
|
}
|
|
@@ -1,30 +1,132 @@
|
|
|
1
|
-
import { modifyOrigin } from "./utils";
|
|
1
|
+
import { isValidMessage, modifyOrigin } from "./utils";
|
|
2
|
+
import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
|
|
3
|
+
import { CommunicatorActionEnum_isCorrect, CommunicatorActionEnum_scenario } from '../enums/CommunicatorActionEnum';
|
|
2
4
|
export class CommunicatorReceiver {
|
|
3
5
|
constructor(props) {
|
|
6
|
+
this.queue = [];
|
|
7
|
+
this.scenario = {};
|
|
4
8
|
this.onMessage = (event) => {
|
|
5
9
|
if (!event || !event.origin || !event.data) {
|
|
6
10
|
return;
|
|
7
11
|
}
|
|
12
|
+
if (this.senderOrigin !== event.origin) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
8
15
|
let message;
|
|
9
16
|
try {
|
|
10
17
|
message = JSON.parse(event.data);
|
|
11
18
|
}
|
|
12
19
|
catch (error) {
|
|
13
|
-
console.log('PostMessage read error');
|
|
14
20
|
return;
|
|
15
21
|
}
|
|
16
|
-
if (
|
|
22
|
+
if (!isValidMessage(message)) {
|
|
17
23
|
return;
|
|
18
24
|
}
|
|
19
|
-
|
|
25
|
+
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
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 => {
|
|
53
|
+
if (typeof item.params.callback === 'function') {
|
|
54
|
+
item.params.callback(message);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
this.subscribe = (params) => {
|
|
60
|
+
if (params.code && typeof params.code !== 'string') {
|
|
61
|
+
throw new Error('WAC: code can only be a string!');
|
|
62
|
+
}
|
|
63
|
+
if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
|
|
64
|
+
(!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
|
|
65
|
+
throw new Error('WAC: target or action is not allowed!');
|
|
66
|
+
}
|
|
67
|
+
if (this.queue.find(item => {
|
|
68
|
+
return (!params.code || item.code === params.code) &&
|
|
69
|
+
item.target === params.target &&
|
|
70
|
+
item.action === params.action;
|
|
71
|
+
})) {
|
|
72
|
+
console.log(`WAB: subscribe is exists!`);
|
|
73
|
+
}
|
|
74
|
+
const newQueueItem = {
|
|
75
|
+
target: params.target,
|
|
76
|
+
action: params.action,
|
|
77
|
+
params: {
|
|
78
|
+
callback: params.callback,
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
if (params.code) {
|
|
82
|
+
newQueueItem['code'] = params.code;
|
|
83
|
+
}
|
|
84
|
+
this.queue.push(newQueueItem);
|
|
85
|
+
};
|
|
86
|
+
this.unsubscribe = (params) => {
|
|
87
|
+
if (!params) {
|
|
88
|
+
this.queue = [];
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
const queueForRemove = this.queue.filter(item => {
|
|
92
|
+
return (!params.code || item.code === params.code) &&
|
|
93
|
+
(!params.target || item.target === params.target) &&
|
|
94
|
+
(!params.action || item.action === params.action);
|
|
95
|
+
});
|
|
96
|
+
if (queueForRemove.length) {
|
|
97
|
+
const toRemove = new Set(queueForRemove);
|
|
98
|
+
this.queue = this.queue.filter(x => !toRemove.has(x));
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
};
|
|
103
|
+
this.getMessageScenario = (target) => {
|
|
104
|
+
let scenario = this.scenario[target];
|
|
105
|
+
if (scenario) {
|
|
106
|
+
return scenario;
|
|
107
|
+
}
|
|
108
|
+
return CommunicatorActionEnum_scenario();
|
|
109
|
+
};
|
|
110
|
+
this.setMessageScenario = (target, scenario) => {
|
|
111
|
+
if (!CommunicatorTargetEnum_isCorrect(target) ||
|
|
112
|
+
!Array.isArray(scenario) ||
|
|
113
|
+
!scenario.length) {
|
|
20
114
|
return;
|
|
21
115
|
}
|
|
22
|
-
|
|
116
|
+
scenario = [...new Set(scenario)];
|
|
117
|
+
scenario.forEach(item => {
|
|
118
|
+
if (!CommunicatorActionEnum_isCorrect(item)) {
|
|
119
|
+
throw new Error('WAC: failed scenario action!');
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
this.scenario[target] = scenario;
|
|
23
123
|
};
|
|
24
|
-
this.callback = props.callback;
|
|
25
124
|
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
125
|
+
if (this.senderOrigin) {
|
|
126
|
+
window.addEventListener('message', this.onMessage, false);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
throw new Error('WAC: senderOrigin is broken!');
|
|
130
|
+
}
|
|
29
131
|
}
|
|
30
132
|
}
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import { modifyOrigin } from './utils';
|
|
2
|
-
import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
|
|
1
|
+
import { isValidMessage, modifyOrigin } from './utils';
|
|
3
2
|
export class CommunicatorSender {
|
|
4
3
|
constructor(props) {
|
|
5
4
|
this.otherWindow = props.otherWindow;
|
|
6
5
|
this.receiverOrigin = modifyOrigin(props.receiverOrigin);
|
|
6
|
+
if (!this.receiverOrigin) {
|
|
7
|
+
throw new Error('WAC: receiverOrigin is broken!');
|
|
8
|
+
}
|
|
7
9
|
}
|
|
8
10
|
sendMessage(message) {
|
|
9
|
-
if (!this.receiverOrigin
|
|
10
|
-
!CommunicatorTargetEnum_isCorrect(message.target)) {
|
|
11
|
+
if (!this.receiverOrigin) {
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
14
|
+
if (!isValidMessage(message)) {
|
|
15
|
+
throw new Error(`WAC: Message not valid!`);
|
|
16
|
+
}
|
|
13
17
|
const jsonMessage = JSON.stringify(message);
|
|
14
18
|
this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
|
|
15
19
|
}
|
package/dist/engine/utils.d.ts
CHANGED
package/dist/engine/utils.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { CommunicatorTargetEnum_isCorrect } from "../enums/CommunicatorTargetEnum";
|
|
2
|
+
import { CommunicatorActionEnum_isCorrect } from "../enums/CommunicatorActionEnum";
|
|
1
3
|
export function modifyOrigin(address) {
|
|
2
4
|
let url;
|
|
3
5
|
try {
|
|
@@ -10,3 +12,19 @@ export function modifyOrigin(address) {
|
|
|
10
12
|
}
|
|
11
13
|
return url.origin;
|
|
12
14
|
}
|
|
15
|
+
export function isValidMessage(message) {
|
|
16
|
+
const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
|
|
17
|
+
const actions = message.action;
|
|
18
|
+
let isValidActions = typeof actions === 'object';
|
|
19
|
+
if (isValidTarget && isValidActions) {
|
|
20
|
+
for (const prop in actions) {
|
|
21
|
+
if (actions.hasOwnProperty(prop)) {
|
|
22
|
+
isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
|
|
23
|
+
if (!isValidActions) {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return isValidTarget && isValidActions;
|
|
30
|
+
}
|
|
@@ -1,7 +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
|
+
GOTO = "goto"
|
|
6
7
|
}
|
|
7
8
|
export declare function CommunicatorActionEnum_isCorrect(item: string): boolean;
|
|
9
|
+
export declare function CommunicatorActionEnum_scenario(): string[];
|
|
@@ -1,10 +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
8
|
})(CommunicatorActionEnum || (CommunicatorActionEnum = {}));
|
|
8
9
|
export function CommunicatorActionEnum_isCorrect(item) {
|
|
9
10
|
return Object.values(CommunicatorActionEnum).includes(item);
|
|
10
11
|
}
|
|
12
|
+
export function CommunicatorActionEnum_scenario() {
|
|
13
|
+
return Object.values(CommunicatorActionEnum);
|
|
14
|
+
}
|
package/dist/receiver.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ import { CommunicatorReceiver } from "./engine/CommunicatorReceiver";
|
|
|
2
2
|
import { CommunicatorTargetEnum } from "./enums/CommunicatorTargetEnum";
|
|
3
3
|
import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
|
|
4
4
|
import * as types from "./types";
|
|
5
|
-
|
|
5
|
+
import { isValidMessage as communicatorMessageIsValid } from "./engine/utils";
|
|
6
|
+
export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
|
package/dist/receiver.js
CHANGED
|
@@ -2,4 +2,5 @@ import { CommunicatorReceiver } from "./engine/CommunicatorReceiver";
|
|
|
2
2
|
import { CommunicatorTargetEnum } from "./enums/CommunicatorTargetEnum";
|
|
3
3
|
import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
|
|
4
4
|
import * as types from "./types";
|
|
5
|
-
|
|
5
|
+
import { isValidMessage as communicatorMessageIsValid } from "./engine/utils";
|
|
6
|
+
export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,10 +5,15 @@ 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;
|
|
11
|
+
export declare type TCommunicatorActionTokenParams = {
|
|
12
|
+
name: string;
|
|
13
|
+
value: string;
|
|
14
|
+
domain: string;
|
|
15
|
+
expires: string;
|
|
16
|
+
};
|
|
12
17
|
export declare type TCommunicatorActionResizeParams = {
|
|
13
18
|
width?: number;
|
|
14
19
|
height?: number;
|
|
@@ -18,6 +23,7 @@ export declare type TCommunicatorActionGotoParams = {
|
|
|
18
23
|
isTargetBlank?: boolean;
|
|
19
24
|
};
|
|
20
25
|
export declare type TCommunicatorSenderActionMap = {
|
|
26
|
+
[CommunicatorActionEnum.TOKEN]?: TCommunicatorActionTokenParams;
|
|
21
27
|
[CommunicatorActionEnum.RESIZE]?: TCommunicatorActionResizeParams;
|
|
22
28
|
[CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams;
|
|
23
29
|
[CommunicatorActionEnum.CLOSE]?: true;
|
|
@@ -27,3 +33,26 @@ export declare type TCommunicatorMessage = {
|
|
|
27
33
|
target: CommunicatorTargetEnum;
|
|
28
34
|
action: TCommunicatorSenderActionMap;
|
|
29
35
|
};
|
|
36
|
+
export declare type TReceiverSubscribeParams = {
|
|
37
|
+
code?: string;
|
|
38
|
+
target: CommunicatorTargetEnum;
|
|
39
|
+
action: CommunicatorActionEnum;
|
|
40
|
+
callback: TReceiverCallback;
|
|
41
|
+
};
|
|
42
|
+
export declare type TReceiverMessageQueue = TReceiverMessageQueueItem[];
|
|
43
|
+
export declare type TReceiverMessageQueueItem = {
|
|
44
|
+
code?: string;
|
|
45
|
+
target: CommunicatorTargetEnum;
|
|
46
|
+
action: CommunicatorActionEnum;
|
|
47
|
+
params?: {
|
|
48
|
+
callback: TReceiverCallback;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export declare type TReceiverUnsubscribeMessageParams = {
|
|
52
|
+
code?: string;
|
|
53
|
+
target?: CommunicatorTargetEnum;
|
|
54
|
+
action?: CommunicatorActionEnum;
|
|
55
|
+
};
|
|
56
|
+
export declare type TReceiverTargetScenario = {
|
|
57
|
+
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
58
|
+
};
|
package/package.json
CHANGED
|
@@ -1,20 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
TCommunicatorMessage,
|
|
3
|
+
TReceiverMessageQueue,
|
|
4
|
+
TReceiverProps,
|
|
5
|
+
TReceiverSubscribeParams,
|
|
6
|
+
TReceiverTargetScenario,
|
|
7
|
+
TReceiverUnsubscribeMessageParams,
|
|
8
|
+
} from '../types';
|
|
9
|
+
import {isValidMessage, modifyOrigin} from "./utils";
|
|
10
|
+
import {CommunicatorTargetEnum, CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
|
|
11
|
+
import {
|
|
12
|
+
CommunicatorActionEnum,
|
|
13
|
+
CommunicatorActionEnum_isCorrect,
|
|
14
|
+
CommunicatorActionEnum_scenario
|
|
15
|
+
} from '../enums/CommunicatorActionEnum';
|
|
16
|
+
|
|
17
|
+
// const defaultScenario = [
|
|
18
|
+
// CommunicatorActionEnum.TOKEN,
|
|
19
|
+
// CommunicatorActionEnum.RESIZE,
|
|
20
|
+
// CommunicatorActionEnum.GOTO,
|
|
21
|
+
// CommunicatorActionEnum.CLOSE,
|
|
22
|
+
// CommunicatorActionEnum.OPEN,
|
|
23
|
+
// ];
|
|
3
24
|
|
|
4
25
|
export class CommunicatorReceiver {
|
|
5
|
-
private readonly callback: TReceiverCallback;
|
|
6
26
|
private readonly senderOrigin: string;
|
|
27
|
+
private queue: TReceiverMessageQueue = [];
|
|
28
|
+
private scenario: TReceiverTargetScenario = {};
|
|
7
29
|
|
|
8
30
|
constructor(props: TReceiverProps) {
|
|
9
|
-
this.callback = props.callback;
|
|
10
31
|
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
11
|
-
}
|
|
12
32
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
33
|
+
if (this.senderOrigin) {
|
|
34
|
+
// Watch Messages
|
|
35
|
+
window.addEventListener('message', this.onMessage, false);
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error('WAC: senderOrigin is broken!');
|
|
38
|
+
}
|
|
18
39
|
}
|
|
19
40
|
|
|
20
41
|
/**
|
|
@@ -26,24 +47,154 @@ export class CommunicatorReceiver {
|
|
|
26
47
|
return;
|
|
27
48
|
}
|
|
28
49
|
|
|
50
|
+
if (this.senderOrigin !== event.origin) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
29
54
|
let message: TCommunicatorMessage;
|
|
30
55
|
|
|
31
56
|
try {
|
|
32
57
|
message = JSON.parse(event.data);
|
|
33
58
|
} catch (error) {
|
|
34
|
-
console.log('PostMessage read error');
|
|
35
59
|
return;
|
|
36
60
|
}
|
|
37
61
|
|
|
38
|
-
if (
|
|
62
|
+
if (!isValidMessage(message)) {
|
|
39
63
|
return;
|
|
40
64
|
}
|
|
41
65
|
|
|
42
|
-
|
|
43
|
-
|
|
66
|
+
// Find queue by target and action
|
|
67
|
+
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
68
|
+
|
|
69
|
+
// default sort queue
|
|
70
|
+
const defaultScenario = CommunicatorActionEnum_scenario();
|
|
71
|
+
let sortedQueue = queue.sort((a, b) => {
|
|
72
|
+
if (defaultScenario.indexOf(a.action) > defaultScenario.indexOf(b.action)) {
|
|
73
|
+
return -1;
|
|
74
|
+
}
|
|
75
|
+
if (defaultScenario.indexOf(a.action) < defaultScenario.indexOf(b.action)) {
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
return 0;
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// sort queue
|
|
82
|
+
const scenario = this.getMessageScenario(message.target);
|
|
83
|
+
if (defaultScenario !== scenario) {
|
|
84
|
+
sortedQueue = queue.sort((a, b) => {
|
|
85
|
+
if (scenario.indexOf(a.action) === -1 || scenario.indexOf(b.action) === -1) {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
if (scenario.indexOf(a.action) > scenario.indexOf(b.action)) {
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
if (scenario.indexOf(a.action) < scenario.indexOf(b.action)) {
|
|
92
|
+
return -1;
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
})
|
|
44
96
|
}
|
|
45
97
|
|
|
46
|
-
|
|
98
|
+
if (sortedQueue.length) {
|
|
99
|
+
sortedQueue.forEach(item => {
|
|
100
|
+
if (typeof item.params.callback === 'function') {
|
|
101
|
+
item.params.callback(message);
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
}
|
|
47
105
|
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Subscribe by target and action
|
|
109
|
+
* @param params
|
|
110
|
+
*/
|
|
111
|
+
public subscribe = (params: TReceiverSubscribeParams): void => {
|
|
112
|
+
if (params.code && typeof params.code !== 'string') {
|
|
113
|
+
throw new Error('WAC: code can only be a string!');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (
|
|
117
|
+
(!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
|
|
118
|
+
(!params.action || !CommunicatorActionEnum_isCorrect(params.action))
|
|
119
|
+
) {
|
|
120
|
+
throw new Error('WAC: target or action is not allowed!');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (this.queue.find(item => {
|
|
124
|
+
return (!params.code || item.code === params.code) &&
|
|
125
|
+
item.target === params.target &&
|
|
126
|
+
item.action === params.action;
|
|
127
|
+
})) {
|
|
128
|
+
console.log(`WAB: subscribe is exists!`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const newQueueItem = {
|
|
132
|
+
target: params.target,
|
|
133
|
+
action: params.action,
|
|
134
|
+
params: {
|
|
135
|
+
callback: params.callback,
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
if (params.code) {
|
|
140
|
+
newQueueItem['code'] = params.code;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this.queue.push(newQueueItem);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Subscribe by code or target or action
|
|
148
|
+
* @param params
|
|
149
|
+
*/
|
|
150
|
+
public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
|
|
151
|
+
if (!params) {
|
|
152
|
+
this.queue = [];
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const queueForRemove = this.queue.filter(item => {
|
|
157
|
+
return (!params.code || item.code === params.code) &&
|
|
158
|
+
(!params.target || item.target === params.target) &&
|
|
159
|
+
(!params.action || item.action === params.action);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (queueForRemove.length) {
|
|
163
|
+
const toRemove = new Set(queueForRemove);
|
|
164
|
+
this.queue = this.queue.filter(x => !toRemove.has(x));
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private getMessageScenario = (target: CommunicatorTargetEnum) => {
|
|
172
|
+
let scenario = this.scenario[target];
|
|
173
|
+
|
|
174
|
+
if (scenario) {
|
|
175
|
+
return scenario;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return CommunicatorActionEnum_scenario();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public setMessageScenario = (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => {
|
|
182
|
+
if (
|
|
183
|
+
!CommunicatorTargetEnum_isCorrect(target) ||
|
|
184
|
+
!Array.isArray(scenario) ||
|
|
185
|
+
!scenario.length) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
scenario = [...new Set(scenario)];
|
|
190
|
+
|
|
191
|
+
scenario.forEach(item => {
|
|
192
|
+
if (!CommunicatorActionEnum_isCorrect(item)) {
|
|
193
|
+
throw new Error('WAC: failed scenario action!');
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
this.scenario[target] = scenario;
|
|
198
|
+
}
|
|
48
199
|
}
|
|
49
200
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {TCommunicatorMessage, TSenderProps} from '../types';
|
|
2
|
-
import {modifyOrigin} from './utils';
|
|
3
|
-
import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
|
|
2
|
+
import {isValidMessage, modifyOrigin} from './utils';
|
|
4
3
|
|
|
5
4
|
export class CommunicatorSender {
|
|
6
5
|
private readonly otherWindow: Window;
|
|
@@ -9,6 +8,10 @@ export class CommunicatorSender {
|
|
|
9
8
|
constructor(props: TSenderProps) {
|
|
10
9
|
this.otherWindow = props.otherWindow;
|
|
11
10
|
this.receiverOrigin = modifyOrigin(props.receiverOrigin);
|
|
11
|
+
|
|
12
|
+
if (!this.receiverOrigin) {
|
|
13
|
+
throw new Error('WAC: receiverOrigin is broken!');
|
|
14
|
+
}
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
/**
|
|
@@ -16,13 +19,14 @@ export class CommunicatorSender {
|
|
|
16
19
|
* @param message
|
|
17
20
|
*/
|
|
18
21
|
public sendMessage(message: TCommunicatorMessage) {
|
|
19
|
-
if (
|
|
20
|
-
!this.receiverOrigin ||
|
|
21
|
-
!CommunicatorTargetEnum_isCorrect(message.target)
|
|
22
|
-
) {
|
|
22
|
+
if (!this.receiverOrigin) {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
if (!isValidMessage(message)) {
|
|
27
|
+
throw new Error(`WAC: Message not valid!`);
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
const jsonMessage = JSON.stringify(message);
|
|
27
31
|
this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
|
|
28
32
|
}
|
package/src/engine/utils.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {TCommunicatorMessage} from "../types";
|
|
2
|
+
import {CommunicatorTargetEnum_isCorrect} from "../enums/CommunicatorTargetEnum";
|
|
3
|
+
import {CommunicatorActionEnum_isCorrect} from "../enums/CommunicatorActionEnum";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param address
|
|
7
|
+
*/
|
|
1
8
|
export function modifyOrigin(address: string): string {
|
|
2
9
|
let url: URL;
|
|
3
10
|
try {
|
|
@@ -9,3 +16,26 @@ export function modifyOrigin(address: string): string {
|
|
|
9
16
|
}
|
|
10
17
|
return url.origin;
|
|
11
18
|
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Check the message for validity
|
|
22
|
+
* @param message
|
|
23
|
+
*/
|
|
24
|
+
export function isValidMessage(message: TCommunicatorMessage) {
|
|
25
|
+
const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
|
|
26
|
+
const actions = message.action;
|
|
27
|
+
let isValidActions = typeof actions === 'object';
|
|
28
|
+
|
|
29
|
+
if (isValidTarget && isValidActions) {
|
|
30
|
+
for (const prop in actions) {
|
|
31
|
+
if(actions.hasOwnProperty(prop)) {
|
|
32
|
+
isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
|
|
33
|
+
if (!isValidActions) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return isValidTarget && isValidActions;
|
|
41
|
+
}
|
|
@@ -1,10 +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
7
|
}
|
|
7
8
|
|
|
8
9
|
export function CommunicatorActionEnum_isCorrect(item: string): boolean {
|
|
9
10
|
return Object.values(CommunicatorActionEnum).includes(item as CommunicatorActionEnum);
|
|
10
11
|
}
|
|
12
|
+
|
|
13
|
+
export function CommunicatorActionEnum_scenario(): string[] {
|
|
14
|
+
return Object.values(CommunicatorActionEnum);
|
|
15
|
+
}
|
package/src/receiver.ts
CHANGED
|
@@ -2,10 +2,12 @@ import {CommunicatorReceiver} from "./engine/CommunicatorReceiver"
|
|
|
2
2
|
import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum"
|
|
3
3
|
import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum"
|
|
4
4
|
import * as types from "./types";
|
|
5
|
+
import {isValidMessage as communicatorMessageIsValid} from "./engine/utils";
|
|
5
6
|
|
|
6
7
|
export {
|
|
7
8
|
CommunicatorReceiver,
|
|
8
9
|
CommunicatorTargetEnum,
|
|
9
10
|
CommunicatorActionEnum,
|
|
10
11
|
types,
|
|
12
|
+
communicatorMessageIsValid
|
|
11
13
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -7,12 +7,18 @@ export type TSenderProps = {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export type TReceiverProps = {
|
|
10
|
-
callback: TReceiverCallback,
|
|
11
10
|
senderOrigin: string,
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
export type TReceiverCallback = (message: TCommunicatorMessage) => void;
|
|
15
14
|
|
|
15
|
+
export type TCommunicatorActionTokenParams = {
|
|
16
|
+
name: string,
|
|
17
|
+
value: string,
|
|
18
|
+
domain: string,
|
|
19
|
+
expires: string,
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
export type TCommunicatorActionResizeParams = {
|
|
17
23
|
width?: number,
|
|
18
24
|
height?: number,
|
|
@@ -24,6 +30,7 @@ export type TCommunicatorActionGotoParams = {
|
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
export type TCommunicatorSenderActionMap = {
|
|
33
|
+
[CommunicatorActionEnum.TOKEN]?: TCommunicatorActionTokenParams,
|
|
27
34
|
[CommunicatorActionEnum.RESIZE]?: TCommunicatorActionResizeParams,
|
|
28
35
|
[CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams,
|
|
29
36
|
[CommunicatorActionEnum.CLOSE]?: true,
|
|
@@ -34,3 +41,31 @@ export type TCommunicatorMessage = {
|
|
|
34
41
|
target: CommunicatorTargetEnum,
|
|
35
42
|
action: TCommunicatorSenderActionMap
|
|
36
43
|
}
|
|
44
|
+
|
|
45
|
+
export type TReceiverSubscribeParams = {
|
|
46
|
+
code?: string;
|
|
47
|
+
target: CommunicatorTargetEnum,
|
|
48
|
+
action: CommunicatorActionEnum,
|
|
49
|
+
callback: TReceiverCallback,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
|
|
53
|
+
|
|
54
|
+
export type TReceiverMessageQueueItem = {
|
|
55
|
+
code?: string;
|
|
56
|
+
target: CommunicatorTargetEnum,
|
|
57
|
+
action: CommunicatorActionEnum,
|
|
58
|
+
params?: {
|
|
59
|
+
callback: TReceiverCallback,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type TReceiverUnsubscribeMessageParams = {
|
|
64
|
+
code?: string;
|
|
65
|
+
target?: CommunicatorTargetEnum,
|
|
66
|
+
action?: CommunicatorActionEnum,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type TReceiverTargetScenario = {
|
|
70
|
+
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
71
|
+
}
|