-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ios.js
94 lines (82 loc) · 2.62 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* @flow */
import { NativeEventEmitter, NativeModules } from 'react-native';
const NativeStoreViewManager = NativeModules.RJHStoreViewManager;
const moduleEventEmitter = new NativeEventEmitter(NativeStoreViewManager);
export type LoadProductParameters = {
iTunesItemIdentifier: number,
affiliateToken?: string,
campaignToken?: string,
providerToken?: string,
advertisingPartnerToken?: string
};
export type StoreViewEventName =
'loading'
| 'loaded'
| 'presenting'
| 'presented'
| 'dismissing'
| 'dismissed';
function jsEventToNativeEvent(event: StoreViewEventName) {
if (event.substring(0,19) === 'RJHStoreViewManager') {
return event;
}
if (event.substring(0,2) === 'on') {
const badEvent = event;
event = ((event.substring(2).toLowerCase(): any): StoreViewEventName);
console.warn(`[react-native-store-view] Event names starting with 'on' ('${badEvent}') are deprecated. Listen for '${event}' instead`);
}
return 'RJHStoreViewManager' + event.charAt(0).toUpperCase() + event.substring(1);
}
export function loadProductWithParameters(params: LoadProductParameters): Promise<void> {
return new Promise((resolve, reject) => {
NativeStoreViewManager.loadProductWithParameters(params, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
export function presentViewController(animated: boolean = true): Promise<void> {
return new Promise((resolve, reject) => {
NativeStoreViewManager.presentViewController(animated, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
export function isAvailable(): Promise<boolean> {
return new Promise((resolve, reject) => {
NativeStoreViewManager.isAvailable((err, available) => {
if (err) {
return reject(err);
}
resolve(available);
});
});
}
export function dismiss(animated: boolean = true): Promise<void> {
return new Promise((resolve, reject) => {
NativeStoreViewManager.dismiss(animated, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
export function addListener(event: StoreViewEventName, listener: (payload: any) => any) {
return moduleEventEmitter.addListener(jsEventToNativeEvent(event), listener);
}
export function removeListener(event: StoreViewEventName, listener: (payload: any) => any) {
moduleEventEmitter.removeListener(jsEventToNativeEvent(event), listener);
}
export function once(event: StoreViewEventName, callback: (payload: any) => any) {
const listener = (payload) => {
callback(payload);
addListener(event, listener);
};
removeListener(event, listener);
}