brew-js-react 0.7.2 → 0.7.4

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/hooks.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type SetQueryParamAction<T> = Partial<T> | ((prev: Readonly<T>) => Partial<T>);
2
2
 
3
- export const ViewStateContainer: React.FC;
3
+ export const ViewStateContainer: React.FC<React.PropsWithChildren<{}>>;
4
4
 
5
5
  /**
6
6
  * Returns if the app has completed initialization, and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brew-js-react",
3
- "version": "0.7.2",
3
+ "version": "0.7.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/view.d.ts CHANGED
@@ -249,14 +249,14 @@ export function matchView(path: string, ...views: ViewComponent<any>[]): ViewCom
249
249
  * Renders view by matching current route state against registered route parameters of each supplied views.
250
250
  * @param args A list of view components created by {@link registerView}.
251
251
  */
252
- export function renderView(...args: ViewComponent<any>[]): JSX.Element;
252
+ export function renderView(...args: ViewComponent<any>[]): React.ReactElement;
253
253
 
254
254
  /**
255
255
  * Renders view by matching current route state against registered route parameters of each supplied views.
256
256
  * @param props Optional parameters passed to the root element rendered by the function.
257
257
  * @param args A list of view components created by {@link registerView}.
258
258
  */
259
- export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]): JSX.Element;
259
+ export function renderView(props: ViewComponentRootProps, ...args: ViewComponent<any>[]): React.ReactElement;
260
260
 
261
261
  /**
262
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.
package/view.js CHANGED
@@ -2,7 +2,7 @@ import { Component, createContext, createElement, useContext, useLayoutEffect }
2
2
  import { createAsyncScope, useAsync } from "zeta-dom-react";
3
3
  import dom, { reportError } from "zeta-dom/dom";
4
4
  import { ZetaEventContainer } from "zeta-dom/events";
5
- import { always, any, arrRemove, catchAsync, createPrivateStore, defineObservableProperty, defineOwnProperty, definePrototype, delay, each, exclude, executeOnce, extend, fill, freeze, grep, isArray, isFunction, isPlainObject, isThenable, isUndefinedOrNull, keys, makeArray, map, noop, pick, randomId, setImmediate, single, throwNotFunction, watch } from "zeta-dom/util";
5
+ import { always, any, arrRemove, catchAsync, combineFn, createPrivateStore, defineObservableProperty, defineOwnProperty, definePrototype, delay, each, exclude, executeOnce, extend, fill, freeze, grep, hasOwnProperty, isArray, isFunction, isPlainObject, isThenable, isUndefinedOrNull, keys, makeArray, map, noop, pick, randomId, setImmediate, single, throwNotFunction, watch } from "zeta-dom/util";
6
6
  import { animateIn, animateOut } from "brew-js/anim";
7
7
  import { toQueryString } from "brew-js/util/common";
8
8
  import { removeQueryAndHash } from "brew-js/util/path";
@@ -30,10 +30,7 @@ onAppInit(function () {
30
30
  event = e;
31
31
  (function updateViewRecursive(next) {
32
32
  each(next.children, function (i, v) {
33
- e.waitFor(new Promise(function (resolve) {
34
- v.onRender = resolve;
35
- v.forceUpdate();
36
- }).then(function () {
33
+ e.waitFor(v.renderAsync().then(function () {
37
34
  updateViewRecursive(v);
38
35
  }));
39
36
  });
@@ -149,6 +146,7 @@ definePrototype(ErrorBoundary, Component, {
149
146
  function ViewContainer() {
150
147
  Component.apply(this, arguments);
151
148
  this.views = [];
149
+ this.dispose = new Set();
152
150
  }
153
151
  ViewContainer.contextType = StateContext;
154
152
 
@@ -166,6 +164,7 @@ definePrototype(ViewContainer, Component, {
166
164
  setImmediate(function () {
167
165
  if (self.currentContext && !self.currentContext.active) {
168
166
  self.unmountView();
167
+ combineFn(self.dispose)();
169
168
  }
170
169
  });
171
170
  };
@@ -180,13 +179,22 @@ definePrototype(ViewContainer, Component, {
180
179
  this.currentContext = context;
181
180
  (this.props.rootProps.ref || {}).current = context;
182
181
  },
182
+ renderAsync: function () {
183
+ var self = this;
184
+ return new Promise(function (resolve) {
185
+ self.dispose.add(resolve);
186
+ self.forceUpdate(function () {
187
+ self.dispose.delete(resolve);
188
+ resolve();
189
+ });
190
+ });
191
+ },
183
192
  render: function () {
184
193
  /** @type {any} */
185
194
  var self = this;
186
195
  if (self.context.active) {
187
196
  self.updateView();
188
197
  }
189
- self.onRender();
190
198
  return self.views;
191
199
  },
192
200
  updateView: function () {
@@ -234,10 +242,10 @@ definePrototype(ViewContainer, Component, {
234
242
  app.emit('pageleave', element, { pathname: context.page.path, view: V }, true);
235
243
  return animateOut(element, 'show').then(function () {
236
244
  self.views[0] = null;
237
- self.forceUpdate();
245
+ return self.renderAsync();
238
246
  });
239
247
  });
240
- always(promise, delay).then(function () {
248
+ always(promise || delay(), function () {
241
249
  app.emit('pageenter', element, { pathname: context.page.path, view: V }, true);
242
250
  });
243
251
  self.views.shift();
@@ -264,7 +272,7 @@ definePrototype(ViewContainer, Component, {
264
272
  }) || props.defaultView;
265
273
  }
266
274
  });
267
- fill(ViewContainer.prototype, 'abort onRender setActive setPage unmountView', noop);
275
+ fill(ViewContainer.prototype, 'abort setActive setPage unmountView', noop);
268
276
 
269
277
  function normalizePart(value, part) {
270
278
  return isUndefinedOrNull(value) || value === '' || value === part ? '' : value[0] === part ? value : part + value;
@@ -273,31 +281,26 @@ function normalizePart(value, part) {
273
281
  function getCurrentParams(view, params) {
274
282
  var state = routeMap.get(view);
275
283
  if (!state.maxParams) {
276
- var matchers = exclude(state.matchers, ['remainingSegments']);
284
+ var maxParams = {};
277
285
  var matched = map(app.routes, function (v) {
278
286
  var route = app.parseRoute(v);
279
- var matched = route.length && !any(matchers, function (v, i) {
280
- var pos = route.params[i];
281
- return (v ? !(pos >= 0) : pos < route.minLength) || (!isFunction(v) && !route.match(i, v));
287
+ var routeParams = route.params;
288
+ var params = {};
289
+ var invalid = single(routeParams, (v, i) => {
290
+ if (usedParams[i] && !state.matchers[i]) {
291
+ return v < route.minLength;
292
+ }
293
+ params[i] = true;
294
+ }) || single(state.matchers, function (v, i) {
295
+ return i !== 'remainingSegments' && !(isFunction(v) ? hasOwnProperty(routeParams, i) : v ? route.match(i, v) : routeParams[i] >= route.minLength);
282
296
  });
283
- return matched ? route : null;
297
+ return invalid ? null : extend(maxParams, params) && route;
298
+ });
299
+ var last = matched.slice(-1)[0] || {};
300
+ state.maxParams = ['remainingSegments'].concat(keys(maxParams));
301
+ state.minParams = map(last.params, function (v, i) {
302
+ return state.params[i] || v >= last.minLength ? null : i;
284
303
  });
285
- if (matched[1]) {
286
- matched = grep(matched, function (v) {
287
- return !single(v.params, function (v, i) {
288
- return usedParams[i] && !matchers[i];
289
- });
290
- });
291
- }
292
- if (matched[0]) {
293
- var last = matched.slice(-1)[0];
294
- state.maxParams = keys(extend.apply(0, [{ remainingSegments: true }].concat(matched.map(function (v) {
295
- return v.params;
296
- }))));
297
- state.minParams = map(last.params, function (v, i) {
298
- return state.params[i] || v >= last.minLength ? null : i;
299
- });
300
- }
301
304
  }
302
305
  return extend(pick(app.route, state.minParams), params && pick(params, state.maxParams), state.params);
303
306
  }