brew-js-react 0.3.4 → 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,4 +1,4 @@
1
- /*! brew-js-react v0.3.4 | (c) misonou | https://hackmd.io/@misonou/brew-js-react */
1
+ /*! brew-js-react v0.4.0 | (c) misonou | https://hackmd.io/@misonou/brew-js-react */
2
2
  (function webpackUniversalModuleDefinition(root, factory) {
3
3
  if(typeof exports === 'object' && typeof module === 'object')
4
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"));
@@ -387,6 +387,7 @@ var _lib$dom = external_commonjs_zeta_dom_commonjs2_zeta_dom_amd_zeta_dom_root_z
387
387
  textInputAllowed = _lib$dom.textInputAllowed,
388
388
  beginDrag = _lib$dom.beginDrag,
389
389
  beginPinchZoom = _lib$dom.beginPinchZoom,
390
+ insertText = _lib$dom.insertText,
390
391
  getShortcut = _lib$dom.getShortcut,
391
392
  setShortcut = _lib$dom.setShortcut,
392
393
  focusable = _lib$dom.focusable,
@@ -538,12 +539,18 @@ var defaults_defaultExport = external_commonjs_brew_js_commonjs2_brew_js_amd_bre
538
539
  ;// CONCATENATED MODULE: ./src/app.js
539
540
 
540
541
 
542
+
541
543
  /** @type {Brew.AppInstance<Brew.WithRouter & Brew.WithI18n>} */
542
544
 
543
545
  var app_app;
546
+ var appInitCallabcks = [];
547
+ function onAppInit(callback) {
548
+ appInitCallabcks.push(throwNotFunction(callback));
549
+ }
544
550
  install('react', function (app_) {
545
551
  // @ts-ignore: type inference issue
546
552
  app_app = app_;
553
+ combineFn(appInitCallabcks)(app_app);
547
554
  });
548
555
  brew_js_defaults.react = true;
549
556
  ;// CONCATENATED MODULE: ./tmp/brew-js/anim.js
@@ -585,6 +592,14 @@ var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_reac
585
592
  active: true
586
593
  }));
587
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
+ });
588
603
 
589
604
  function ErrorBoundary() {
590
605
  external_commonjs_react_commonjs2_react_amd_react_root_React_.Component.apply(this, arguments);
@@ -628,7 +643,7 @@ definePrototype(ErrorBoundary, external_commonjs_react_commonjs2_react_amd_react
628
643
 
629
644
  return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(props.view, {
630
645
  onComponentLoaded: onComponentLoaded,
631
- viewData: self.props.viewData
646
+ viewProps: self.props.viewProps
632
647
  });
633
648
  },
634
649
  reset: function reset() {
@@ -649,10 +664,9 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
649
664
  var self = this;
650
665
  self.componentWillUnmount = combineFn(watch(app_app.route, function () {
651
666
  self.setActive(self.getViewComponent() === self.currentViewComponent);
652
- }), app_app.on('beforepageload', function (e) {
653
- self.waitFor = e.waitFor;
667
+ }), app_app.on('beforepageload', function () {
654
668
  self.stateId = history.state;
655
- self.updateView(e.data);
669
+ self.updateView();
656
670
  self.forceUpdate();
657
671
  }));
658
672
  },
@@ -666,7 +680,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
666
680
 
667
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);
668
682
  },
669
- updateView: function updateView(viewData) {
683
+ updateView: function updateView() {
670
684
  var self = this;
671
685
  var V = self.getViewComponent();
672
686
 
@@ -711,6 +725,10 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
711
725
  });
712
726
  notifyAsync(element, promise);
713
727
  });
728
+ var viewProps = freeze({
729
+ navigationType: view_event.navigationType,
730
+ viewData: view_event.data || {}
731
+ });
714
732
  var state = {
715
733
  view: V
716
734
  };
@@ -721,7 +739,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
721
739
  ref: initElement
722
740
  }), /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ErrorBoundary, {
723
741
  onComponentLoaded: onComponentLoaded,
724
- viewData: viewData
742
+ viewProps: viewProps
725
743
  }))));
726
744
  extend(self, {
727
745
  currentPath: app_app.path,
@@ -729,7 +747,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
729
747
  currentViewComponent: V,
730
748
  setActive: defineObservableProperty(state, 'active', true, true)
731
749
  });
732
- (self.waitFor || noop)(promise);
750
+ (view_event.waitFor || noop)(promise);
733
751
  }
734
752
  },
735
753
  getViewComponent: function getViewComponent() {
@@ -797,9 +815,7 @@ function createViewComponent(factory) {
797
815
  }
798
816
 
799
817
  return function fn(props) {
800
- var viewProps = props.viewProps || {
801
- viewData: useRouteState('_d_' + routeMap.get(fn).id, props.viewData || {})[0]
802
- };
818
+ var viewProps = props.viewProps;
803
819
  var children = !promise && factory(viewProps);
804
820
 
805
821
  if (isThenable(children)) {
@@ -909,10 +925,9 @@ function redirectTo(view, params, data) {
909
925
 
910
926
 
911
927
  var emitter = new ZetaEventContainer();
912
- var states = {};
913
928
 
914
929
  function getCurrentStates() {
915
- return states[history.state] || (states[history.state] = {});
930
+ return app_app.historyStorage.current;
916
931
  }
917
932
 
918
933
  function ViewState(key, value) {
@@ -925,7 +940,9 @@ definePrototype(ViewState, {
925
940
  return this.value;
926
941
  },
927
942
  set: function set(value) {
928
- this.value = value;
943
+ var self = this;
944
+ self.value = value;
945
+ self.store.set(self.key, value);
929
946
  },
930
947
  onPopState: function onPopState(callback) {
931
948
  throwNotFunction(callback);
@@ -987,15 +1004,15 @@ function useRouteParam(name, defaultValue) {
987
1004
  function useRouteState(key, defaultValue, snapshotOnUpdate) {
988
1005
  var container = useViewContainerState();
989
1006
  var cur = getCurrentStates();
990
- 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);
991
1008
 
992
- if (container.active && cur[key] !== state[0]) {
993
- if (snapshotOnUpdate && key in cur) {
1009
+ if (container.active && cur.get(key) !== state[0]) {
1010
+ if (snapshotOnUpdate && cur.has(key)) {
994
1011
  app_app.snapshot();
995
1012
  cur = getCurrentStates();
996
1013
  }
997
1014
 
998
- cur[key] = state[0];
1015
+ cur.set(key, state[0]);
999
1016
  }
1000
1017
 
1001
1018
  (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useEffect)(function () {
@@ -1003,7 +1020,7 @@ function useRouteState(key, defaultValue, snapshotOnUpdate) {
1003
1020
  return bind(window, 'popstate', function () {
1004
1021
  if (container.active) {
1005
1022
  var cur = getCurrentStates();
1006
- state[1](key in cur ? cur[key] : defaultValue);
1023
+ state[1](cur.has(key) ? cur.get(key) : defaultValue);
1007
1024
  }
1008
1025
  });
1009
1026
  }
@@ -1017,25 +1034,22 @@ function ViewStateContainer(props) {
1017
1034
  return {
1018
1035
  getState: function getState(uniqueId, key) {
1019
1036
  var cur = getCurrentStates();
1020
- 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));
1021
1039
 
1022
1040
  if (container.active) {
1023
- var stateId = state.stateId;
1041
+ var store = state.store;
1024
1042
 
1025
- if (stateId && (stateId !== history.state || key !== state.key)) {
1026
- var newValue = cur[key] && cur[key].value;
1043
+ if (store && (store !== cur && cur.has(key) || key !== state.key)) {
1027
1044
  emitter.emit('popstate', state, {
1028
- newValue: newValue
1029
- }); // detach value in previous history state from current one
1030
-
1031
- var previous = new ViewState(state.key, state.value);
1032
- states[stateId][previous.key] = previous;
1033
- state.value = newValue;
1045
+ newValue: value
1046
+ });
1047
+ state.value = value;
1034
1048
  state.key = key;
1035
1049
  }
1036
1050
 
1037
- state.stateId = history.state;
1038
- cur[key] = state;
1051
+ state.store = cur;
1052
+ cur.set(key, state.value);
1039
1053
  }
1040
1054
 
1041
1055
  return state;