@etsoo/appscript 1.2.52 → 1.2.53

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.
@@ -7,13 +7,12 @@ const FlutterHost_1 = require("./FlutterHost");
7
7
  */
8
8
  var BridgeUtils;
9
9
  (function (BridgeUtils) {
10
- var _a;
11
10
  const g = globalThis;
12
11
  /**
13
12
  * Bridge host
14
13
  */
15
- BridgeUtils.host = typeof ((_a = g.flutter_inappwebview) === null || _a === void 0 ? void 0 : _a.callHandler) === 'function'
16
- ? new FlutterHost_1.FlutterHost(g.flutter_inappwebview.callHandler)
14
+ BridgeUtils.host = typeof g.flutter_inappwebview === 'object'
15
+ ? new FlutterHost_1.FlutterHost(g.flutter_inappwebview)
17
16
  : typeof g.electron === 'object'
18
17
  ? g.electron
19
18
  : undefined;
@@ -1,22 +1,31 @@
1
1
  import { IBridgeHost } from './IBridgeHost';
2
+ declare type CallHandlerType = (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
2
3
  /**
3
4
  * Flutter JavaScript Host
4
5
  * https://inappwebview.dev/docs/javascript/communication/
5
6
  */
6
7
  export declare class FlutterHost implements IBridgeHost {
7
- callHandler: (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
8
+ private host;
8
9
  /**
9
10
  * Start Url
10
11
  */
11
12
  private startUrl;
13
+ /**
14
+ * Cached commands
15
+ */
16
+ private cachedCommands;
12
17
  /**
13
18
  * Constructor
14
19
  * @param callHandler Call handler
15
20
  */
16
- constructor(callHandler: (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>);
21
+ constructor(host: {
22
+ callHandler?: CallHandlerType;
23
+ });
24
+ cacheCommand(name: string, ...args: unknown[]): void;
17
25
  changeCulture(locale: string): void;
18
26
  exit(): void;
19
27
  getLabels<T extends string>(...keys: T[]): Promise<any>;
20
28
  getStartUrl(): string | null | undefined;
21
29
  loadApp(name: string, startUrl?: string): void;
22
30
  }
31
+ export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlutterHost = void 0;
4
+ const shared_1 = require("@etsoo/shared");
4
5
  /**
5
6
  * Flutter JavaScript Host
6
7
  * https://inappwebview.dev/docs/javascript/communication/
@@ -10,19 +11,54 @@ class FlutterHost {
10
11
  * Constructor
11
12
  * @param callHandler Call handler
12
13
  */
13
- constructor(callHandler) {
14
- this.callHandler = callHandler;
14
+ constructor(host) {
15
+ this.host = host;
16
+ /**
17
+ * Cached commands
18
+ */
19
+ this.cachedCommands = {};
20
+ window.addEventListener('flutterInAppWebViewPlatformReady', (_event) => {
21
+ if (this.host.callHandler == null)
22
+ return;
23
+ for (const key in this.cachedCommands) {
24
+ // Args
25
+ const args = this.cachedCommands[key];
26
+ // Execute
27
+ this.host.callHandler(key, ...args);
28
+ // Remove the key
29
+ delete this.cachedCommands[key];
30
+ }
31
+ });
32
+ }
33
+ cacheCommand(name, ...args) {
34
+ this.cachedCommands[name] = args;
15
35
  }
16
36
  changeCulture(locale) {
17
- this.callHandler('changeCulture', locale);
37
+ if (this.host.callHandler)
38
+ this.host.callHandler('changeCulture', locale);
39
+ else
40
+ this.cacheCommand('changeCulture', locale);
18
41
  }
19
42
  exit() {
20
- this.callHandler('exit');
43
+ if (this.host.callHandler)
44
+ this.host.callHandler('exit');
45
+ else
46
+ this.cacheCommand('exit');
21
47
  }
22
48
  async getLabels(...keys) {
23
49
  var _a;
50
+ // Try 500 miliseconds
51
+ let count = 5;
52
+ while (this.host.callHandler == null) {
53
+ count--;
54
+ await shared_1.ExtendUtils.sleep(100);
55
+ if (count === 0)
56
+ break;
57
+ }
24
58
  const init = {};
25
- const result = (_a = (await this.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
59
+ if (this.host.callHandler == null)
60
+ return init;
61
+ const result = (_a = (await this.host.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
26
62
  return keys.reduce((a, v) => {
27
63
  var _a;
28
64
  return ({
@@ -36,7 +72,10 @@ class FlutterHost {
36
72
  }
37
73
  loadApp(name, startUrl) {
38
74
  this.startUrl = startUrl;
39
- this.callHandler('loadApp', name, startUrl);
75
+ if (this.host.callHandler)
76
+ this.host.callHandler('loadApp', name, startUrl);
77
+ else
78
+ this.cacheCommand('loadApp', name, startUrl);
40
79
  }
41
80
  }
42
81
  exports.FlutterHost = FlutterHost;
@@ -4,13 +4,12 @@ import { FlutterHost } from './FlutterHost';
4
4
  */
5
5
  export var BridgeUtils;
6
6
  (function (BridgeUtils) {
7
- var _a;
8
7
  const g = globalThis;
9
8
  /**
10
9
  * Bridge host
11
10
  */
12
- BridgeUtils.host = typeof ((_a = g.flutter_inappwebview) === null || _a === void 0 ? void 0 : _a.callHandler) === 'function'
13
- ? new FlutterHost(g.flutter_inappwebview.callHandler)
11
+ BridgeUtils.host = typeof g.flutter_inappwebview === 'object'
12
+ ? new FlutterHost(g.flutter_inappwebview)
14
13
  : typeof g.electron === 'object'
15
14
  ? g.electron
16
15
  : undefined;
@@ -1,22 +1,31 @@
1
1
  import { IBridgeHost } from './IBridgeHost';
2
+ declare type CallHandlerType = (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
2
3
  /**
3
4
  * Flutter JavaScript Host
4
5
  * https://inappwebview.dev/docs/javascript/communication/
5
6
  */
6
7
  export declare class FlutterHost implements IBridgeHost {
7
- callHandler: (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
8
+ private host;
8
9
  /**
9
10
  * Start Url
10
11
  */
11
12
  private startUrl;
13
+ /**
14
+ * Cached commands
15
+ */
16
+ private cachedCommands;
12
17
  /**
13
18
  * Constructor
14
19
  * @param callHandler Call handler
15
20
  */
16
- constructor(callHandler: (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>);
21
+ constructor(host: {
22
+ callHandler?: CallHandlerType;
23
+ });
24
+ cacheCommand(name: string, ...args: unknown[]): void;
17
25
  changeCulture(locale: string): void;
18
26
  exit(): void;
19
27
  getLabels<T extends string>(...keys: T[]): Promise<any>;
20
28
  getStartUrl(): string | null | undefined;
21
29
  loadApp(name: string, startUrl?: string): void;
22
30
  }
31
+ export {};
@@ -1,3 +1,4 @@
1
+ import { ExtendUtils } from '@etsoo/shared';
1
2
  /**
2
3
  * Flutter JavaScript Host
3
4
  * https://inappwebview.dev/docs/javascript/communication/
@@ -7,19 +8,54 @@ export class FlutterHost {
7
8
  * Constructor
8
9
  * @param callHandler Call handler
9
10
  */
10
- constructor(callHandler) {
11
- this.callHandler = callHandler;
11
+ constructor(host) {
12
+ this.host = host;
13
+ /**
14
+ * Cached commands
15
+ */
16
+ this.cachedCommands = {};
17
+ window.addEventListener('flutterInAppWebViewPlatformReady', (_event) => {
18
+ if (this.host.callHandler == null)
19
+ return;
20
+ for (const key in this.cachedCommands) {
21
+ // Args
22
+ const args = this.cachedCommands[key];
23
+ // Execute
24
+ this.host.callHandler(key, ...args);
25
+ // Remove the key
26
+ delete this.cachedCommands[key];
27
+ }
28
+ });
29
+ }
30
+ cacheCommand(name, ...args) {
31
+ this.cachedCommands[name] = args;
12
32
  }
13
33
  changeCulture(locale) {
14
- this.callHandler('changeCulture', locale);
34
+ if (this.host.callHandler)
35
+ this.host.callHandler('changeCulture', locale);
36
+ else
37
+ this.cacheCommand('changeCulture', locale);
15
38
  }
16
39
  exit() {
17
- this.callHandler('exit');
40
+ if (this.host.callHandler)
41
+ this.host.callHandler('exit');
42
+ else
43
+ this.cacheCommand('exit');
18
44
  }
19
45
  async getLabels(...keys) {
20
46
  var _a;
47
+ // Try 500 miliseconds
48
+ let count = 5;
49
+ while (this.host.callHandler == null) {
50
+ count--;
51
+ await ExtendUtils.sleep(100);
52
+ if (count === 0)
53
+ break;
54
+ }
21
55
  const init = {};
22
- const result = (_a = (await this.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
56
+ if (this.host.callHandler == null)
57
+ return init;
58
+ const result = (_a = (await this.host.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
23
59
  return keys.reduce((a, v) => {
24
60
  var _a;
25
61
  return ({
@@ -33,6 +69,9 @@ export class FlutterHost {
33
69
  }
34
70
  loadApp(name, startUrl) {
35
71
  this.startUrl = startUrl;
36
- this.callHandler('loadApp', name, startUrl);
72
+ if (this.host.callHandler)
73
+ this.host.callHandler('loadApp', name, startUrl);
74
+ else
75
+ this.cacheCommand('loadApp', name, startUrl);
37
76
  }
38
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.52",
3
+ "version": "1.2.53",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -11,8 +11,8 @@ export namespace BridgeUtils {
11
11
  * Bridge host
12
12
  */
13
13
  export const host =
14
- typeof g.flutter_inappwebview?.callHandler === 'function'
15
- ? new FlutterHost(g.flutter_inappwebview.callHandler)
14
+ typeof g.flutter_inappwebview === 'object'
15
+ ? new FlutterHost(g.flutter_inappwebview)
16
16
  : typeof g.electron === 'object'
17
17
  ? (g.electron as IBridgeHost)
18
18
  : undefined;
@@ -1,5 +1,12 @@
1
+ import { ExtendUtils } from '@etsoo/shared';
1
2
  import { IBridgeHost } from './IBridgeHost';
2
3
 
4
+ // Call handler type
5
+ type CallHandlerType = (
6
+ name: string,
7
+ ...args: unknown[]
8
+ ) => PromiseLike<Record<string, unknown> | void>;
9
+
3
10
  /**
4
11
  * Flutter JavaScript Host
5
12
  * https://inappwebview.dev/docs/javascript/communication/
@@ -10,28 +17,66 @@ export class FlutterHost implements IBridgeHost {
10
17
  */
11
18
  private startUrl: string | null | undefined;
12
19
 
20
+ /**
21
+ * Cached commands
22
+ */
23
+ private cachedCommands: Record<string, unknown[]> = {};
24
+
13
25
  /**
14
26
  * Constructor
15
27
  * @param callHandler Call handler
16
28
  */
17
- constructor(
18
- public callHandler: (
19
- name: string,
20
- ...args: unknown[]
21
- ) => PromiseLike<Record<string, unknown> | void>
22
- ) {}
29
+ constructor(private host: { callHandler?: CallHandlerType }) {
30
+ window.addEventListener(
31
+ 'flutterInAppWebViewPlatformReady',
32
+ (_event) => {
33
+ if (this.host.callHandler == null) return;
34
+
35
+ for (const key in this.cachedCommands) {
36
+ // Args
37
+ const args = this.cachedCommands[key];
38
+
39
+ // Execute
40
+ this.host.callHandler(key, ...args);
41
+
42
+ // Remove the key
43
+ delete this.cachedCommands[key];
44
+ }
45
+ }
46
+ );
47
+ }
48
+
49
+ cacheCommand(name: string, ...args: unknown[]): void {
50
+ this.cachedCommands[name] = args;
51
+ }
23
52
 
24
53
  changeCulture(locale: string): void {
25
- this.callHandler('changeCulture', locale);
54
+ if (this.host.callHandler)
55
+ this.host.callHandler('changeCulture', locale);
56
+ else this.cacheCommand('changeCulture', locale);
26
57
  }
27
58
 
28
59
  exit(): void {
29
- this.callHandler('exit');
60
+ if (this.host.callHandler) this.host.callHandler('exit');
61
+ else this.cacheCommand('exit');
30
62
  }
31
63
 
32
64
  async getLabels<T extends string>(...keys: T[]) {
65
+ // Try 500 miliseconds
66
+ let count = 5;
67
+ while (this.host.callHandler == null) {
68
+ count--;
69
+ await ExtendUtils.sleep(100);
70
+
71
+ if (count === 0) break;
72
+ }
73
+
33
74
  const init: any = {};
34
- const result = (await this.callHandler('getLabels')) ?? {};
75
+
76
+ if (this.host.callHandler == null) return init;
77
+
78
+ const result = (await this.host.callHandler('getLabels')) ?? {};
79
+
35
80
  return keys.reduce(
36
81
  (a, v) => ({
37
82
  ...a,
@@ -47,6 +92,8 @@ export class FlutterHost implements IBridgeHost {
47
92
 
48
93
  loadApp(name: string, startUrl?: string): void {
49
94
  this.startUrl = startUrl;
50
- this.callHandler('loadApp', name, startUrl);
95
+ if (this.host.callHandler)
96
+ this.host.callHandler('loadApp', name, startUrl);
97
+ else this.cacheCommand('loadApp', name, startUrl);
51
98
  }
52
99
  }