brew-js-react 0.7.1 → 0.7.3
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/dialog.d.ts +12 -2
- package/dialog.js +10 -1
- package/dist/brew-js-react.js +122 -72
- package/dist/brew-js-react.js.map +1 -1
- package/dist/brew-js-react.min.js +2 -2
- package/dist/brew-js-react.min.js.map +1 -1
- package/hooks.d.ts +12 -0
- package/hooks.js +24 -3
- package/package.json +3 -2
- package/view.d.ts +12 -2
- package/view.js +19 -7
package/hooks.d.ts
CHANGED
|
@@ -39,6 +39,18 @@ export function useRouteState<T>(key: string | symbol, initialState: T | (() =>
|
|
|
39
39
|
*/
|
|
40
40
|
export function useRouteState<T = undefined>(key: string | symbol): [T | undefined, React.Dispatch<React.SetStateAction<T | undefined>>];
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Returns a stateful value, and a function to update it.
|
|
44
|
+
* Same as {@link React.useState}, but the state is persisted in `app.sessionStorage`.
|
|
45
|
+
*/
|
|
46
|
+
export function useAppSessionState<T>(key: string | symbol, initialState: T | (() => T)): [T, React.Dispatch<React.SetStateAction<T>>];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns a stateful value, and a function to update it.
|
|
50
|
+
* Same as {@link React.useState}, but the state is persisted in `app.sessionStorage`.
|
|
51
|
+
*/
|
|
52
|
+
export function useAppSessionState<T = undefined>(key: string | symbol): [T | undefined, React.Dispatch<React.SetStateAction<T | undefined>>];
|
|
53
|
+
|
|
42
54
|
/**
|
|
43
55
|
* Returns value of the specified parameter in query string, and a function to update it.
|
|
44
56
|
*
|
package/hooks.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createElement, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
-
import { ViewStateProvider, useEagerState, useMemoizedFunction, useObservableProperty, useValueTrigger } from "zeta-dom-react";
|
|
3
|
-
import { catchAsync, definePrototype, delay, each, equal, extend, freeze, isFunction, isPlainObject, keys, kv, mapObject, throwNotFunction } from "zeta-dom/util";
|
|
2
|
+
import { ViewStateProvider, useEagerReducer, useEagerState, useMemoizedFunction, useObservableProperty, useValueTrigger } from "zeta-dom-react";
|
|
3
|
+
import { catchAsync, definePrototype, delay, each, equal, extend, freeze, isFunction, isPlainObject, keys, kv, mapObject, sameValue, throwNotFunction } from "zeta-dom/util";
|
|
4
4
|
import { ZetaEventContainer } from "zeta-dom/events";
|
|
5
5
|
import { getQueryParam, setQueryParam } from "brew-js/util/common";
|
|
6
6
|
import { parsePath } from "brew-js/util/path";
|
|
@@ -68,6 +68,20 @@ function useViewContextWithEffect(callback, deps) {
|
|
|
68
68
|
return container;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
function usePersistedState(storage, key, defaultValue) {
|
|
72
|
+
var state = useEagerReducer(function (prevState, value) {
|
|
73
|
+
value = isFunction(value) ? value(prevState[1]) : value;
|
|
74
|
+
return sameValue(value, prevState[1]) ? prevState : [prevState[0], value];
|
|
75
|
+
}, []);
|
|
76
|
+
var cur = state[0];
|
|
77
|
+
useMemo(function () {
|
|
78
|
+
if (cur[0] !== key) {
|
|
79
|
+
cur.splice(0, 2, key, storage && storage.has(key) ? storage.get(key) : isFunction(defaultValue) ? defaultValue() : defaultValue);
|
|
80
|
+
}
|
|
81
|
+
}, [storage, key]);
|
|
82
|
+
return [cur[1], state[1]];
|
|
83
|
+
}
|
|
84
|
+
|
|
71
85
|
export function useAppReady() {
|
|
72
86
|
return useAppReadyState().ready;
|
|
73
87
|
}
|
|
@@ -101,7 +115,7 @@ export function useRouteParam(name, defaultValue) {
|
|
|
101
115
|
|
|
102
116
|
export function useRouteState(key, defaultValue, snapshotOnUpdate) {
|
|
103
117
|
var cur = getCurrentStates();
|
|
104
|
-
var state =
|
|
118
|
+
var state = usePersistedState(cur, key, defaultValue);
|
|
105
119
|
var container = useViewContextWithEffect(function (cur) {
|
|
106
120
|
state[1](function (oldValue) {
|
|
107
121
|
return cur.has(key) ? cur.get(key) : (cur.set(key, oldValue), oldValue);
|
|
@@ -116,6 +130,13 @@ export function useRouteState(key, defaultValue, snapshotOnUpdate) {
|
|
|
116
130
|
return state;
|
|
117
131
|
}
|
|
118
132
|
|
|
133
|
+
export function useAppSessionState(key, defaultValue) {
|
|
134
|
+
var cur = app.sessionStorage;
|
|
135
|
+
var state = usePersistedState(cur, key, defaultValue);
|
|
136
|
+
updatePersistedValue(cur, key, state[0]);
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
|
|
119
140
|
export function useQueryParam(key, value, snapshotOnUpdate) {
|
|
120
141
|
if (isPlainObject(key)) {
|
|
121
142
|
snapshotOnUpdate = value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brew-js-react",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"expect-type": "^0.20.0",
|
|
39
39
|
"jest": "^27.0.6",
|
|
40
40
|
"jest-environment-jsdom": "^27.4.6",
|
|
41
|
-
"regenerator-runtime": "^0.13.9"
|
|
41
|
+
"regenerator-runtime": "^0.13.9",
|
|
42
|
+
"webpack-cli": "^7.0.2"
|
|
42
43
|
},
|
|
43
44
|
"sideEffects": [
|
|
44
45
|
"./src/app.js"
|
package/view.d.ts
CHANGED
|
@@ -87,6 +87,11 @@ export class ViewContext implements Zeta.ZetaEventDispatcher<ViewContextEventMap
|
|
|
87
87
|
*/
|
|
88
88
|
setErrorView<T>(errorView: React.ComponentType<ErrorViewProps<T>>, error: T): true | undefined;
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Unmounts and remounts the view component, or re-renders the view component if it is currently replaced by an error view.
|
|
92
|
+
*/
|
|
93
|
+
resetView(): void;
|
|
94
|
+
|
|
90
95
|
/**
|
|
91
96
|
* Adds event handlers to multiple events.
|
|
92
97
|
* @param handlers A dictionary which the keys are event names and values are the callback for each event.
|
|
@@ -244,14 +249,19 @@ export function matchView(path: string, ...views: ViewComponent<any>[]): ViewCom
|
|
|
244
249
|
* Renders view by matching current route state against registered route parameters of each supplied views.
|
|
245
250
|
* @param args A list of view components created by {@link registerView}.
|
|
246
251
|
*/
|
|
247
|
-
export function renderView(...args: ViewComponent<any>[]):
|
|
252
|
+
export function renderView(...args: ViewComponent<any>[]): React.ReactElement;
|
|
248
253
|
|
|
249
254
|
/**
|
|
250
255
|
* Renders view by matching current route state against registered route parameters of each supplied views.
|
|
251
256
|
* @param props Optional parameters passed to the root element rendered by the function.
|
|
252
257
|
* @param args A list of view components created by {@link registerView}.
|
|
253
258
|
*/
|
|
254
|
-
export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]):
|
|
259
|
+
export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]): React.ReactElement;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Resets all rendered views by unmounting and remounting the view components, or re-rendering the view components if they are currently replaced by error views.
|
|
263
|
+
*/
|
|
264
|
+
export function resetAllViews(): void;
|
|
255
265
|
|
|
256
266
|
/**
|
|
257
267
|
* Returns the app path that will render the specified view.
|
package/view.js
CHANGED
|
@@ -31,8 +31,7 @@ onAppInit(function () {
|
|
|
31
31
|
(function updateViewRecursive(next) {
|
|
32
32
|
each(next.children, function (i, v) {
|
|
33
33
|
e.waitFor(new Promise(function (resolve) {
|
|
34
|
-
v.
|
|
35
|
-
v.forceUpdate();
|
|
34
|
+
v.forceUpdate(resolve);
|
|
36
35
|
}).then(function () {
|
|
37
36
|
updateViewRecursive(v);
|
|
38
37
|
}));
|
|
@@ -72,6 +71,13 @@ definePrototype(ViewContext, {
|
|
|
72
71
|
var wrapper = _(this).wrapper;
|
|
73
72
|
return wrapper && errorView && !wrapper.setState({ error, errorView });
|
|
74
73
|
},
|
|
74
|
+
resetView: function () {
|
|
75
|
+
var self = this;
|
|
76
|
+
each(self.parent ? [self] : self.getChildren(), function (i, v) {
|
|
77
|
+
var wrapper = _(v).wrapper;
|
|
78
|
+
wrapper.setState({ key: (wrapper.state.key || 0) + 1, errorView: null });
|
|
79
|
+
});
|
|
80
|
+
},
|
|
75
81
|
on: function (event, handler) {
|
|
76
82
|
return emitter.add(this, event, handler);
|
|
77
83
|
}
|
|
@@ -125,13 +131,14 @@ definePrototype(ErrorBoundary, Component, {
|
|
|
125
131
|
}));
|
|
126
132
|
}
|
|
127
133
|
var onError = self.componentDidCatch.bind(self);
|
|
134
|
+
var key = self.state.key || 0;
|
|
128
135
|
var viewProps = {
|
|
129
136
|
errorHandler: self.errorHandler,
|
|
130
137
|
navigationType: event.navigationType,
|
|
131
138
|
viewContext: context,
|
|
132
139
|
viewData: context.page.data || {}
|
|
133
140
|
};
|
|
134
|
-
return createElement(self.Provider, null, createElement(context.view, extend({ viewProps, onError }, self.props)));
|
|
141
|
+
return createElement(self.Provider, null, createElement(context.view, extend({ key, viewProps, onError }, self.props)));
|
|
135
142
|
},
|
|
136
143
|
reset: function () {
|
|
137
144
|
this.setState({ errorView: null });
|
|
@@ -178,7 +185,6 @@ definePrototype(ViewContainer, Component, {
|
|
|
178
185
|
if (self.context.active) {
|
|
179
186
|
self.updateView();
|
|
180
187
|
}
|
|
181
|
-
self.onRender();
|
|
182
188
|
return self.views;
|
|
183
189
|
},
|
|
184
190
|
updateView: function () {
|
|
@@ -226,10 +232,12 @@ definePrototype(ViewContainer, Component, {
|
|
|
226
232
|
app.emit('pageleave', element, { pathname: context.page.path, view: V }, true);
|
|
227
233
|
return animateOut(element, 'show').then(function () {
|
|
228
234
|
self.views[0] = null;
|
|
229
|
-
|
|
235
|
+
return new Promise(function (resolve) {
|
|
236
|
+
self.forceUpdate(resolve);
|
|
237
|
+
});
|
|
230
238
|
});
|
|
231
239
|
});
|
|
232
|
-
always(promise
|
|
240
|
+
always(promise || delay(), function () {
|
|
233
241
|
app.emit('pageenter', element, { pathname: context.page.path, view: V }, true);
|
|
234
242
|
});
|
|
235
243
|
self.views.shift();
|
|
@@ -256,7 +264,7 @@ definePrototype(ViewContainer, Component, {
|
|
|
256
264
|
}) || props.defaultView;
|
|
257
265
|
}
|
|
258
266
|
});
|
|
259
|
-
fill(ViewContainer.prototype, 'abort
|
|
267
|
+
fill(ViewContainer.prototype, 'abort setActive setPage unmountView', noop);
|
|
260
268
|
|
|
261
269
|
function normalizePart(value, part) {
|
|
262
270
|
return isUndefinedOrNull(value) || value === '' || value === part ? '' : value[0] === part ? value : part + value;
|
|
@@ -399,6 +407,10 @@ export function renderView() {
|
|
|
399
407
|
return createElement(ViewContainer, { rootProps, views, defaultView });
|
|
400
408
|
}
|
|
401
409
|
|
|
410
|
+
export function resetAllViews() {
|
|
411
|
+
rootContext.resetView();
|
|
412
|
+
}
|
|
413
|
+
|
|
402
414
|
export function resolvePath(view, params) {
|
|
403
415
|
var suffix = '';
|
|
404
416
|
if (isArray(params)) {
|