brew-js-react 0.7.0 → 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 +128 -76
- 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/mixins/AnimateSequenceMixin.d.ts +1 -0
- package/mixins/Mixin.d.ts +1 -1
- package/mixins/StaticAttributeMixin.d.ts +1 -0
- package/package.json +4 -4
- package/view.d.ts +18 -1
- package/view.js +23 -9
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;
|
|
@@ -18,5 +18,6 @@ export default class AnimateSequenceMixin extends AnimateMixin {
|
|
|
18
18
|
* Applies custom attributes to element.
|
|
19
19
|
* @private It is used internally by mixins and is declared for type inference.
|
|
20
20
|
*/
|
|
21
|
+
// @ts-ignore
|
|
21
22
|
getCustomAttributes(): Record<'animate-sequence' | 'animate-sequence-type' | 'animate-out', string>;
|
|
22
23
|
}
|
package/mixins/Mixin.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import StaticAttributeMixin from "./StaticAttributeMixin";
|
|
|
5
5
|
export type MixinProps<T extends Element, M> = MixinDefaultProps<T> & UnionToIntersection<M extends CustomAttributeProvider<infer P> ? P : never>;
|
|
6
6
|
|
|
7
7
|
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
8
|
-
type MixinTypes<T extends unknown[]> = Extract<Zeta.ArrayMember<T>, CustomAttributeProvider>;
|
|
8
|
+
type MixinTypes<T extends readonly unknown[]> = Extract<Zeta.ArrayMember<T>, CustomAttributeProvider>;
|
|
9
9
|
|
|
10
10
|
interface MixinDefaultProps<T extends Element> {
|
|
11
11
|
ref: React.RefCallback<T>;
|
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",
|
|
@@ -28,18 +28,18 @@
|
|
|
28
28
|
"@babel/preset-react": "^7.16.7",
|
|
29
29
|
"@jest/globals": "^26.6.2",
|
|
30
30
|
"@misonou/build-utils": "^1.3.1",
|
|
31
|
-
"@misonou/test-utils": "^1.
|
|
31
|
+
"@misonou/test-utils": "^1.4.3",
|
|
32
32
|
"@testing-library/dom": "^8.11.3",
|
|
33
33
|
"@testing-library/react": "^12.1.2",
|
|
34
34
|
"@testing-library/react-hooks": "^7.0.2",
|
|
35
35
|
"@types/jest": "^26.0.15",
|
|
36
36
|
"babel-loader": "^9.1.3",
|
|
37
|
-
"check-dts": "^0.8.2",
|
|
38
37
|
"cross-env": "^7.0.2",
|
|
39
38
|
"expect-type": "^0.20.0",
|
|
40
39
|
"jest": "^27.0.6",
|
|
41
40
|
"jest-environment-jsdom": "^27.4.6",
|
|
42
|
-
"regenerator-runtime": "^0.13.9"
|
|
41
|
+
"regenerator-runtime": "^0.13.9",
|
|
42
|
+
"webpack-cli": "^7.0.2"
|
|
43
43
|
},
|
|
44
44
|
"sideEffects": [
|
|
45
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.
|
|
@@ -98,7 +103,14 @@ export class ViewContext implements Zeta.ZetaEventDispatcher<ViewContextEventMap
|
|
|
98
103
|
* @param event Name of the event.
|
|
99
104
|
* @param handler A callback function to be fired when the specified event is triggered.
|
|
100
105
|
*/
|
|
101
|
-
on<E extends
|
|
106
|
+
on<E extends Zeta.StringKeyOf<ViewContextEventMap>>(event: E, handler: Zeta.ZetaEventHandler<E, ViewContextEventMap, ViewContext>): Zeta.UnregisterCallback;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Adds an event handler to a specific event.
|
|
110
|
+
* @param event Name of the event.
|
|
111
|
+
* @param handler A callback function to be fired when the specified event is triggered.
|
|
112
|
+
*/
|
|
113
|
+
on<E extends Zeta.HintedStringKeyOf<ViewContextEventMap>>(event: E, handler: Zeta.ZetaEventHandler<Zeta.WhitespaceDelimited<E>, ViewContextEventMap, ViewContext>): Zeta.UnregisterCallback;
|
|
102
114
|
}
|
|
103
115
|
|
|
104
116
|
export interface ViewProps<S = {}> {
|
|
@@ -246,6 +258,11 @@ export function renderView(...args: ViewComponent<any>[]): JSX.Element;
|
|
|
246
258
|
*/
|
|
247
259
|
export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]): JSX.Element;
|
|
248
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
|
+
|
|
249
266
|
/**
|
|
250
267
|
* Returns the app path that will render the specified view.
|
|
251
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
|
}
|
|
@@ -105,11 +112,13 @@ definePrototype(ErrorBoundary, Component, {
|
|
|
105
112
|
var context = self.props.context;
|
|
106
113
|
if (!context.container) {
|
|
107
114
|
setImmediate(function () {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
115
|
+
if (!self.errorHandler) {
|
|
116
|
+
extend(self, createAsyncScope(context.container));
|
|
117
|
+
dom.on(context.container, 'error', function (e) {
|
|
118
|
+
return emitter.emit(e, context, { error: e.error }, false);
|
|
119
|
+
});
|
|
120
|
+
self.forceUpdate();
|
|
121
|
+
}
|
|
113
122
|
});
|
|
114
123
|
return null;
|
|
115
124
|
}
|
|
@@ -123,13 +132,14 @@ definePrototype(ErrorBoundary, Component, {
|
|
|
123
132
|
}));
|
|
124
133
|
}
|
|
125
134
|
var onError = self.componentDidCatch.bind(self);
|
|
135
|
+
var key = self.state.key || 0;
|
|
126
136
|
var viewProps = {
|
|
127
137
|
errorHandler: self.errorHandler,
|
|
128
138
|
navigationType: event.navigationType,
|
|
129
139
|
viewContext: context,
|
|
130
140
|
viewData: context.page.data || {}
|
|
131
141
|
};
|
|
132
|
-
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)));
|
|
133
143
|
},
|
|
134
144
|
reset: function () {
|
|
135
145
|
this.setState({ errorView: null });
|
|
@@ -312,7 +322,7 @@ function createViewComponent(factory) {
|
|
|
312
322
|
}
|
|
313
323
|
return function fn(props) {
|
|
314
324
|
var children = promise || factory(props.viewProps);
|
|
315
|
-
if (isThenable(children)) {
|
|
325
|
+
if (promise || isThenable(children)) {
|
|
316
326
|
promise = children;
|
|
317
327
|
catchAsync(promise);
|
|
318
328
|
} else {
|
|
@@ -320,8 +330,8 @@ function createViewComponent(factory) {
|
|
|
320
330
|
return children;
|
|
321
331
|
}
|
|
322
332
|
var component = useAsync(function () {
|
|
323
|
-
|
|
324
|
-
|
|
333
|
+
promise = true;
|
|
334
|
+
return (isThenable(children) || factory()).then(null, function (error) {
|
|
325
335
|
props.onError(error);
|
|
326
336
|
});
|
|
327
337
|
})[0];
|
|
@@ -397,6 +407,10 @@ export function renderView() {
|
|
|
397
407
|
return createElement(ViewContainer, { rootProps, views, defaultView });
|
|
398
408
|
}
|
|
399
409
|
|
|
410
|
+
export function resetAllViews() {
|
|
411
|
+
rootContext.resetView();
|
|
412
|
+
}
|
|
413
|
+
|
|
400
414
|
export function resolvePath(view, params) {
|
|
401
415
|
var suffix = '';
|
|
402
416
|
if (isArray(params)) {
|