@maxzima/wa-communicator 0.0.9 → 0.0.12
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 +2 -3
- package/dist/engine/CommunicatorReceiver.d.ts +7 -4
- package/dist/engine/CommunicatorReceiver.js +72 -37
- package/dist/engine/CommunicatorSender.js +3 -8
- 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 +12 -1
- package/package.json +1 -1
- package/src/engine/CommunicatorReceiver.ts +95 -53
- package/src/engine/CommunicatorSender.ts +3 -11
- 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 +14 -1
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ const communicator = new CommunicatorReceiver({
|
|
|
56
56
|
});
|
|
57
57
|
|
|
58
58
|
// subscribe
|
|
59
|
-
communicator.
|
|
59
|
+
communicator.subscribe({
|
|
60
60
|
target: CommunicatorTargetEnum.SIGN_UP,
|
|
61
61
|
action: CommunicatorActionEnum.CLOSE,
|
|
62
62
|
callback: message => {
|
|
@@ -65,8 +65,7 @@ communicator.follow('one', {
|
|
|
65
65
|
});
|
|
66
66
|
|
|
67
67
|
// unsubscribe
|
|
68
|
-
receiver.
|
|
69
|
-
code: 'one',
|
|
68
|
+
receiver.unsubscribe({
|
|
70
69
|
action: CommunicatorActionEnum.CLOSE
|
|
71
70
|
});
|
|
72
71
|
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
8
|
-
isValidMessage: (message: TCommunicatorMessage) => boolean;
|
|
9
|
-
subscribe: (code: string, params: TReceiverSubscribeParams) => void;
|
|
10
|
+
subscribe: (params: TReceiverSubscribeParams) => void;
|
|
10
11
|
unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
|
|
12
|
+
private getMessageScenario;
|
|
13
|
+
setMessageScenario: (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => void;
|
|
11
14
|
}
|
|
@@ -1,82 +1,96 @@
|
|
|
1
|
-
import { modifyOrigin } from "./utils";
|
|
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;
|
|
10
11
|
}
|
|
12
|
+
if (this.senderOrigin !== event.origin) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
11
15
|
let message;
|
|
12
16
|
try {
|
|
13
17
|
message = JSON.parse(event.data);
|
|
14
18
|
}
|
|
15
19
|
catch (error) {
|
|
16
|
-
console.log('PostMessage read error');
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (this.senderOrigin !== event.origin) {
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
|
-
if (!
|
|
22
|
+
if (!isValidMessage(message)) {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
26
|
-
|
|
27
|
-
|
|
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 => {
|
|
28
53
|
if (typeof item.params.callback === 'function') {
|
|
29
54
|
item.params.callback(message);
|
|
30
55
|
}
|
|
31
56
|
});
|
|
32
57
|
}
|
|
33
58
|
};
|
|
34
|
-
this.
|
|
35
|
-
if (typeof code
|
|
36
|
-
throw new Error('WAC: code
|
|
59
|
+
this.subscribe = (params) => {
|
|
60
|
+
if (params.code && typeof params.code !== 'string') {
|
|
61
|
+
throw new Error('WAC: code can only be a string!');
|
|
37
62
|
}
|
|
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
63
|
if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
|
|
58
64
|
(!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
|
|
59
65
|
throw new Error('WAC: target or action is not allowed!');
|
|
60
66
|
}
|
|
61
|
-
if (this.queue.find(item =>
|
|
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
|
+
})) {
|
|
62
72
|
console.log(`WAB: subscribe is exists!`);
|
|
73
|
+
return;
|
|
63
74
|
}
|
|
64
|
-
|
|
65
|
-
code: this.prepareCode(code),
|
|
75
|
+
const newQueueItem = {
|
|
66
76
|
target: params.target,
|
|
67
77
|
action: params.action,
|
|
68
78
|
params: {
|
|
69
79
|
callback: params.callback,
|
|
70
80
|
}
|
|
71
|
-
}
|
|
81
|
+
};
|
|
82
|
+
if (params.code) {
|
|
83
|
+
newQueueItem['code'] = params.code;
|
|
84
|
+
}
|
|
85
|
+
this.queue.push(newQueueItem);
|
|
72
86
|
};
|
|
73
87
|
this.unsubscribe = (params) => {
|
|
74
|
-
if (!params) {
|
|
88
|
+
if (!params || Object.keys(params).length === 0) {
|
|
75
89
|
this.queue = [];
|
|
76
90
|
return true;
|
|
77
91
|
}
|
|
78
92
|
const queueForRemove = this.queue.filter(item => {
|
|
79
|
-
return (!params.code || item.code ===
|
|
93
|
+
return (!params.code || item.code === params.code) &&
|
|
80
94
|
(!params.target || item.target === params.target) &&
|
|
81
95
|
(!params.action || item.action === params.action);
|
|
82
96
|
});
|
|
@@ -87,6 +101,27 @@ export class CommunicatorReceiver {
|
|
|
87
101
|
}
|
|
88
102
|
return false;
|
|
89
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
|
+
};
|
|
90
125
|
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
91
126
|
if (this.senderOrigin) {
|
|
92
127
|
window.addEventListener('message', this.onMessage, false);
|
|
@@ -1,5 +1,4 @@
|
|
|
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;
|
|
@@ -12,12 +11,8 @@ export class CommunicatorSender {
|
|
|
12
11
|
if (!this.receiverOrigin) {
|
|
13
12
|
return;
|
|
14
13
|
}
|
|
15
|
-
if (!
|
|
16
|
-
throw new Error(`WAC:
|
|
17
|
-
}
|
|
18
|
-
if (!this.receiverOrigin ||
|
|
19
|
-
!CommunicatorTargetEnum_isCorrect(message.target)) {
|
|
20
|
-
return;
|
|
14
|
+
if (!isValidMessage(message)) {
|
|
15
|
+
throw new Error(`WAC: Message not valid!`);
|
|
21
16
|
}
|
|
22
17
|
const jsonMessage = JSON.stringify(message);
|
|
23
18
|
this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
|
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
|
@@ -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?: string;
|
|
16
|
+
};
|
|
11
17
|
export declare type TCommunicatorActionResizeParams = {
|
|
12
18
|
width?: number;
|
|
13
19
|
height?: number;
|
|
@@ -17,6 +23,7 @@ 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;
|
|
@@ -27,13 +34,14 @@ export declare type TCommunicatorMessage = {
|
|
|
27
34
|
action: TCommunicatorSenderActionMap;
|
|
28
35
|
};
|
|
29
36
|
export declare type TReceiverSubscribeParams = {
|
|
37
|
+
code?: string;
|
|
30
38
|
target: CommunicatorTargetEnum;
|
|
31
39
|
action: CommunicatorActionEnum;
|
|
32
40
|
callback: TReceiverCallback;
|
|
33
41
|
};
|
|
34
42
|
export declare type TReceiverMessageQueue = TReceiverMessageQueueItem[];
|
|
35
43
|
export declare type TReceiverMessageQueueItem = {
|
|
36
|
-
code
|
|
44
|
+
code?: string;
|
|
37
45
|
target: CommunicatorTargetEnum;
|
|
38
46
|
action: CommunicatorActionEnum;
|
|
39
47
|
params?: {
|
|
@@ -45,3 +53,6 @@ export declare type TReceiverUnsubscribeMessageParams = {
|
|
|
45
53
|
target?: CommunicatorTargetEnum;
|
|
46
54
|
action?: CommunicatorActionEnum;
|
|
47
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
|
-
import {modifyOrigin} from "./utils";
|
|
9
|
-
import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
|
|
10
|
-
import {
|
|
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';
|
|
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);
|
|
@@ -33,75 +39,72 @@ export class CommunicatorReceiver {
|
|
|
33
39
|
return;
|
|
34
40
|
}
|
|
35
41
|
|
|
42
|
+
if (this.senderOrigin !== event.origin) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
36
46
|
let message: TCommunicatorMessage;
|
|
37
47
|
|
|
38
48
|
try {
|
|
39
49
|
message = JSON.parse(event.data);
|
|
40
50
|
} catch (error) {
|
|
41
|
-
console.log('PostMessage read error');
|
|
42
51
|
return;
|
|
43
52
|
}
|
|
44
53
|
|
|
45
|
-
if (
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!this.isValidMessage(message)) {
|
|
54
|
+
if (!isValidMessage(message)) {
|
|
50
55
|
return;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
// Find queue by target and action
|
|
54
59
|
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
55
60
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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;
|
|
60
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;
|
|
61
87
|
})
|
|
62
88
|
}
|
|
63
|
-
};
|
|
64
89
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
}
|
|
90
|
+
if (sortedQueue.length) {
|
|
91
|
+
sortedQueue.forEach(item => {
|
|
92
|
+
if (typeof item.params.callback === 'function') {
|
|
93
|
+
item.params.callback(message);
|
|
92
94
|
}
|
|
93
|
-
}
|
|
95
|
+
})
|
|
94
96
|
}
|
|
95
|
-
|
|
96
|
-
return isValidTarget && isValidActions;
|
|
97
|
-
}
|
|
97
|
+
};
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* Subscribe by target and action
|
|
101
|
-
* @param code
|
|
102
101
|
* @param params
|
|
103
102
|
*/
|
|
104
|
-
public subscribe = (
|
|
103
|
+
public subscribe = (params: TReceiverSubscribeParams): void => {
|
|
104
|
+
if (params.code && typeof params.code !== 'string') {
|
|
105
|
+
throw new Error('WAC: code can only be a string!');
|
|
106
|
+
}
|
|
107
|
+
|
|
105
108
|
if (
|
|
106
109
|
(!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
|
|
107
110
|
(!params.action || !CommunicatorActionEnum_isCorrect(params.action))
|
|
@@ -109,18 +112,28 @@ export class CommunicatorReceiver {
|
|
|
109
112
|
throw new Error('WAC: target or action is not allowed!');
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
if (this.queue.find(item =>
|
|
115
|
+
if (this.queue.find(item => {
|
|
116
|
+
return (!params.code || item.code === params.code) &&
|
|
117
|
+
item.target === params.target &&
|
|
118
|
+
item.action === params.action;
|
|
119
|
+
})) {
|
|
113
120
|
console.log(`WAB: subscribe is exists!`);
|
|
121
|
+
return;
|
|
114
122
|
}
|
|
115
123
|
|
|
116
|
-
|
|
117
|
-
code: this.prepareCode(code),
|
|
124
|
+
const newQueueItem = {
|
|
118
125
|
target: params.target,
|
|
119
126
|
action: params.action,
|
|
120
127
|
params: {
|
|
121
128
|
callback: params.callback,
|
|
122
129
|
}
|
|
123
|
-
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if (params.code) {
|
|
133
|
+
newQueueItem['code'] = params.code;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.queue.push(newQueueItem);
|
|
124
137
|
}
|
|
125
138
|
|
|
126
139
|
/**
|
|
@@ -128,13 +141,13 @@ export class CommunicatorReceiver {
|
|
|
128
141
|
* @param params
|
|
129
142
|
*/
|
|
130
143
|
public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
|
|
131
|
-
if (!params) {
|
|
144
|
+
if (!params || Object.keys(params).length === 0) {
|
|
132
145
|
this.queue = [];
|
|
133
146
|
return true;
|
|
134
147
|
}
|
|
135
148
|
|
|
136
149
|
const queueForRemove = this.queue.filter(item => {
|
|
137
|
-
return (!params.code || item.code ===
|
|
150
|
+
return (!params.code || item.code === params.code) &&
|
|
138
151
|
(!params.target || item.target === params.target) &&
|
|
139
152
|
(!params.action || item.action === params.action);
|
|
140
153
|
});
|
|
@@ -147,5 +160,34 @@ export class CommunicatorReceiver {
|
|
|
147
160
|
|
|
148
161
|
return false;
|
|
149
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
|
+
}
|
|
150
192
|
}
|
|
151
193
|
|
|
@@ -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;
|
|
@@ -24,15 +23,8 @@ export class CommunicatorSender {
|
|
|
24
23
|
return;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
if (!
|
|
28
|
-
throw new Error(`WAC:
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
!this.receiverOrigin ||
|
|
33
|
-
!CommunicatorTargetEnum_isCorrect(message.target)
|
|
34
|
-
) {
|
|
35
|
-
return;
|
|
26
|
+
if (!isValidMessage(message)) {
|
|
27
|
+
throw new Error(`WAC: Message not valid!`);
|
|
36
28
|
}
|
|
37
29
|
|
|
38
30
|
const jsonMessage = JSON.stringify(message);
|
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
|
@@ -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?: string,
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
export type TCommunicatorActionResizeParams = {
|
|
16
23
|
width?: number,
|
|
17
24
|
height?: number,
|
|
@@ -23,6 +30,7 @@ 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,
|
|
@@ -35,6 +43,7 @@ export type TCommunicatorMessage = {
|
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
export type TReceiverSubscribeParams = {
|
|
46
|
+
code?: string;
|
|
38
47
|
target: CommunicatorTargetEnum,
|
|
39
48
|
action: CommunicatorActionEnum,
|
|
40
49
|
callback: TReceiverCallback,
|
|
@@ -43,7 +52,7 @@ export type TReceiverSubscribeParams = {
|
|
|
43
52
|
export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
|
|
44
53
|
|
|
45
54
|
export type TReceiverMessageQueueItem = {
|
|
46
|
-
code
|
|
55
|
+
code?: string;
|
|
47
56
|
target: CommunicatorTargetEnum,
|
|
48
57
|
action: CommunicatorActionEnum,
|
|
49
58
|
params?: {
|
|
@@ -56,3 +65,7 @@ export type TReceiverUnsubscribeMessageParams = {
|
|
|
56
65
|
target?: CommunicatorTargetEnum,
|
|
57
66
|
action?: CommunicatorActionEnum,
|
|
58
67
|
}
|
|
68
|
+
|
|
69
|
+
export type TReceiverTargetScenario = {
|
|
70
|
+
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
71
|
+
}
|