brew-js-react 0.3.3 → 0.4.0

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/app.js CHANGED
@@ -1,12 +1,20 @@
1
+ import { combineFn, throwNotFunction } from "./include/zeta-dom/util.js";
1
2
  import { install } from "./include/brew-js/app.js";
2
3
  import defaults from "./include/brew-js/defaults.js";
3
4
 
4
5
  /** @type {Brew.AppInstance<Brew.WithRouter & Brew.WithI18n>} */
5
6
  export var app;
6
7
 
8
+ const appInitCallabcks = [];
9
+
10
+ export function onAppInit(callback) {
11
+ appInitCallabcks.push(throwNotFunction(callback));
12
+ }
13
+
7
14
  install('react', function (app_) {
8
15
  // @ts-ignore: type inference issue
9
16
  app = app_;
17
+ combineFn(appInitCallabcks)(app);
10
18
  });
11
19
 
12
20
  defaults.react = true;
@@ -1,3 +1,4 @@
1
+ /*! brew-js-react v0.4.0 | (c) misonou | https://hackmd.io/@misonou/brew-js-react */
1
2
  (function webpackUniversalModuleDefinition(root, factory) {
2
3
  if(typeof exports === 'object' && typeof module === 'object')
3
4
  module.exports = factory(require("brew-js"), require("react"), require("react-dom"), (function webpackLoadOptionalExternalModule() { try { return require("react-dom/client"); } catch(e) {} }()), require("zeta-dom"), require("zeta-dom-react"), require("waterpipe"), require("jQuery"));
@@ -209,6 +210,7 @@ __webpack_require__.d(src_namespaceObject, {
209
210
  "registerErrorView": () => (registerErrorView),
210
211
  "registerView": () => (registerView),
211
212
  "renderView": () => (renderView),
213
+ "resolvePath": () => (resolvePath),
212
214
  "useAnimateMixin": () => (useAnimateMixin),
213
215
  "useAnimateSequenceMixin": () => (useAnimateSequenceMixin),
214
216
  "useAppReady": () => (useAppReady),
@@ -385,6 +387,7 @@ var _lib$dom = external_commonjs_zeta_dom_commonjs2_zeta_dom_amd_zeta_dom_root_z
385
387
  textInputAllowed = _lib$dom.textInputAllowed,
386
388
  beginDrag = _lib$dom.beginDrag,
387
389
  beginPinchZoom = _lib$dom.beginPinchZoom,
390
+ insertText = _lib$dom.insertText,
388
391
  getShortcut = _lib$dom.getShortcut,
389
392
  setShortcut = _lib$dom.setShortcut,
390
393
  focusable = _lib$dom.focusable,
@@ -536,12 +539,18 @@ var defaults_defaultExport = external_commonjs_brew_js_commonjs2_brew_js_amd_bre
536
539
  ;// CONCATENATED MODULE: ./src/app.js
537
540
 
538
541
 
542
+
539
543
  /** @type {Brew.AppInstance<Brew.WithRouter & Brew.WithI18n>} */
540
544
 
541
545
  var app_app;
546
+ var appInitCallabcks = [];
547
+ function onAppInit(callback) {
548
+ appInitCallabcks.push(throwNotFunction(callback));
549
+ }
542
550
  install('react', function (app_) {
543
551
  // @ts-ignore: type inference issue
544
552
  app_app = app_;
553
+ combineFn(appInitCallabcks)(app_app);
545
554
  });
546
555
  brew_js_defaults.react = true;
547
556
  ;// CONCATENATED MODULE: ./tmp/brew-js/anim.js
@@ -583,6 +592,14 @@ var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_reac
583
592
  active: true
584
593
  }));
585
594
  var errorView;
595
+ /** @type {Partial<Zeta.ZetaEventType<"beforepageload", Brew.RouterEventMap, Element>>} */
596
+
597
+ var view_event = {};
598
+ onAppInit(function () {
599
+ app_app.on('beforepageload', function (e) {
600
+ view_event = e;
601
+ });
602
+ });
586
603
 
587
604
  function ErrorBoundary() {
588
605
  external_commonjs_react_commonjs2_react_amd_react_root_React_.Component.apply(this, arguments);
@@ -625,7 +642,8 @@ definePrototype(ErrorBoundary, external_commonjs_react_commonjs2_react_amd_react
625
642
  }
626
643
 
627
644
  return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(props.view, {
628
- onComponentLoaded: onComponentLoaded
645
+ onComponentLoaded: onComponentLoaded,
646
+ viewProps: self.props.viewProps
629
647
  });
630
648
  },
631
649
  reset: function reset() {
@@ -646,9 +664,9 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
646
664
  var self = this;
647
665
  self.componentWillUnmount = combineFn(watch(app_app.route, function () {
648
666
  self.setActive(self.getViewComponent() === self.currentViewComponent);
649
- }), app_app.on('beforepageload', function (e) {
650
- self.waitFor = e.waitFor;
667
+ }), app_app.on('beforepageload', function () {
651
668
  self.stateId = history.state;
669
+ self.updateView();
652
670
  self.forceUpdate();
653
671
  }));
654
672
  },
@@ -656,16 +674,20 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
656
674
  /** @type {any} */
657
675
  var self = this;
658
676
 
659
- if (history.state !== self.stateId) {
660
- return self.lastChild || null;
677
+ if (history.state === self.stateId) {
678
+ self.updateView();
661
679
  }
662
680
 
681
+ return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(external_commonjs_react_commonjs2_react_amd_react_root_React_.Fragment, null, self.prevView, self.currentView);
682
+ },
683
+ updateView: function updateView() {
684
+ var self = this;
663
685
  var V = self.getViewComponent();
664
686
 
665
687
  if (V) {
666
688
  // ensure the current path actually corresponds to the matched view
667
689
  // when some views are not included in the list of allowed views
668
- var targetPath = linkTo(V, getCurrentParams(V, true));
690
+ var targetPath = resolvePath(V, getCurrentParams(V, true));
669
691
 
670
692
  if (targetPath !== removeQueryAndHash(app_app.path)) {
671
693
  app_app.navigate(targetPath, true);
@@ -703,6 +725,10 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
703
725
  });
704
726
  notifyAsync(element, promise);
705
727
  });
728
+ var viewProps = freeze({
729
+ navigationType: view_event.navigationType,
730
+ viewData: view_event.data || {}
731
+ });
706
732
  var state = {
707
733
  view: V
708
734
  };
@@ -712,7 +738,8 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
712
738
  }, /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ViewStateContainer, null, /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement('div', extend({}, self.props.rootProps, {
713
739
  ref: initElement
714
740
  }), /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ErrorBoundary, {
715
- onComponentLoaded: onComponentLoaded
741
+ onComponentLoaded: onComponentLoaded,
742
+ viewProps: viewProps
716
743
  }))));
717
744
  extend(self, {
718
745
  currentPath: app_app.path,
@@ -720,12 +747,8 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
720
747
  currentViewComponent: V,
721
748
  setActive: defineObservableProperty(state, 'active', true, true)
722
749
  });
723
- (self.waitFor || noop)(promise);
750
+ (view_event.waitFor || noop)(promise);
724
751
  }
725
-
726
- var child = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(external_commonjs_react_commonjs2_react_amd_react_root_React_.Fragment, null, self.prevView, self.currentView);
727
- self.lastChild = child;
728
- return child;
729
752
  },
730
753
  getViewComponent: function getViewComponent() {
731
754
  var props = this.props;
@@ -791,8 +814,8 @@ function createViewComponent(factory) {
791
814
  factory = external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement.bind(null, factory);
792
815
  }
793
816
 
794
- return function (props) {
795
- var viewProps = Object.freeze(props.viewProps || {});
817
+ return function fn(props) {
818
+ var viewProps = props.viewProps;
796
819
  var children = !promise && factory(viewProps);
797
820
 
798
821
  if (isThenable(children)) {
@@ -874,7 +897,7 @@ function renderView() {
874
897
  defaultView: defaultView
875
898
  });
876
899
  }
877
- function linkTo(view, params) {
900
+ function resolvePath(view, params) {
878
901
  var state = routeMap.get(view);
879
902
 
880
903
  if (!state) {
@@ -884,11 +907,14 @@ function linkTo(view, params) {
884
907
  var newParams = extend(getCurrentParams(view), getCurrentParams(view, true, params || {}), state.params);
885
908
  return app_app.route.getPath(newParams);
886
909
  }
887
- function navigateTo(view, params) {
888
- return app_app.navigate(linkTo(view, params));
910
+ function linkTo(view, params) {
911
+ return app_app.toHref(resolvePath(view, params));
912
+ }
913
+ function navigateTo(view, params, data, replace) {
914
+ return app_app.navigate(resolvePath(view, params), replace, data && freeze(extend({}, data)));
889
915
  }
890
- function redirectTo(view, params) {
891
- return app_app.navigate(linkTo(view, params), true);
916
+ function redirectTo(view, params, data) {
917
+ return navigateTo(view, params, data, true);
892
918
  }
893
919
  ;// CONCATENATED MODULE: ./src/hooks.js
894
920
 
@@ -899,10 +925,9 @@ function redirectTo(view, params) {
899
925
 
900
926
 
901
927
  var emitter = new ZetaEventContainer();
902
- var states = {};
903
928
 
904
929
  function getCurrentStates() {
905
- return states[history.state] || (states[history.state] = {});
930
+ return app_app.historyStorage.current;
906
931
  }
907
932
 
908
933
  function ViewState(key, value) {
@@ -915,7 +940,9 @@ definePrototype(ViewState, {
915
940
  return this.value;
916
941
  },
917
942
  set: function set(value) {
918
- this.value = value;
943
+ var self = this;
944
+ self.value = value;
945
+ self.store.set(self.key, value);
919
946
  },
920
947
  onPopState: function onPopState(callback) {
921
948
  throwNotFunction(callback);
@@ -977,24 +1004,26 @@ function useRouteParam(name, defaultValue) {
977
1004
  function useRouteState(key, defaultValue, snapshotOnUpdate) {
978
1005
  var container = useViewContainerState();
979
1006
  var cur = getCurrentStates();
980
- var state = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(key in cur ? cur[key] : defaultValue);
1007
+ var state = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(cur.has(key) ? cur.get(key) : defaultValue);
981
1008
 
982
- if (container.active && cur[key] !== state[0]) {
983
- if (snapshotOnUpdate && key in cur) {
1009
+ if (container.active && cur.get(key) !== state[0]) {
1010
+ if (snapshotOnUpdate && cur.has(key)) {
984
1011
  app_app.snapshot();
985
1012
  cur = getCurrentStates();
986
1013
  }
987
1014
 
988
- cur[key] = state[0];
1015
+ cur.set(key, state[0]);
989
1016
  }
990
1017
 
991
1018
  (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useEffect)(function () {
992
- return bind(window, 'popstate', function () {
993
- if (snapshotOnUpdate && container.active) {
994
- var cur = getCurrentStates();
995
- state[1](key in cur ? cur[key] : defaultValue);
996
- }
997
- });
1019
+ if (snapshotOnUpdate) {
1020
+ return bind(window, 'popstate', function () {
1021
+ if (container.active) {
1022
+ var cur = getCurrentStates();
1023
+ state[1](cur.has(key) ? cur.get(key) : defaultValue);
1024
+ }
1025
+ });
1026
+ }
998
1027
  }, [container, snapshotOnUpdate]);
999
1028
  return state;
1000
1029
  }
@@ -1005,25 +1034,22 @@ function ViewStateContainer(props) {
1005
1034
  return {
1006
1035
  getState: function getState(uniqueId, key) {
1007
1036
  var cur = getCurrentStates();
1008
- var state = cache[uniqueId] || (cache[uniqueId] = new ViewState(key, cur[key] && cur[key].value));
1037
+ var value = cur.get(key);
1038
+ var state = cache[uniqueId] || (cache[uniqueId] = new ViewState(key, value));
1009
1039
 
1010
1040
  if (container.active) {
1011
- var stateId = state.stateId;
1041
+ var store = state.store;
1012
1042
 
1013
- if (stateId && (stateId !== history.state || key !== state.key)) {
1014
- var newValue = cur[key] && cur[key].value;
1043
+ if (store && (store !== cur && cur.has(key) || key !== state.key)) {
1015
1044
  emitter.emit('popstate', state, {
1016
- newValue: newValue
1017
- }); // detach value in previous history state from current one
1018
-
1019
- var previous = new ViewState(state.key, state.value);
1020
- states[stateId][previous.key] = previous;
1021
- state.value = newValue;
1045
+ newValue: value
1046
+ });
1047
+ state.value = value;
1022
1048
  state.key = key;
1023
1049
  }
1024
1050
 
1025
- state.stateId = history.state;
1026
- cur[key] = state;
1051
+ state.store = cur;
1052
+ cur.set(key, state.value);
1027
1053
  }
1028
1054
 
1029
1055
  return state;
@@ -1747,6 +1773,15 @@ function createUseFunction(ctor) {
1747
1773
  };
1748
1774
  }
1749
1775
 
1776
+ function disposeMixin(mixin) {
1777
+ mixin.disposed = true;
1778
+ setImmediate(function () {
1779
+ if (mixin.disposed) {
1780
+ mixin.dispose();
1781
+ }
1782
+ });
1783
+ }
1784
+
1750
1785
  var useAnimateMixin = createUseFunction(AnimateMixin);
1751
1786
  var useAnimateSequenceMixin = createUseFunction(AnimateSequenceMixin);
1752
1787
  var useFlyoutMixin = createUseFunction(FlyoutMixin);
@@ -1758,7 +1793,8 @@ function useMixin(ctor) {
1758
1793
  return new ctor();
1759
1794
  })[0].reset();
1760
1795
  (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useEffect)(function () {
1761
- return mixin.dispose.bind(mixin);
1796
+ mixin.disposed = false;
1797
+ return disposeMixin.bind(0, mixin);
1762
1798
  }, []);
1763
1799
  return mixin;
1764
1800
  }