@btsd/aitu-bridge 0.2.19 → 0.3.0

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/src/index.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import { LIB_VERSION } from './version';
2
2
 
3
- import promisifyInvoke from './promisifyInvoke';
4
- import promisifyStorage from './promisifyStorage';
5
- import promisifyMethod from './promisifyMethod';
3
+ import {
4
+ promisifyMethod,
5
+ promisifyStorage,
6
+ promisifyInvoke,
7
+ } from './utils'
8
+
9
+ import WebBridge from './webBridge';
6
10
 
7
11
  enum EInvokeRequest {
8
12
  getMe = 'GetMe',
@@ -104,7 +108,7 @@ interface BridgeStorage {
104
108
  clear: ClearType
105
109
  }
106
110
 
107
- interface AituBridge {
111
+ export interface AituBridge {
108
112
  version: string;
109
113
  invoke: BridgeInvoke<EInvokeRequest, ResponseObject>;
110
114
  storage: BridgeStorage;
@@ -168,29 +172,7 @@ const setCustomBackArrowOnClickHandlerMethod = 'setCustomBackArrowOnClickHandler
168
172
 
169
173
  const android = typeof window !== 'undefined' && (window as any).AndroidBridge;
170
174
  const ios = typeof window !== 'undefined' && (window as any).webkit && (window as any).webkit.messageHandlers;
171
- const web = typeof window !== 'undefined' && (window.top !== window) && ((window as any).WebBridge = (window as any).WebBridge || {});
172
-
173
- if (web) {
174
- const aituOrigin = (window as any).AITU_ORIGIN || 'https://aitu.io';
175
-
176
- [invokeMethod, storageMethod].forEach((method) => {
177
- if (!web[method]) {
178
- web[method] = (...args) => window.top.postMessage(JSON.stringify({
179
- method,
180
- payload: args,
181
- }), aituOrigin);
182
- }
183
- });
184
-
185
- window.addEventListener('message', (event) => {
186
- if (event.origin === aituOrigin && event.data) {
187
- try {
188
- const detail = JSON.parse(event.data);
189
- window.dispatchEvent(new CustomEvent('aituEvents', { detail }));
190
- } catch (e) { }
191
- }
192
- });
193
- }
175
+ const web = typeof window !== 'undefined' && (window.top !== window) && WebBridge;
194
176
 
195
177
  const buildBridge = (): AituBridge => {
196
178
  const subs = [];
@@ -199,19 +181,30 @@ const buildBridge = (): AituBridge => {
199
181
  window.addEventListener('aituEvents', (e: any) => {
200
182
  [...subs].map((fn) => fn.call(null, e));
201
183
  })
184
+
185
+ window.addEventListener('message', (e)=>{
186
+ const message = JSON.parse(e.data)
187
+
188
+ if(message && message['method']){
189
+ if(message.method === 'setCustomBackArrowOnClickHandler'){
190
+ (window as any).onAituBridgeBackArrowClick()
191
+ }else if(message.method === 'setHeaderMenuItemClickHandler'){
192
+ (window as any).onAituBridgeHeaderMenuItemClick(message.param)
193
+ }
194
+ }
195
+ })
202
196
  }
203
197
 
204
198
  const invoke = (reqId, method, data = {}) => {
205
199
  const isAndroid = android && android[invokeMethod];
206
200
  const isIos = ios && ios[invokeMethod];
207
- const isWeb = web && web[invokeMethod];
208
201
 
209
202
  if (isAndroid) {
210
203
  android[invokeMethod](reqId, method, JSON.stringify(data));
211
204
  } else if (isIos) {
212
205
  ios[invokeMethod].postMessage({ reqId, method, data });
213
- } else if (isWeb) {
214
- web[invokeMethod](reqId, method, data);
206
+ } else if (web) {
207
+ web.execute(invokeMethod, reqId, method, data)
215
208
  } else if (typeof window !== 'undefined') {
216
209
  console.log('--invoke-isUnknown');
217
210
  }
@@ -220,14 +213,13 @@ const buildBridge = (): AituBridge => {
220
213
  const storage = (reqId, method, data = {}) => {
221
214
  const isAndroid = android && android[storageMethod];
222
215
  const isIos = ios && ios[storageMethod];
223
- const isWeb = web && web[storageMethod];
224
216
 
225
217
  if (isAndroid) {
226
218
  android[storageMethod](reqId, method, JSON.stringify(data));
227
219
  } else if (isIos) {
228
220
  ios[storageMethod].postMessage({ reqId, method, data });
229
- } else if (isWeb) {
230
- web[storageMethod](reqId, method, data);
221
+ } else if (web) {
222
+ web.execute(storageMethod, reqId, method, data);
231
223
  } else if (typeof window !== 'undefined') {
232
224
  console.log('--storage-isUnknown');
233
225
  }
@@ -241,8 +233,10 @@ const buildBridge = (): AituBridge => {
241
233
  android[getGeoMethod](reqId);
242
234
  } else if (isIos) {
243
235
  ios[getGeoMethod].postMessage({ reqId });
236
+ } else if (web) {
237
+ web.execute(getGeoMethod, reqId);
244
238
  } else if (typeof window !== 'undefined') {
245
- console.log('--getGeo-isWeb');
239
+ console.log('--getGeo-isUnknown');
246
240
  }
247
241
  }
248
242
 
@@ -254,8 +248,10 @@ const buildBridge = (): AituBridge => {
254
248
  android[getQrMethod](reqId);
255
249
  } else if (isIos) {
256
250
  ios[getQrMethod].postMessage({ reqId });
251
+ } else if (web) {
252
+ web.execute(getQrMethod, reqId);
257
253
  } else if (typeof window !== 'undefined') {
258
- console.log('--getQr-isWeb');
254
+ console.log('--getQr-isUnknown');
259
255
  }
260
256
  }
261
257
 
@@ -267,8 +263,10 @@ const buildBridge = (): AituBridge => {
267
263
  android[getSMSCodeMethod](reqId);
268
264
  } else if (isIos) {
269
265
  ios[getSMSCodeMethod].postMessage({ reqId });
266
+ } else if (web) {
267
+ web.execute(getSMSCodeMethod, reqId);
270
268
  } else if (typeof window !== 'undefined') {
271
- console.log('--getSMSCode-isWeb');
269
+ console.log('--getSMSCode-isUnknown');
272
270
  }
273
271
  }
274
272
 
@@ -280,8 +278,10 @@ const buildBridge = (): AituBridge => {
280
278
  android[selectContactMethod](reqId);
281
279
  } else if (isIos) {
282
280
  ios[selectContactMethod].postMessage({ reqId });
281
+ } else if (web) {
282
+ web.execute(selectContactMethod, reqId);
283
283
  } else if (typeof window !== 'undefined') {
284
- console.log('--selectContact-isWeb');
284
+ console.log('--selectContact-isUnknown');
285
285
  }
286
286
  }
287
287
 
@@ -293,8 +293,10 @@ const buildBridge = (): AituBridge => {
293
293
  android[openSettingsMethod](reqId);
294
294
  } else if (isIos) {
295
295
  ios[openSettingsMethod].postMessage({ reqId });
296
+ } else if (web) {
297
+ web.execute(openSettingsMethod, reqId);
296
298
  } else if (typeof window !== 'undefined') {
297
- console.log('--openSettings-isWeb');
299
+ console.log('--openSettings-isUnknown');
298
300
  }
299
301
  }
300
302
 
@@ -306,8 +308,10 @@ const buildBridge = (): AituBridge => {
306
308
  android[closeApplicationMethod](reqId);
307
309
  } else if (isIos) {
308
310
  ios[closeApplicationMethod].postMessage({ reqId });
311
+ } else if (web) {
312
+ web.execute(closeApplicationMethod, reqId);
309
313
  } else if (typeof window !== 'undefined') {
310
- console.log('--closeApplication-isWeb');
314
+ console.log('--closeApplication-isUnknown');
311
315
  }
312
316
  }
313
317
 
@@ -319,8 +323,10 @@ const buildBridge = (): AituBridge => {
319
323
  android[shareMethod](reqId, text);
320
324
  } else if (isIos) {
321
325
  ios[shareMethod].postMessage({ reqId, text });
326
+ } else if (web) {
327
+ web.execute(shareMethod, reqId, text);
322
328
  } else if (typeof window !== 'undefined') {
323
- console.log('--share-isWeb');
329
+ console.log('--share-isUnknown');
324
330
  }
325
331
  }
326
332
 
@@ -332,8 +338,10 @@ const buildBridge = (): AituBridge => {
332
338
  android[setTitleMethod](reqId, text);
333
339
  } else if (isIos) {
334
340
  ios[setTitleMethod].postMessage({ reqId, text });
341
+ } else if (web) {
342
+ web.execute(setTitleMethod, reqId, text);
335
343
  } else if (typeof window !== 'undefined') {
336
- console.log('--setTitle-isWeb');
344
+ console.log('--setTitle-isUnknown');
337
345
  }
338
346
  }
339
347
 
@@ -345,8 +353,10 @@ const buildBridge = (): AituBridge => {
345
353
  android[copyToClipboardMethod](reqId, text);
346
354
  } else if (isIos) {
347
355
  ios[copyToClipboardMethod].postMessage({ reqId, text });
356
+ } else if (web) {
357
+ web.execute(copyToClipboardMethod, reqId, text);
348
358
  } else if (typeof window !== 'undefined') {
349
- console.log('--copyToClipboard-isWeb');
359
+ console.log('--copyToClipboard-isUnknown');
350
360
  }
351
361
  }
352
362
 
@@ -358,8 +368,10 @@ const buildBridge = (): AituBridge => {
358
368
  android[enableScreenCaptureMethod](reqId);
359
369
  } else if (isIos) {
360
370
  ios[enableScreenCaptureMethod].postMessage({ reqId });
371
+ } else if (web) {
372
+ web.execute(enableScreenCaptureMethod, reqId);
361
373
  } else if (typeof window !== 'undefined') {
362
- console.log('--enableScreenCapture-isWeb');
374
+ console.log('--enableScreenCapture-isUnknown');
363
375
  }
364
376
  }
365
377
 
@@ -371,8 +383,10 @@ const buildBridge = (): AituBridge => {
371
383
  android[disableScreenCaptureMethod](reqId);
372
384
  } else if (isIos) {
373
385
  ios[disableScreenCaptureMethod].postMessage({ reqId });
386
+ } else if (web) {
387
+ web.execute(disableScreenCaptureMethod, reqId);
374
388
  } else if (typeof window !== 'undefined') {
375
- console.log('--disableScreenCapture-isWeb');
389
+ console.log('--disableScreenCapture-isUnknown');
376
390
  }
377
391
  }
378
392
 
@@ -406,8 +420,10 @@ const buildBridge = (): AituBridge => {
406
420
  android[shareFileMethod](reqId, text, filename, base64Data);
407
421
  } else if (isIos) {
408
422
  ios[shareFileMethod].postMessage({ reqId, text, filename, base64Data });
423
+ } else if (web) {
424
+ web.execute(shareFileMethod, reqId, { text, filename, base64Data });
409
425
  } else if (typeof window !== 'undefined') {
410
- console.log('--shareFile-isWeb');
426
+ console.log('--shareFile-isUnknown');
411
427
  }
412
428
  }
413
429
 
@@ -419,8 +435,10 @@ const buildBridge = (): AituBridge => {
419
435
  android[shareFileMethod](reqId, text, filename, base64Data);
420
436
  } else if (isIos) {
421
437
  ios[shareFileMethod].postMessage({ reqId, text, filename, base64Data });
438
+ } else if (web) {
439
+ web.execute(shareFileMethod, reqId, text, filename, base64Data);
422
440
  } else if (typeof window !== 'undefined') {
423
- console.log('--shareFile-isWeb');
441
+ console.log('--shareFile-isUnknown');
424
442
  }
425
443
  }
426
444
 
@@ -432,10 +450,10 @@ const buildBridge = (): AituBridge => {
432
450
  const isAndroid = android && android[setShakeHandlerMethod];
433
451
  const isIos = ios && ios[setShakeHandlerMethod];
434
452
 
435
- if (isAndroid || isIos) {
453
+ if (isAndroid || isIos || web) {
436
454
  (window as any).onAituBridgeShake = handler;
437
455
  } else if (typeof window !== 'undefined') {
438
- console.log('--setShakeHandler-isWeb');
456
+ console.log('--setShakeHandler-isUnknown');
439
457
  }
440
458
  };
441
459
 
@@ -443,10 +461,10 @@ const buildBridge = (): AituBridge => {
443
461
  const isAndroid = android && android[setTabActiveHandlerMethod];
444
462
  const isIos = ios && ios[setTabActiveHandlerMethod];
445
463
 
446
- if (isAndroid || isIos) {
464
+ if (isAndroid || isIos || web) {
447
465
  (window as any).onAituBridgeTabActive = handler;
448
466
  } else if (typeof window !== 'undefined') {
449
- console.log('--setTabActiveHandler-isWeb');
467
+ console.log('--setTabActiveHandler-isUnknown');
450
468
  }
451
469
  };
452
470
 
@@ -467,8 +485,10 @@ const buildBridge = (): AituBridge => {
467
485
  android[vibrateMethod](reqId, JSON.stringify(pattern));
468
486
  } else if (isIos) {
469
487
  ios[vibrateMethod].postMessage({ reqId, pattern });
488
+ } else if (web) {
489
+ web.execute(vibrateMethod, reqId, pattern);
470
490
  } else if (typeof window !== 'undefined') {
471
- console.log('--vibrate-isWeb');
491
+ console.log('--vibrate-isUnknown');
472
492
  }
473
493
  }
474
494
 
@@ -477,6 +497,7 @@ const buildBridge = (): AituBridge => {
477
497
  return Boolean(android || iosSup || web);
478
498
  }
479
499
 
500
+ // TODO: implement web support
480
501
  const supports = (method) =>
481
502
  (android && typeof android[method] === 'function') ||
482
503
  (ios && ios[method] && typeof ios[method].postMessage === 'function') ||
@@ -501,8 +522,10 @@ const buildBridge = (): AituBridge => {
501
522
  android[setHeaderMenuItemsMethod](reqId, itemsJsonArray);
502
523
  } else if (isIos) {
503
524
  ios[setHeaderMenuItemsMethod].postMessage({ reqId, itemsJsonArray });
525
+ } else if (web) {
526
+ web.execute(setHeaderMenuItemsMethod, reqId, itemsJsonArray);
504
527
  } else if (typeof window !== 'undefined') {
505
- console.log('--setHeaderMenuItems-isWeb');
528
+ console.log('--setHeaderMenuItems-isUnknown');
506
529
  }
507
530
  }
508
531
 
@@ -510,10 +533,10 @@ const buildBridge = (): AituBridge => {
510
533
  const isAndroid = android && android[setHeaderMenuItemClickHandlerMethod];
511
534
  const isIos = ios && ios[setHeaderMenuItemClickHandlerMethod];
512
535
 
513
- if (isAndroid || isIos) {
536
+ if (isAndroid || isIos || web) {
514
537
  (window as any).onAituBridgeHeaderMenuItemClick = handler;
515
538
  } else if (typeof window !== 'undefined') {
516
- console.log('--setHeaderMenuItemClickHandler-isWeb');
539
+ console.log('--setHeaderMenuItemClickHandler-isUnknown');
517
540
  }
518
541
  }
519
542
 
@@ -525,8 +548,10 @@ const buildBridge = (): AituBridge => {
525
548
  android[setCustomBackArrowModeMethod](reqId, enabled);
526
549
  } else if (isIos) {
527
550
  ios[setCustomBackArrowModeMethod].postMessage({ reqId, enabled });
551
+ } else if (web) {
552
+ web.execute(setCustomBackArrowModeMethod, reqId, enabled);
528
553
  } else if (typeof window !== 'undefined') {
529
- console.log('--setCustomBackArrowMode-isWeb');
554
+ console.log('--setCustomBackArrowMode-isUnknown');
530
555
  }
531
556
  }
532
557
 
@@ -538,8 +563,10 @@ const buildBridge = (): AituBridge => {
538
563
  android[getCustomBackArrowModeMethod](reqId);
539
564
  } else if (isIos) {
540
565
  ios[getCustomBackArrowModeMethod].postMessage({ reqId });
566
+ } else if (web) {
567
+ web.execute(getCustomBackArrowModeMethod, reqId);
541
568
  } else if (typeof window !== 'undefined') {
542
- console.log('--getCustomBackArrowMode-isWeb');
569
+ console.log('--getCustomBackArrowMode-isUnknown');
543
570
  }
544
571
  }
545
572
 
@@ -551,8 +578,10 @@ const buildBridge = (): AituBridge => {
551
578
  android[setCustomBackArrowVisibleMethod](reqId, visible);
552
579
  } else if (isIos) {
553
580
  ios[setCustomBackArrowVisibleMethod].postMessage({ reqId, visible });
581
+ } else if (web) {
582
+ web.execute(setCustomBackArrowVisibleMethod, reqId, visible);
554
583
  } else if (typeof window !== 'undefined') {
555
- console.log('--setCustomBackArrowVisible-isWeb');
584
+ console.log('--setCustomBackArrowVisible-isUnknown');
556
585
  }
557
586
  }
558
587
 
@@ -560,33 +589,34 @@ const buildBridge = (): AituBridge => {
560
589
  const isAndroid = android && android[setCustomBackArrowOnClickHandlerMethod];
561
590
  const isIos = ios && ios[setCustomBackArrowOnClickHandlerMethod];
562
591
 
563
- if (isAndroid || isIos) {
592
+ if (isAndroid || isIos || web) {
564
593
  (window as any).onAituBridgeBackArrowClick = handler;
565
- } else if (typeof window !== 'undefined') {
566
- console.log('--setCustomBackArrowOnClickHandler-isWeb');
594
+ }else if (typeof window !== 'undefined') {
595
+ console.log('--setCustomBackArrowOnClickHandler-isUnknown');
567
596
  }
568
597
  }
569
598
 
599
+
570
600
  const invokePromise = promisifyInvoke(invoke, sub);
571
601
  const storagePromise = promisifyStorage(storage, sub);
572
- const getGeoPromise = promisifyMethod(getGeo, sub);
573
- const getQrPromise = promisifyMethod(getQr, sub);
574
- const getSMSCodePromise = promisifyMethod(getSMSCode, sub);
575
- const selectContactPromise = promisifyMethod(selectContact, sub);
576
- const openSettingsPromise = promisifyMethod(openSettings, sub);
577
- const closeApplicationPromise = promisifyMethod(closeApplication, sub);
578
- const sharePromise = promisifyMethod(share, sub);
579
- const setTitlePromise = promisifyMethod(setTitle, sub);
580
- const copyToClipboardPromise = promisifyMethod(copyToClipboard, sub);
581
- const shareImagePromise = promisifyMethod(shareImage, sub);
582
- const shareFilePromise = promisifyMethod(shareFile, sub);
583
- const vibratePromise = promisifyMethod(vibrate, sub);
584
- const enableScreenCapturePromise = promisifyMethod(enableScreenCapture, sub);
585
- const disableScreenCapturePromise = promisifyMethod(disableScreenCapture, sub);
586
- const setHeaderMenuItemsPromise = promisifyMethod(setHeaderMenuItems, sub);
587
- const setCustomBackArrowModePromise = promisifyMethod(setCustomBackArrowMode, sub);
588
- const getCustomBackArrowModePromise = promisifyMethod(getCustomBackArrowMode, sub);
589
- const setCustomBackArrowVisiblePromise = promisifyMethod(setCustomBackArrowVisible, sub);
602
+ const getGeoPromise = promisifyMethod(getGeo, getGeoMethod, sub);
603
+ const getQrPromise = promisifyMethod(getQr, getQrMethod, sub);
604
+ const getSMSCodePromise = promisifyMethod(getSMSCode, getSMSCodeMethod, sub);
605
+ const selectContactPromise = promisifyMethod(selectContact, selectContactMethod, sub);
606
+ const openSettingsPromise = promisifyMethod(openSettings, openSettingsMethod, sub);
607
+ const closeApplicationPromise = promisifyMethod(closeApplication, closeApplicationMethod, sub);
608
+ const sharePromise = promisifyMethod(share, shareMethod, sub);
609
+ const setTitlePromise = promisifyMethod(setTitle, setTitleMethod, sub);
610
+ const copyToClipboardPromise = promisifyMethod(copyToClipboard, copyToClipboardMethod, sub);
611
+ const shareImagePromise = promisifyMethod(shareImage, shareImageMethod, sub);
612
+ const shareFilePromise = promisifyMethod(shareFile, shareFileMethod, sub);
613
+ const vibratePromise = promisifyMethod(vibrate, vibrateMethod, sub);
614
+ const enableScreenCapturePromise = promisifyMethod(enableScreenCapture, enableScreenCaptureMethod, sub);
615
+ const disableScreenCapturePromise = promisifyMethod(disableScreenCapture, disableScreenCaptureMethod, sub);
616
+ const setHeaderMenuItemsPromise = promisifyMethod(setHeaderMenuItems, setHeaderMenuItemsMethod, sub);
617
+ const setCustomBackArrowModePromise = promisifyMethod(setCustomBackArrowMode, setCustomBackArrowModeMethod, sub);
618
+ const getCustomBackArrowModePromise = promisifyMethod(getCustomBackArrowMode, getCustomBackArrowModeMethod, sub);
619
+ const setCustomBackArrowVisiblePromise = promisifyMethod(setCustomBackArrowVisible,setCustomBackArrowVisibleMethod, sub);
590
620
 
591
621
  return {
592
622
  version: String(LIB_VERSION),
package/src/utils.ts ADDED
@@ -0,0 +1,113 @@
1
+ function createCounter(prefix = 'm:') {
2
+ return {
3
+ current: 0,
4
+ next() {
5
+ return prefix + ++this.current;
6
+ },
7
+ };
8
+ }
9
+
10
+ function createRequestResolver(prefix: string) {
11
+ type PromiseController = {
12
+ resolve: (value: any) => any;
13
+ reject: (reason: any) => any;
14
+ };
15
+
16
+ const counter = createCounter(prefix);
17
+ const promiseControllers: Record<string, PromiseController | null> = {};
18
+
19
+ return {
20
+ add(controller: PromiseController, customId = ''): number | string {
21
+ const id = customId + counter.next()
22
+ promiseControllers[id] = controller;
23
+ return id;
24
+ },
25
+
26
+ resolve<T>(reqId: number | string, data: T, isSuccess: (data: T) => boolean, error: any) {
27
+ const requestPromise = promiseControllers[reqId];
28
+
29
+ if (requestPromise) {
30
+ if (isSuccess(error)) {
31
+ requestPromise.resolve(data);
32
+ } else {
33
+ requestPromise.reject(error);
34
+ }
35
+
36
+ promiseControllers[reqId] = null;
37
+ }
38
+ },
39
+ };
40
+ }
41
+
42
+ function handleSubscribe(subscribe: (handler: (event: any) => void) => void, requestResolver: ReturnType<typeof createRequestResolver>) {
43
+ subscribe(event => {
44
+ if (!event.detail) {
45
+ return;
46
+ }
47
+
48
+ if ('reqId' in event.detail) {
49
+ const { reqId, data, error } = event.detail;
50
+
51
+ if (reqId) {
52
+ requestResolver.resolve(reqId, data, (error) => !(error), error);
53
+ }
54
+ }
55
+ })
56
+ }
57
+
58
+ export function promisifyStorage(storage, subscribe: (fn: any) => void) {
59
+ const requestResolver = createRequestResolver('storage:');
60
+
61
+ handleSubscribe(subscribe, requestResolver)
62
+
63
+ return {
64
+ setItem: (keyName: string, keyValue: string): Promise<void> => {
65
+ return new Promise((resolve, reject) => {
66
+ const reqId = requestResolver.add({ resolve, reject });
67
+ storage(reqId, 'setItem', { keyName, keyValue });
68
+ });
69
+ },
70
+ getItem: (keyName: string): Promise<string | null> => {
71
+ return new Promise((resolve, reject) => {
72
+ const reqId = requestResolver.add({ resolve, reject });
73
+ storage(reqId, 'getItem', { keyName });
74
+ });
75
+ },
76
+ clear: (): Promise<void> => {
77
+ return new Promise((resolve, reject) => {
78
+ const reqId = requestResolver.add({ resolve, reject });
79
+ storage(reqId, 'clear', {});
80
+ });
81
+ },
82
+ }
83
+ }
84
+
85
+ export function promisifyInvoke(invoke, subscribe: (fn: any) => void) {
86
+ const requestResolver = createRequestResolver('invoke:');
87
+
88
+ handleSubscribe(subscribe, requestResolver)
89
+
90
+ return function promisifiedFunc(invokeMethodName: string, props: any = {}): Promise<any | void> {
91
+ return new Promise((resolve, reject) => {
92
+ const reqId = requestResolver.add({ resolve, reject }, invokeMethodName + ':');
93
+
94
+ invoke(reqId, invokeMethodName, props);
95
+ });
96
+ };
97
+ }
98
+
99
+ export function promisifyMethod(method: Function, methodName: string, subscribe: (fn: any) => void) {
100
+ const requestResolver = createRequestResolver(methodName + ':');
101
+
102
+ handleSubscribe(subscribe, requestResolver)
103
+
104
+ return function promisifiedFunc(...args: any[]): Promise<any | void> {
105
+ return new Promise((resolve, reject) => {
106
+ const reqId = requestResolver.add({ resolve, reject });
107
+ method(reqId, ...args);
108
+ });
109
+ };
110
+ }
111
+
112
+
113
+
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const LIB_VERSION = "0.2.19";
1
+ export const LIB_VERSION = "0.3.0";
@@ -0,0 +1,39 @@
1
+ import type { AituBridge } from './index';
2
+
3
+ const AITU_DOMAIN_PARAM = '__aitu-domain'
4
+
5
+ const searchParams = new URLSearchParams(window.location.search)
6
+
7
+ const aituOrigin = searchParams.get(AITU_DOMAIN_PARAM)
8
+
9
+ interface WebBridge {
10
+ execute(method: keyof AituBridge, reqId: string, ...payload: any[] ): void
11
+ origin: string
12
+ }
13
+
14
+ let WebBridge: WebBridge | null = null
15
+
16
+ if (aituOrigin) {
17
+ WebBridge = {
18
+ origin: aituOrigin,
19
+ execute: (method, reqId, ...payload) => {
20
+ window.top.postMessage({
21
+ source: 'aitu-bridge',
22
+ method,
23
+ reqId,
24
+ payload: [...payload],
25
+ },
26
+ WebBridge.origin
27
+ )
28
+ }
29
+
30
+ }
31
+
32
+ window.addEventListener('message', event => {
33
+ if (event.origin === aituOrigin && event.data) {
34
+ window.dispatchEvent(new CustomEvent('aituEvents', { detail: event.data }));
35
+ }
36
+ })
37
+ }
38
+
39
+ export default WebBridge
@@ -1,2 +0,0 @@
1
- declare function promisifyInvoke(invoke: any, subscribe: (fn: any) => void): (method: any, props?: any) => Promise<any | void>;
2
- export default promisifyInvoke;
@@ -1,2 +0,0 @@
1
- declare function promisifyMethod(method: any, subscribe: (fn: any) => void): (...args: any[]) => Promise<any | void>;
2
- export default promisifyMethod;
@@ -1,6 +0,0 @@
1
- declare function promisifyStorage(storage: any, subscribe: (fn: any) => void): {
2
- setItem: (keyName: string, keyValue: string) => Promise<void>;
3
- getItem: (keyName: string) => Promise<string | null>;
4
- clear: () => Promise<void>;
5
- };
6
- export default promisifyStorage;
@@ -1,69 +0,0 @@
1
- function createCounter() {
2
- return {
3
- current: 0,
4
- next() {
5
- return ++this.current;
6
- },
7
- };
8
- }
9
-
10
- function createRequestResolver() {
11
- type PromiseController = {
12
- resolve: (value: any) => any;
13
- reject: (reason: any) => any;
14
- };
15
-
16
- const counter = createCounter();
17
- const promiseControllers: Record<string, PromiseController | null> = {};
18
-
19
- return {
20
- add(controller: PromiseController, customId?: number | string): number | string {
21
- const id = customId != null ? customId : counter.next();
22
- const invokeId = id;
23
- promiseControllers[invokeId] = controller;
24
- return invokeId;
25
- },
26
-
27
- resolve<T>(reqId: number | string, data: T, isSuccess: (data: T) => boolean, error: any) {
28
- const requestPromise = promiseControllers[reqId];
29
-
30
- if (requestPromise) {
31
- if (isSuccess(error)) {
32
- requestPromise.resolve(data);
33
- } else {
34
- requestPromise.reject(error);
35
- }
36
-
37
- promiseControllers[reqId] = null;
38
- }
39
- },
40
- };
41
- }
42
-
43
- function promisifyInvoke(invoke, subscribe: (fn: any) => void) {
44
- const requestResolver = createRequestResolver();
45
-
46
- subscribe((event) => {
47
- if (!event.detail) {
48
- return;
49
- }
50
-
51
- if ('reqId' in event.detail) {
52
- const { reqId, data, error } = event.detail;
53
-
54
- if (reqId) {
55
- requestResolver.resolve(reqId, data, (error) => !(error), error);
56
- }
57
- }
58
- });
59
-
60
- return function promisifiedFunc(method: any, props: any = {}): Promise<any | void> {
61
- return new Promise((resolve, reject) => {
62
- const reqId = requestResolver.add({ resolve, reject }, props.reqId);
63
-
64
- invoke(reqId, method, props);
65
- });
66
- };
67
- }
68
-
69
- export default promisifyInvoke;