@maxzima/wa-communicator 0.0.10 → 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.
@@ -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
- if (queue.length) {
26
- queue.forEach(item => {
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
  }
@@ -74,6 +100,27 @@ export class CommunicatorReceiver {
74
100
  }
75
101
  return false;
76
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) {
114
+ return;
115
+ }
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;
123
+ };
77
124
  this.senderOrigin = modifyOrigin(props.senderOrigin);
78
125
  if (this.senderOrigin) {
79
126
  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
+ }
@@ -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,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
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.10",
2
+ "version": "0.0.11",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
@@ -3,15 +3,29 @@ 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 {CommunicatorActionEnum_isCorrect} from '../enums/CommunicatorActionEnum';
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
+ // ];
11
24
 
12
25
  export class CommunicatorReceiver {
13
26
  private readonly senderOrigin: string;
14
27
  private queue: TReceiverMessageQueue = [];
28
+ private scenario: TReceiverTargetScenario = {};
15
29
 
16
30
  constructor(props: TReceiverProps) {
17
31
  this.senderOrigin = modifyOrigin(props.senderOrigin);
@@ -52,8 +66,37 @@ export class CommunicatorReceiver {
52
66
  // Find queue by target and action
53
67
  const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
54
68
 
55
- if (queue.length) {
56
- queue.forEach(item => {
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
+ })
96
+ }
97
+
98
+ if (sortedQueue.length) {
99
+ sortedQueue.forEach(item => {
57
100
  if (typeof item.params.callback === 'function') {
58
101
  item.params.callback(message);
59
102
  }
@@ -124,5 +167,34 @@ export class CommunicatorReceiver {
124
167
 
125
168
  return false;
126
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
+ }
127
199
  }
128
200
 
@@ -3,7 +3,6 @@ import {CommunicatorTargetEnum_isCorrect} from "../enums/CommunicatorTargetEnum"
3
3
  import {CommunicatorActionEnum_isCorrect} from "../enums/CommunicatorActionEnum";
4
4
 
5
5
  /**
6
- *
7
6
  * @param address
8
7
  */
9
8
  export function modifyOrigin(address: string): string {
@@ -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
+ }
@@ -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,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
+ }