@etsoo/appscript 1.2.49 → 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.
@@ -1,15 +1,11 @@
1
+ import { FlutterHost } from './FlutterHost';
1
2
  import { IBridgeHost } from './IBridgeHost';
2
3
  /**
3
4
  * Bridge utils
4
5
  */
5
6
  export declare namespace BridgeUtils {
6
- /**
7
- * Is electron client
8
- * @returns Result
9
- */
10
- function isElectronClient(): boolean;
11
7
  /**
12
8
  * Bridge host
13
9
  */
14
- const host: IBridgeHost | undefined;
10
+ const host: IBridgeHost | FlutterHost | undefined;
15
11
  }
@@ -1,41 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BridgeUtils = void 0;
4
+ const FlutterHost_1 = require("./FlutterHost");
4
5
  /**
5
6
  * Bridge utils
6
7
  */
7
8
  var BridgeUtils;
8
9
  (function (BridgeUtils) {
9
- /**
10
- * Is electron client
11
- * @returns Result
12
- */
13
- function isElectronClient() {
14
- // Renderer process
15
- if (typeof window !== 'undefined' &&
16
- typeof window.process === 'object' &&
17
- window.process.type === 'renderer') {
18
- return true;
19
- }
20
- // Main process
21
- if (typeof process !== 'undefined' &&
22
- typeof process.versions === 'object' &&
23
- !!process.versions.electron) {
24
- return true;
25
- }
26
- // Detect the user agent when the `nodeIntegration` option is set to true
27
- if (typeof navigator === 'object' &&
28
- typeof navigator.userAgent === 'string' &&
29
- navigator.userAgent.indexOf('Electron') >= 0) {
30
- return true;
31
- }
32
- return false;
33
- }
34
- BridgeUtils.isElectronClient = isElectronClient;
10
+ const g = globalThis;
35
11
  /**
36
12
  * Bridge host
37
13
  */
38
- BridgeUtils.host = isElectronClient()
39
- ? globalThis.electron
40
- : undefined;
14
+ BridgeUtils.host = typeof g.flutter_inappwebview === 'object'
15
+ ? new FlutterHost_1.FlutterHost(g.flutter_inappwebview)
16
+ : typeof g.electron === 'object'
17
+ ? g.electron
18
+ : undefined;
41
19
  })(BridgeUtils = exports.BridgeUtils || (exports.BridgeUtils = {}));
@@ -0,0 +1,31 @@
1
+ import { IBridgeHost } from './IBridgeHost';
2
+ declare type CallHandlerType = (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
3
+ /**
4
+ * Flutter JavaScript Host
5
+ * https://inappwebview.dev/docs/javascript/communication/
6
+ */
7
+ export declare class FlutterHost implements IBridgeHost {
8
+ private host;
9
+ /**
10
+ * Start Url
11
+ */
12
+ private startUrl;
13
+ /**
14
+ * Cached commands
15
+ */
16
+ private cachedCommands;
17
+ /**
18
+ * Constructor
19
+ * @param callHandler Call handler
20
+ */
21
+ constructor(host: {
22
+ callHandler?: CallHandlerType;
23
+ });
24
+ cacheCommand(name: string, ...args: unknown[]): void;
25
+ changeCulture(locale: string): void;
26
+ exit(): void;
27
+ getLabels<T extends string>(...keys: T[]): Promise<any>;
28
+ getStartUrl(): string | null | undefined;
29
+ loadApp(name: string, startUrl?: string): void;
30
+ }
31
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FlutterHost = void 0;
4
+ const shared_1 = require("@etsoo/shared");
5
+ /**
6
+ * Flutter JavaScript Host
7
+ * https://inappwebview.dev/docs/javascript/communication/
8
+ */
9
+ class FlutterHost {
10
+ /**
11
+ * Constructor
12
+ * @param callHandler Call handler
13
+ */
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;
35
+ }
36
+ changeCulture(locale) {
37
+ if (this.host.callHandler)
38
+ this.host.callHandler('changeCulture', locale);
39
+ else
40
+ this.cacheCommand('changeCulture', locale);
41
+ }
42
+ exit() {
43
+ if (this.host.callHandler)
44
+ this.host.callHandler('exit');
45
+ else
46
+ this.cacheCommand('exit');
47
+ }
48
+ async getLabels(...keys) {
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
+ }
58
+ const init = {};
59
+ if (this.host.callHandler == null)
60
+ return init;
61
+ const result = (_a = (await this.host.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
62
+ return keys.reduce((a, v) => {
63
+ var _a;
64
+ return ({
65
+ ...a,
66
+ [v]: (_a = result[v]) !== null && _a !== void 0 ? _a : ''
67
+ });
68
+ }, init);
69
+ }
70
+ getStartUrl() {
71
+ return this.startUrl;
72
+ }
73
+ loadApp(name, startUrl) {
74
+ this.startUrl = startUrl;
75
+ if (this.host.callHandler)
76
+ this.host.callHandler('loadApp', name, startUrl);
77
+ else
78
+ this.cacheCommand('loadApp', name, startUrl);
79
+ }
80
+ }
81
+ exports.FlutterHost = FlutterHost;
@@ -11,10 +11,21 @@ export interface IBridgeHost {
11
11
  * Exit the application
12
12
  */
13
13
  exit(): void;
14
+ /**
15
+ * Get multiple culture labels
16
+ * @param keys Keys
17
+ */
18
+ getLabels<T extends string>(...keys: T[]): PromiseLike<{
19
+ [K in T]: string;
20
+ }>;
21
+ /**
22
+ * Get app start Url / router Url
23
+ */
24
+ getStartUrl(): string | undefined | null;
14
25
  /**
15
26
  * Load application
16
27
  * @param name App name
17
- * @param startUrl Start Url
28
+ * @param startUrl Start Url / router Url
18
29
  */
19
30
  loadApp(name: string, startUrl?: string): void;
20
31
  }
@@ -1,15 +1,11 @@
1
+ import { FlutterHost } from './FlutterHost';
1
2
  import { IBridgeHost } from './IBridgeHost';
2
3
  /**
3
4
  * Bridge utils
4
5
  */
5
6
  export declare namespace BridgeUtils {
6
- /**
7
- * Is electron client
8
- * @returns Result
9
- */
10
- function isElectronClient(): boolean;
11
7
  /**
12
8
  * Bridge host
13
9
  */
14
- const host: IBridgeHost | undefined;
10
+ const host: IBridgeHost | FlutterHost | undefined;
15
11
  }
@@ -1,38 +1,16 @@
1
+ import { FlutterHost } from './FlutterHost';
1
2
  /**
2
3
  * Bridge utils
3
4
  */
4
5
  export var BridgeUtils;
5
6
  (function (BridgeUtils) {
6
- /**
7
- * Is electron client
8
- * @returns Result
9
- */
10
- function isElectronClient() {
11
- // Renderer process
12
- if (typeof window !== 'undefined' &&
13
- typeof window.process === 'object' &&
14
- window.process.type === 'renderer') {
15
- return true;
16
- }
17
- // Main process
18
- if (typeof process !== 'undefined' &&
19
- typeof process.versions === 'object' &&
20
- !!process.versions.electron) {
21
- return true;
22
- }
23
- // Detect the user agent when the `nodeIntegration` option is set to true
24
- if (typeof navigator === 'object' &&
25
- typeof navigator.userAgent === 'string' &&
26
- navigator.userAgent.indexOf('Electron') >= 0) {
27
- return true;
28
- }
29
- return false;
30
- }
31
- BridgeUtils.isElectronClient = isElectronClient;
7
+ const g = globalThis;
32
8
  /**
33
9
  * Bridge host
34
10
  */
35
- BridgeUtils.host = isElectronClient()
36
- ? globalThis.electron
37
- : undefined;
11
+ BridgeUtils.host = typeof g.flutter_inappwebview === 'object'
12
+ ? new FlutterHost(g.flutter_inappwebview)
13
+ : typeof g.electron === 'object'
14
+ ? g.electron
15
+ : undefined;
38
16
  })(BridgeUtils || (BridgeUtils = {}));
@@ -0,0 +1,31 @@
1
+ import { IBridgeHost } from './IBridgeHost';
2
+ declare type CallHandlerType = (name: string, ...args: unknown[]) => PromiseLike<Record<string, unknown> | void>;
3
+ /**
4
+ * Flutter JavaScript Host
5
+ * https://inappwebview.dev/docs/javascript/communication/
6
+ */
7
+ export declare class FlutterHost implements IBridgeHost {
8
+ private host;
9
+ /**
10
+ * Start Url
11
+ */
12
+ private startUrl;
13
+ /**
14
+ * Cached commands
15
+ */
16
+ private cachedCommands;
17
+ /**
18
+ * Constructor
19
+ * @param callHandler Call handler
20
+ */
21
+ constructor(host: {
22
+ callHandler?: CallHandlerType;
23
+ });
24
+ cacheCommand(name: string, ...args: unknown[]): void;
25
+ changeCulture(locale: string): void;
26
+ exit(): void;
27
+ getLabels<T extends string>(...keys: T[]): Promise<any>;
28
+ getStartUrl(): string | null | undefined;
29
+ loadApp(name: string, startUrl?: string): void;
30
+ }
31
+ export {};
@@ -0,0 +1,77 @@
1
+ import { ExtendUtils } from '@etsoo/shared';
2
+ /**
3
+ * Flutter JavaScript Host
4
+ * https://inappwebview.dev/docs/javascript/communication/
5
+ */
6
+ export class FlutterHost {
7
+ /**
8
+ * Constructor
9
+ * @param callHandler Call handler
10
+ */
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;
32
+ }
33
+ changeCulture(locale) {
34
+ if (this.host.callHandler)
35
+ this.host.callHandler('changeCulture', locale);
36
+ else
37
+ this.cacheCommand('changeCulture', locale);
38
+ }
39
+ exit() {
40
+ if (this.host.callHandler)
41
+ this.host.callHandler('exit');
42
+ else
43
+ this.cacheCommand('exit');
44
+ }
45
+ async getLabels(...keys) {
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
+ }
55
+ const init = {};
56
+ if (this.host.callHandler == null)
57
+ return init;
58
+ const result = (_a = (await this.host.callHandler('getLabels'))) !== null && _a !== void 0 ? _a : {};
59
+ return keys.reduce((a, v) => {
60
+ var _a;
61
+ return ({
62
+ ...a,
63
+ [v]: (_a = result[v]) !== null && _a !== void 0 ? _a : ''
64
+ });
65
+ }, init);
66
+ }
67
+ getStartUrl() {
68
+ return this.startUrl;
69
+ }
70
+ loadApp(name, startUrl) {
71
+ this.startUrl = startUrl;
72
+ if (this.host.callHandler)
73
+ this.host.callHandler('loadApp', name, startUrl);
74
+ else
75
+ this.cacheCommand('loadApp', name, startUrl);
76
+ }
77
+ }
@@ -11,10 +11,21 @@ export interface IBridgeHost {
11
11
  * Exit the application
12
12
  */
13
13
  exit(): void;
14
+ /**
15
+ * Get multiple culture labels
16
+ * @param keys Keys
17
+ */
18
+ getLabels<T extends string>(...keys: T[]): PromiseLike<{
19
+ [K in T]: string;
20
+ }>;
21
+ /**
22
+ * Get app start Url / router Url
23
+ */
24
+ getStartUrl(): string | undefined | null;
14
25
  /**
15
26
  * Load application
16
27
  * @param name App name
17
- * @param startUrl Start Url
28
+ * @param startUrl Start Url / router Url
18
29
  */
19
30
  loadApp(name: string, startUrl?: string): void;
20
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.49",
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",
@@ -1,48 +1,19 @@
1
+ import { FlutterHost } from './FlutterHost';
1
2
  import { IBridgeHost } from './IBridgeHost';
2
3
 
3
4
  /**
4
5
  * Bridge utils
5
6
  */
6
7
  export namespace BridgeUtils {
7
- /**
8
- * Is electron client
9
- * @returns Result
10
- */
11
- export function isElectronClient() {
12
- // Renderer process
13
- if (
14
- typeof window !== 'undefined' &&
15
- typeof window.process === 'object' &&
16
- (window.process as any).type === 'renderer'
17
- ) {
18
- return true;
19
- }
20
-
21
- // Main process
22
- if (
23
- typeof process !== 'undefined' &&
24
- typeof process.versions === 'object' &&
25
- !!process.versions.electron
26
- ) {
27
- return true;
28
- }
29
-
30
- // Detect the user agent when the `nodeIntegration` option is set to true
31
- if (
32
- typeof navigator === 'object' &&
33
- typeof navigator.userAgent === 'string' &&
34
- navigator.userAgent.indexOf('Electron') >= 0
35
- ) {
36
- return true;
37
- }
38
-
39
- return false;
40
- }
8
+ const g: any = globalThis;
41
9
 
42
10
  /**
43
11
  * Bridge host
44
12
  */
45
- export const host = isElectronClient()
46
- ? ((globalThis as any).electron as IBridgeHost)
47
- : undefined;
13
+ export const host =
14
+ typeof g.flutter_inappwebview === 'object'
15
+ ? new FlutterHost(g.flutter_inappwebview)
16
+ : typeof g.electron === 'object'
17
+ ? (g.electron as IBridgeHost)
18
+ : undefined;
48
19
  }
@@ -0,0 +1,99 @@
1
+ import { ExtendUtils } from '@etsoo/shared';
2
+ import { IBridgeHost } from './IBridgeHost';
3
+
4
+ // Call handler type
5
+ type CallHandlerType = (
6
+ name: string,
7
+ ...args: unknown[]
8
+ ) => PromiseLike<Record<string, unknown> | void>;
9
+
10
+ /**
11
+ * Flutter JavaScript Host
12
+ * https://inappwebview.dev/docs/javascript/communication/
13
+ */
14
+ export class FlutterHost implements IBridgeHost {
15
+ /**
16
+ * Start Url
17
+ */
18
+ private startUrl: string | null | undefined;
19
+
20
+ /**
21
+ * Cached commands
22
+ */
23
+ private cachedCommands: Record<string, unknown[]> = {};
24
+
25
+ /**
26
+ * Constructor
27
+ * @param callHandler Call handler
28
+ */
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
+ }
52
+
53
+ changeCulture(locale: string): void {
54
+ if (this.host.callHandler)
55
+ this.host.callHandler('changeCulture', locale);
56
+ else this.cacheCommand('changeCulture', locale);
57
+ }
58
+
59
+ exit(): void {
60
+ if (this.host.callHandler) this.host.callHandler('exit');
61
+ else this.cacheCommand('exit');
62
+ }
63
+
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
+
74
+ const init: any = {};
75
+
76
+ if (this.host.callHandler == null) return init;
77
+
78
+ const result = (await this.host.callHandler('getLabels')) ?? {};
79
+
80
+ return keys.reduce(
81
+ (a, v) => ({
82
+ ...a,
83
+ [v]: result[v] ?? ''
84
+ }),
85
+ init
86
+ );
87
+ }
88
+
89
+ getStartUrl(): string | null | undefined {
90
+ return this.startUrl;
91
+ }
92
+
93
+ loadApp(name: string, startUrl?: string): void {
94
+ this.startUrl = startUrl;
95
+ if (this.host.callHandler)
96
+ this.host.callHandler('loadApp', name, startUrl);
97
+ else this.cacheCommand('loadApp', name, startUrl);
98
+ }
99
+ }
@@ -13,10 +13,23 @@ export interface IBridgeHost {
13
13
  */
14
14
  exit(): void;
15
15
 
16
+ /**
17
+ * Get multiple culture labels
18
+ * @param keys Keys
19
+ */
20
+ getLabels<T extends string>(
21
+ ...keys: T[]
22
+ ): PromiseLike<{ [K in T]: string }>;
23
+
24
+ /**
25
+ * Get app start Url / router Url
26
+ */
27
+ getStartUrl(): string | undefined | null;
28
+
16
29
  /**
17
30
  * Load application
18
31
  * @param name App name
19
- * @param startUrl Start Url
32
+ * @param startUrl Start Url / router Url
20
33
  */
21
34
  loadApp(name: string, startUrl?: string): void;
22
35
  }