brew-js-react 0.7.1 → 0.7.2
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 +11 -1
- package/dialog.js +10 -1
- package/dist/brew-js-react.js +116 -66
- 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 +10 -0
- package/view.js +13 -1
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.2",
|
|
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.
|
|
@@ -253,6 +258,11 @@ export function renderView(...args: ViewComponent<any>[]): JSX.Element;
|
|
|
253
258
|
*/
|
|
254
259
|
export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]): JSX.Element;
|
|
255
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;
|
|
265
|
+
|
|
256
266
|
/**
|
|
257
267
|
* Returns the app path that will render the specified view.
|
|
258
268
|
*
|
package/view.js
CHANGED
|
@@ -72,6 +72,13 @@ definePrototype(ViewContext, {
|
|
|
72
72
|
var wrapper = _(this).wrapper;
|
|
73
73
|
return wrapper && errorView && !wrapper.setState({ error, errorView });
|
|
74
74
|
},
|
|
75
|
+
resetView: function () {
|
|
76
|
+
var self = this;
|
|
77
|
+
each(self.parent ? [self] : self.getChildren(), function (i, v) {
|
|
78
|
+
var wrapper = _(v).wrapper;
|
|
79
|
+
wrapper.setState({ key: (wrapper.state.key || 0) + 1, errorView: null });
|
|
80
|
+
});
|
|
81
|
+
},
|
|
75
82
|
on: function (event, handler) {
|
|
76
83
|
return emitter.add(this, event, handler);
|
|
77
84
|
}
|
|
@@ -125,13 +132,14 @@ definePrototype(ErrorBoundary, Component, {
|
|
|
125
132
|
}));
|
|
126
133
|
}
|
|
127
134
|
var onError = self.componentDidCatch.bind(self);
|
|
135
|
+
var key = self.state.key || 0;
|
|
128
136
|
var viewProps = {
|
|
129
137
|
errorHandler: self.errorHandler,
|
|
130
138
|
navigationType: event.navigationType,
|
|
131
139
|
viewContext: context,
|
|
132
140
|
viewData: context.page.data || {}
|
|
133
141
|
};
|
|
134
|
-
return createElement(self.Provider, null, createElement(context.view, extend({ viewProps, onError }, self.props)));
|
|
142
|
+
return createElement(self.Provider, null, createElement(context.view, extend({ key, viewProps, onError }, self.props)));
|
|
135
143
|
},
|
|
136
144
|
reset: function () {
|
|
137
145
|
this.setState({ errorView: null });
|
|
@@ -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)) {
|