UNPKG

2.08 kBJavaScriptView Raw
1import { Platform } from 'react-native';
2// centralized memo object
3let memo = {};
4export function clearMemo() {
5 memo = {};
6}
7/**
8 * function returns the proper getter based current platform X supported platforms
9 * @param supportedPlatforms array of supported platforms (OS)
10 * @param getter desired function used to get info
11 * @param defaultGetter getter that returns a default value if desired getter is not supported by current platform
12 */
13
14function getSupportedFunction(supportedPlatforms, getter, defaultGetter) {
15 let supportedMap = {};
16 supportedPlatforms.filter(key => Platform.OS == key).forEach(key => supportedMap[key] = getter);
17 return Platform.select({ ...supportedMap,
18 default: defaultGetter
19 });
20}
21/**
22 * function used to get desired info synchronously — with optional memoization
23 * @param param0
24 */
25
26
27export function getSupportedPlatformInfoSync({
28 getter,
29 supportedPlatforms,
30 defaultValue,
31 memoKey
32}) {
33 if (memoKey && memo[memoKey] != undefined) {
34 return memo[memoKey];
35 } else {
36 const output = getSupportedFunction(supportedPlatforms, getter, () => defaultValue)();
37
38 if (memoKey) {
39 memo[memoKey] = output;
40 }
41
42 return output;
43 }
44}
45/**
46 * function used to get desired info asynchronously — with optional memoization
47 * @param param0
48 */
49
50export async function getSupportedPlatformInfoAsync({
51 getter,
52 supportedPlatforms,
53 defaultValue,
54 memoKey
55}) {
56 if (memoKey && memo[memoKey] != undefined) {
57 return memo[memoKey];
58 } else {
59 const output = await getSupportedFunction(supportedPlatforms, getter, () => Promise.resolve(defaultValue))();
60
61 if (memoKey) {
62 memo[memoKey] = output;
63 }
64
65 return output;
66 }
67}
68/**
69 * function that returns array of getter functions [async, sync]
70 * @param param0
71 */
72
73export function getSupportedPlatformInfoFunctions({
74 syncGetter,
75 ...asyncParams
76}) {
77 return [() => getSupportedPlatformInfoAsync(asyncParams), () => getSupportedPlatformInfoSync({ ...asyncParams,
78 getter: syncGetter
79 })];
80}
81//# sourceMappingURL=supported-platform-info.js.map
\No newline at end of file