@applicaster/quick-brick-core 15.1.5-alpha.6034932020 → 15.1.5-alpha.6655775719

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.
@@ -20,10 +20,6 @@ const warnUndefinedContext = (context) =>
20
20
  data: { context },
21
21
  });
22
22
 
23
- type Subscription = {
24
- [x: string]: () => any;
25
- };
26
-
27
23
  const getModuleContext = R.path(["module", "context"]);
28
24
 
29
25
  const getContexts = R.compose(
@@ -63,24 +59,40 @@ const useSubscribeToActionProviders = () => {
63
59
  export const useRiverInitialState = () => {
64
60
  const key = "getRiverInitialState";
65
61
  const actionProvidersSubscriptions = useSubscribeToActionProviders();
62
+ // Capture river-init promises once. Re-calling getRiverInitialState() on every
63
+ // context re-render causes thrash and can drop already-completed observables.
64
+ // Plugins should return Promise.resolve() when already initialized (not undefined).
65
+ const capturedPromisesRef = React.useRef<Promise<unknown>[] | null>(null);
66
+
67
+ if (
68
+ capturedPromisesRef.current === null &&
69
+ actionProvidersSubscriptions.length > 0
70
+ ) {
71
+ const providersWithMethod = actionProvidersSubscriptions.filter(
72
+ (item) => item?.[key]
73
+ );
74
+
75
+ const promises = providersWithMethod
76
+ .map((item) => item[key]())
77
+ .filter(Boolean) as Promise<unknown>[];
78
+
79
+ // Lock when every river-init provider returned a promise, or none expose it.
80
+ // Retry while a provider still returns undefined (getInitialState not started).
81
+ if (
82
+ providersWithMethod.length === 0 ||
83
+ promises.length === providersWithMethod.length
84
+ ) {
85
+ capturedPromisesRef.current = promises;
86
+ }
87
+ }
66
88
 
67
- const subscriptions = React.useMemo(() => {
68
- if (!actionProvidersSubscriptions.length) return undefined;
69
-
70
- return actionProvidersSubscriptions.reduce((acc, item) => {
71
- const riverInitialStatePromise = item[key]?.();
72
-
73
- if (riverInitialStatePromise) {
74
- acc.push(riverInitialStatePromise);
75
- }
76
-
77
- return acc;
78
- }, [] as Subscription[]);
79
- }, [actionProvidersSubscriptions]);
89
+ if (!actionProvidersSubscriptions.length) {
90
+ return undefined;
91
+ }
80
92
 
81
- if (!actionProvidersSubscriptions.length) return undefined;
82
-
83
- return subscriptions;
93
+ return capturedPromisesRef.current === null
94
+ ? undefined
95
+ : capturedPromisesRef.current;
84
96
  };
85
97
 
86
98
  const getActionInitialStateSetters = (key) => {
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Boot-time dedupe for remote JSON GETs.
3
+ * Shared across packages via globalThis so layout-manager + core
4
+ * don't double-fetch the same URL during startup.
5
+ */
6
+ declare global {
7
+ // eslint-disable-next-line no-var
8
+ var __QB_REMOTE_JSON_INFLIGHT__: Map<string, Promise<unknown>> | undefined;
9
+ }
10
+
11
+ export function cachedRemoteJson<T>(
12
+ url: string,
13
+ fetcher: () => Promise<T>
14
+ ): Promise<T> {
15
+ if (!url) {
16
+ return fetcher();
17
+ }
18
+
19
+ if (!globalThis.__QB_REMOTE_JSON_INFLIGHT__) {
20
+ globalThis.__QB_REMOTE_JSON_INFLIGHT__ = new Map();
21
+ }
22
+
23
+ const cache = globalThis.__QB_REMOTE_JSON_INFLIGHT__;
24
+
25
+ if (!cache.has(url)) {
26
+ cache.set(
27
+ url,
28
+ fetcher().finally(() => {
29
+ cache.delete(url);
30
+ })
31
+ );
32
+ }
33
+
34
+ return cache.get(url) as Promise<T>;
35
+ }
@@ -1,15 +1,14 @@
1
1
  import * as R from "ramda";
2
2
  import { loadAppContextData } from "@applicaster/zapp-react-native-redux";
3
3
 
4
- import { URL_KEYS } from "../consts";
5
4
  import {
6
5
  fetchPluginConfiguration,
7
- getPromiseForKey,
8
- prepareRuntimeConfigurationUrls,
6
+ fetchAllRemoteConfigurations,
9
7
  } from "../helpers";
10
8
  import { isTablet } from "@applicaster/zapp-react-native-utils/reactHooks";
11
9
  import { Dimensions } from "react-native";
12
10
  import { cacheAssets } from "../../AssetCache";
11
+ import { logger } from "@applicaster/zapp-react-native-utils/logger";
13
12
 
14
13
  const buildContextData = (remoteData) => {
15
14
  const { width, height } = Dimensions.get("window");
@@ -33,6 +32,18 @@ export async function getLegacyRemoteContextData(
33
32
  runtimeConfigurationUrls,
34
33
  plugins
35
34
  ) {
35
+ const t0 = performance.now();
36
+
37
+ const mark = (label: string) => {
38
+ logger.log({
39
+ message: `[PERF][remote] ${label} +${Math.round(
40
+ performance.now() - t0
41
+ )}ms`,
42
+ });
43
+ };
44
+
45
+ mark("start");
46
+
36
47
  const layoutManagerPlugin = R.find(
37
48
  R.pathSatisfies(R.is(Function), ["module", "selectLayout"])
38
49
  )(plugins);
@@ -50,25 +61,16 @@ export async function getLegacyRemoteContextData(
50
61
  isTablet: isTabletDevice,
51
62
  });
52
63
 
53
- const { preparedRuntimeConfigurationUrls, layoutData = [] } =
54
- await prepareRuntimeConfigurationUrls(
55
- configurationUrls || runtimeConfigurationUrls,
56
- isTabletDevice
57
- );
64
+ mark("selectLayout done");
58
65
 
59
- const promises = R.compose(
60
- R.map(getPromiseForKey(preparedRuntimeConfigurationUrls)),
61
- R.reject(R.isNil),
62
- R.keys,
63
- R.pick(URL_KEYS)
64
- )(preparedRuntimeConfigurationUrls);
66
+ const remoteDataRaw = await fetchAllRemoteConfigurations(
67
+ configurationUrls || runtimeConfigurationUrls,
68
+ isTabletDevice
69
+ );
65
70
 
66
- const resolvedPromises = await Promise.all(promises);
71
+ mark("fetchAllRemoteConfigurations done");
67
72
 
68
- const remoteData = R.compose(
69
- buildContextData,
70
- R.mergeAll
71
- )([...layoutData, ...resolvedPromises]);
73
+ const remoteData = buildContextData(remoteDataRaw);
72
74
 
73
75
  const useTabletLayout =
74
76
  isTabletDevice &&
@@ -83,6 +85,8 @@ export async function getLegacyRemoteContextData(
83
85
  : remoteData.cellStyles,
84
86
  });
85
87
 
88
+ mark("cacheAssets done");
89
+
86
90
  const { pluginConfigurations } = cachedRemoteData;
87
91
 
88
92
  const updatedPlugins = R.map(
@@ -105,4 +109,6 @@ export async function getLegacyRemoteContextData(
105
109
  ...appProperties,
106
110
  plugins: updatedPlugins,
107
111
  });
112
+
113
+ mark("loadAppContextData done");
108
114
  }
@@ -20,6 +20,18 @@ export async function getNativeRemoteContextData(
20
20
  runtimeConfigurationUrls,
21
21
  plugins
22
22
  ) {
23
+ const t0 = performance.now();
24
+
25
+ const mark = (label: string) => {
26
+ logger.log({
27
+ message: `[PERF][remote-native] ${label} +${Math.round(
28
+ performance.now() - t0
29
+ )}ms`,
30
+ });
31
+ };
32
+
33
+ mark("start");
34
+
23
35
  const { width, height } = Dimensions.get("window");
24
36
 
25
37
  const isTabletDevice = isTablet(
@@ -33,6 +45,8 @@ export async function getNativeRemoteContextData(
33
45
  "pipesEndpoints",
34
46
  ]);
35
47
 
48
+ mark("generalConfiguration done");
49
+
36
50
  loadAppContextData(dispatch, R.mergeAll([...generalConfiguration]));
37
51
 
38
52
  try {
@@ -60,7 +74,10 @@ export async function getNativeRemoteContextData(
60
74
  });
61
75
  }
62
76
 
77
+ mark("selectLayout done");
78
+
63
79
  const layout = await getPromiseForType("layout");
80
+ mark("layout done");
64
81
 
65
82
  const layoutLoadingErrorMessage = "Failed to load layout data";
66
83
 
@@ -79,6 +96,7 @@ export async function getNativeRemoteContextData(
79
96
  dispatch(AppData.actions.merge({ layoutVersion: apiVersion || "v1" }));
80
97
 
81
98
  const resolvedPromises = await mapPromises(getPromiseForType, ["cellStyles"]);
99
+ mark("cellStyles done");
82
100
 
83
101
  let optionalResolvedPromise;
84
102
 
@@ -102,6 +120,8 @@ export async function getNativeRemoteContextData(
102
120
  cellStyles: remoteData.cellStyles,
103
121
  });
104
122
 
123
+ mark("cacheAssets done");
124
+
105
125
  const { pluginConfigurations } = cachedRemoteData;
106
126
 
107
127
  const updatedPlugins = R.map(
@@ -126,4 +146,6 @@ export async function getNativeRemoteContextData(
126
146
  ...appProperties,
127
147
  plugins: updatedPlugins,
128
148
  });
149
+
150
+ mark("loadAppContextData done");
129
151
  }
@@ -2,8 +2,9 @@ import axios from "axios";
2
2
  import camelize from "camelize";
3
3
  import * as R from "ramda";
4
4
  import { NativeModules } from "react-native";
5
- import { URL_KEY_SUFFIX } from "./consts";
5
+ import { URL_KEY_SUFFIX, URL_KEYS } from "./consts";
6
6
  import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
7
+ import { cachedRemoteJson } from "./cachedRemoteJson";
7
8
 
8
9
  const { AppLoaderBridge } = NativeModules;
9
10
 
@@ -27,25 +28,127 @@ export const urlKeyToContext = R.compose(
27
28
  R.replace(URL_KEY_SUFFIX, "")
28
29
  );
29
30
 
30
- export function getPromiseForKey(runtimeConfigurationUrls) {
31
- return async function (urlKey) {
32
- const context = urlKeyToContext(urlKey);
33
- const url = R.prop(urlKey, runtimeConfigurationUrls);
31
+ function stripOppositePlatformUrls(runtimeConfigurationUrls, isTablet) {
32
+ const preparedRuntimeConfigurationUrls = { ...runtimeConfigurationUrls };
33
+
34
+ if (isTablet) {
35
+ if (preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.LAYOUT]) {
36
+ delete preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.MOBILE.LAYOUT];
37
+ }
38
+
39
+ if (
40
+ preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.CELL_STYLES]
41
+ ) {
42
+ delete preparedRuntimeConfigurationUrls[
43
+ REMOTE_CONFIG_KEYS.MOBILE.CELL_STYLES
44
+ ];
45
+ }
46
+ } else {
47
+ delete preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.LAYOUT];
34
48
 
35
- try {
49
+ delete preparedRuntimeConfigurationUrls[
50
+ REMOTE_CONFIG_KEYS.TABLET.CELL_STYLES
51
+ ];
52
+ }
53
+
54
+ return preparedRuntimeConfigurationUrls;
55
+ }
56
+
57
+ async function fetchJsonByUrlKey(runtimeConfigurationUrls, urlKey) {
58
+ const context = urlKeyToContext(urlKey);
59
+ const url = R.prop(urlKey, runtimeConfigurationUrls);
60
+
61
+ try {
62
+ const data = await cachedRemoteJson(url, async () => {
36
63
  const response = await axios.get(url);
37
64
 
38
- const { data } = response;
65
+ return response.data;
66
+ });
39
67
 
40
- return {
41
- [context]: data,
42
- };
43
- } catch (e) {
44
- throw new Error("cannot retrieve configuration data for " + urlKey);
45
- }
68
+ return {
69
+ [context]: data,
70
+ };
71
+ } catch (e) {
72
+ throw new Error("cannot retrieve configuration data for " + urlKey);
73
+ }
74
+ }
75
+
76
+ export function getPromiseForKey(runtimeConfigurationUrls) {
77
+ return async function (urlKey) {
78
+ return fetchJsonByUrlKey(runtimeConfigurationUrls, urlKey);
46
79
  };
47
80
  }
48
81
 
82
+ /**
83
+ * Fetches layout + remaining remote JSON in parallel.
84
+ * If cell_styles URL is out of sync with layout.id, refetches cells only.
85
+ */
86
+ export async function fetchAllRemoteConfigurations(
87
+ runtimeConfigurationUrls,
88
+ isTablet
89
+ ) {
90
+ const layoutUrlKey = isTablet
91
+ ? REMOTE_CONFIG_KEYS.TABLET.LAYOUT
92
+ : REMOTE_CONFIG_KEYS.MOBILE.LAYOUT;
93
+
94
+ const cellStylesUrlKey = isTablet
95
+ ? REMOTE_CONFIG_KEYS.TABLET.CELL_STYLES
96
+ : REMOTE_CONFIG_KEYS.MOBILE.CELL_STYLES;
97
+
98
+ const layoutUrl = R.prop(layoutUrlKey, runtimeConfigurationUrls);
99
+ const cellStylesUrl = R.prop(cellStylesUrlKey, runtimeConfigurationUrls);
100
+ const layoutContext = urlKeyToContext(layoutUrlKey);
101
+ const cellStylesContext = urlKeyToContext(cellStylesUrlKey);
102
+
103
+ const preparedRuntimeConfigurationUrls = stripOppositePlatformUrls(
104
+ runtimeConfigurationUrls,
105
+ isTablet
106
+ );
107
+
108
+ const keys = R.compose(
109
+ R.reject(R.isNil),
110
+ R.keys,
111
+ R.pick(URL_KEYS)
112
+ )(preparedRuntimeConfigurationUrls);
113
+
114
+ if (!layoutUrl) {
115
+ const resolvedPromises = await Promise.all(
116
+ keys.map(getPromiseForKey(preparedRuntimeConfigurationUrls))
117
+ );
118
+
119
+ return R.mergeAll(resolvedPromises);
120
+ }
121
+
122
+ const resolvedPromises = await Promise.all(
123
+ keys.map(getPromiseForKey(preparedRuntimeConfigurationUrls))
124
+ );
125
+
126
+ const remoteData = R.mergeAll(resolvedPromises);
127
+ const layout = remoteData[layoutContext];
128
+ const layoutId = layout?.id;
129
+
130
+ if (cellStylesUrl && layoutId && !cellStylesUrl.includes(layoutId)) {
131
+ const splitedUrl = cellStylesUrl.split("/");
132
+ const oldLayoutId = splitedUrl[splitedUrl.length - 2];
133
+ const correctedUrl = cellStylesUrl.replace(oldLayoutId, layoutId);
134
+
135
+ await sessionStorage.setItem("rivers_configuration_id", layoutId);
136
+
137
+ const cellStyles = await cachedRemoteJson(correctedUrl, async () => {
138
+ const response = await axios.get(correctedUrl);
139
+
140
+ return response.data;
141
+ });
142
+
143
+ return {
144
+ ...remoteData,
145
+ [cellStylesContext]: cellStyles,
146
+ };
147
+ }
148
+
149
+ return remoteData;
150
+ }
151
+
49
152
  export async function prepareRuntimeConfigurationUrls(
50
153
  runtimeConfigurationUrls,
51
154
  isTablet
@@ -64,31 +167,21 @@ export async function prepareRuntimeConfigurationUrls(
64
167
  const context = urlKeyToContext(layoutUrlKey);
65
168
 
66
169
  try {
67
- const response = await axios.get(layoutUrl);
68
- const { data } = response;
69
- const layoutData = [{ [context]: data }];
70
- const { id: layoutId } = data;
170
+ const data = await cachedRemoteJson(layoutUrl, async () => {
171
+ const response = await axios.get(layoutUrl);
71
172
 
72
- const preparedRuntimeConfigurationUrls = { ...runtimeConfigurationUrls };
73
- delete preparedRuntimeConfigurationUrls[layoutUrlKey];
173
+ return response.data;
174
+ });
74
175
 
75
- if (isTablet) {
76
- preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.LAYOUT] &&
77
- delete preparedRuntimeConfigurationUrls[
78
- REMOTE_CONFIG_KEYS.MOBILE.LAYOUT
79
- ];
176
+ const layoutData = [{ [context]: data }];
177
+ const { id: layoutId } = data;
80
178
 
81
- preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.CELL_STYLES] &&
82
- delete preparedRuntimeConfigurationUrls[
83
- REMOTE_CONFIG_KEYS.MOBILE.CELL_STYLES
84
- ];
85
- } else {
86
- delete preparedRuntimeConfigurationUrls[REMOTE_CONFIG_KEYS.TABLET.LAYOUT];
179
+ const preparedRuntimeConfigurationUrls = stripOppositePlatformUrls(
180
+ runtimeConfigurationUrls,
181
+ isTablet
182
+ );
87
183
 
88
- delete preparedRuntimeConfigurationUrls[
89
- REMOTE_CONFIG_KEYS.TABLET.CELL_STYLES
90
- ];
91
- }
184
+ delete preparedRuntimeConfigurationUrls[layoutUrlKey];
92
185
 
93
186
  if (cellStylesUrl && !cellStylesUrl.includes(layoutId)) {
94
187
  const splitedUrl = cellStylesUrl.split("/");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/quick-brick-core",
3
- "version": "15.1.5-alpha.6034932020",
3
+ "version": "15.1.5-alpha.6655775719",
4
4
  "description": "Core package for Applicaster's Quick Brick App",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -28,13 +28,13 @@
28
28
  },
29
29
  "homepage": "https://github.com/applicaster/quickbrick#readme",
30
30
  "dependencies": {
31
- "@applicaster/applicaster-types": "15.1.5-alpha.6034932020",
32
- "@applicaster/quick-brick-core-plugins": "15.1.5-alpha.6034932020",
33
- "@applicaster/zapp-pipes-v2-client": "15.1.5-alpha.6034932020",
34
- "@applicaster/zapp-react-native-bridge": "15.1.5-alpha.6034932020",
35
- "@applicaster/zapp-react-native-redux": "15.1.5-alpha.6034932020",
36
- "@applicaster/zapp-react-native-ui-components": "15.1.5-alpha.6034932020",
37
- "@applicaster/zapp-react-native-utils": "15.1.5-alpha.6034932020",
31
+ "@applicaster/applicaster-types": "15.1.5-alpha.6655775719",
32
+ "@applicaster/quick-brick-core-plugins": "15.1.5-alpha.6655775719",
33
+ "@applicaster/zapp-pipes-v2-client": "15.1.5-alpha.6655775719",
34
+ "@applicaster/zapp-react-native-bridge": "15.1.5-alpha.6655775719",
35
+ "@applicaster/zapp-react-native-redux": "15.1.5-alpha.6655775719",
36
+ "@applicaster/zapp-react-native-ui-components": "15.1.5-alpha.6655775719",
37
+ "@applicaster/zapp-react-native-utils": "15.1.5-alpha.6655775719",
38
38
  "atob": "^2.1.2",
39
39
  "axios": "^0.28.0",
40
40
  "btoa": "^1.2.1",