@ionic/portals-react-native 0.0.1 → 0.0.4

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,6 +1,12 @@
1
- import { NativeEventEmitter, NativeModules, ViewProps } from 'react-native';
1
+ import {
2
+ EmitterSubscription,
3
+ NativeEventEmitter,
4
+ NativeModules,
5
+ ViewProps,
6
+ } from 'react-native';
2
7
 
3
- const { IONPortalsPubSub, IONPortalManager } = NativeModules;
8
+ const { IONPortalPubSub, IONPortalManager, IONLiveUpdatesManager } =
9
+ NativeModules;
4
10
 
5
11
  export { default as PortalView } from './PortalView';
6
12
 
@@ -10,30 +16,43 @@ export interface Message {
10
16
  topic: string;
11
17
  }
12
18
 
13
- const PortalsPubSub = new NativeEventEmitter(IONPortalsPubSub);
19
+ const PortalsPubSub = new NativeEventEmitter(IONPortalPubSub);
14
20
 
15
- let subscriptionRefDict: any = {};
21
+ const subscriptionMap = new Map<number, EmitterSubscription>();
16
22
 
17
23
  export const subscribe = async (
18
24
  topic: string,
19
25
  onMessageReceived: (message: Message) => void
20
26
  ): Promise<number> => {
21
- const subscriptionRef = await IONPortalsPubSub.subscribe(topic);
22
- subscriptionRefDict[subscriptionRef] = PortalsPubSub.addListener(
27
+ const subscriptionRef = await IONPortalPubSub.subscribe(topic);
28
+
29
+ const subscriber = PortalsPubSub.addListener(
23
30
  'PortalsSubscription',
24
- onMessageReceived
31
+ (message: Message) => {
32
+ if (message.subscriptionRef === subscriptionRef) {
33
+ onMessageReceived(message);
34
+ }
35
+ }
25
36
  );
37
+
38
+ subscriptionMap.set(subscriptionRef, subscriber);
39
+
26
40
  return subscriptionRef;
27
41
  };
28
42
 
29
43
  export const unsubscribe = (topic: string, subRef: number) => {
30
- IONPortalsPubSub.unsubscribe(topic, subRef);
31
- subscriptionRefDict[subRef] = null;
44
+ IONPortalPubSub.unsubscribe(topic, subRef);
45
+
46
+ const subscription = subscriptionMap.get(subRef);
47
+ if (subscription !== undefined) {
48
+ subscription.remove();
49
+ subscriptionMap.delete(subRef);
50
+ }
32
51
  };
33
52
 
34
53
  export const publish = (topic: string, data: any) => {
35
54
  const msg = { message: data };
36
- IONPortalsPubSub.publish(topic, msg);
55
+ IONPortalPubSub.publish(topic, msg);
37
56
  };
38
57
 
39
58
  export const register = (key: string) => {
@@ -42,8 +61,12 @@ export const register = (key: string) => {
42
61
 
43
62
  export interface Portal {
44
63
  name: string;
64
+ androidPlugins?: string[];
45
65
  startDir?: string;
46
- initialContext?: any;
66
+ initialContext?: {
67
+ [key: string]: any;
68
+ };
69
+ liveUpdate?: LiveUpdateConfig;
47
70
  }
48
71
 
49
72
  export type PortalProps = Pick<Portal, 'name' | 'initialContext'> & ViewProps;
@@ -51,3 +74,33 @@ export type PortalProps = Pick<Portal, 'name' | 'initialContext'> & ViewProps;
51
74
  export const addPortal = (portal: Portal) => {
52
75
  IONPortalManager.addPortal(portal);
53
76
  };
77
+
78
+ export interface LiveUpdate {
79
+ appId: string;
80
+ channel: string;
81
+ }
82
+
83
+ export type LiveUpdateConfig = LiveUpdate & { syncOnAdd: boolean };
84
+
85
+ export interface LiveUpdateError {
86
+ appId: string;
87
+ failStep: string;
88
+ message: string;
89
+ }
90
+
91
+ export interface SyncResults {
92
+ liveUpdates: LiveUpdate[];
93
+ errors: LiveUpdateError[];
94
+ }
95
+
96
+ export const syncOne = (appId: string): Promise<LiveUpdate> => {
97
+ return IONLiveUpdatesManager.syncOne(appId);
98
+ };
99
+
100
+ export const syncSome = (appIds: string[]): Promise<SyncResults> => {
101
+ return IONLiveUpdatesManager.syncSome(appIds);
102
+ };
103
+
104
+ export const syncAll = (): Promise<SyncResults> => {
105
+ return IONLiveUpdatesManager.syncAll();
106
+ };