brew-js-react 0.2.4 → 0.2.7

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.js CHANGED
@@ -1,113 +1,119 @@
1
- import { createElement, useEffect, useRef, useState } from "react";
2
- import { ViewStateProvider } from "zeta-dom-react";
3
- import { definePrototype, extend, kv, setImmediateOnce, throwNotFunction } from "./include/zeta-dom/util.js";
4
- import { ZetaEventContainer } from "./include/zeta-dom/events.js";
5
- import { app } from "./app.js";
6
- import { useViewContainerState } from "./view.js";
7
-
8
- const emitter = new ZetaEventContainer();
9
- const states = {};
10
-
11
- function getCurrentStates() {
12
- return states[history.state] || (states[history.state] = {});
13
- }
14
-
15
- function ViewState(value) {
16
- this.value = value;
17
- }
18
-
19
- definePrototype(ViewState, {
20
- get: function () {
21
- return this.value;
22
- },
23
- set: function (value) {
24
- this.value = value;
25
- },
26
- onPopState: function (callback) {
27
- throwNotFunction(callback);
28
- return emitter.add(this, 'popstate', function (e) {
29
- callback.call(this, e.newValue);
30
- });
31
- }
32
- });
33
-
34
- export function useAppReady() {
35
- const sReady = useState(false);
36
- const ready = sReady[0], setReady = sReady[1];
37
- useEffect(function () {
38
- app.ready.then(function () {
39
- setReady(true);
40
- });
41
- }, []);
42
- return ready;
43
- }
44
-
45
- export function useRouteParam(name, defaultValue) {
46
- const container = useViewContainerState();
47
- const route = app.route;
48
- const value = route[name] || '';
49
- const ref = useRef(value);
50
- const forceUpdate = useState()[1];
51
- useEffect(function () {
52
- var setValue = function () {
53
- var current = route[name] || '';
54
- if (container.active && current !== ref.current) {
55
- ref.current = current;
56
- forceUpdate({});
57
- }
58
- };
59
- // route parameter might be changed after state initialization and before useEffect hook is called
60
- setValue();
61
- if (name in route) {
62
- return route.watch(name, function () {
63
- setImmediateOnce(setValue);
64
- });
65
- }
66
- console.error('Route parameter ' + name + ' does not exist');
67
- }, [name, defaultValue]);
68
- ref.current = value;
69
- if (!value && defaultValue !== undefined) {
70
- app.navigate(route.getPath(extend({}, route, kv(name, defaultValue))), true);
71
- }
72
- return value;
73
- }
74
-
75
- export function useRouteState(key, defaultValue) {
76
- const container = useViewContainerState();
77
- const cur = getCurrentStates();
78
- const state = useState(key in cur ? cur[key] : defaultValue);
79
- if (container.active) {
80
- cur[key] = state[0];
81
- }
82
- return state;
83
- }
84
-
85
- export function ViewStateContainer(props) {
86
- const container = useViewContainerState();
87
- const provider = useState(function () {
88
- const cache = {};
89
- return {
90
- getState: function (uniqueId, key) {
91
- var cur = getCurrentStates();
92
- var state = cache[uniqueId] || (cache[uniqueId] = new ViewState(cur[key] && cur[key].value));
93
- if (container.active) {
94
- var stateId = state.stateId;
95
- if (stateId && stateId !== history.state) {
96
- var newValue = cur[key] && cur[key].value;
97
- emitter.emit('popstate', state, {
98
- newValue: newValue
99
- });
100
- // detach value in previous history state from current one
101
- var previous = new ViewState(state.value);
102
- states[stateId][key] = previous;
103
- state.value = newValue;
104
- }
105
- state.stateId = history.state;
106
- cur[key] = state;
107
- }
108
- return state;
109
- }
110
- };
111
- })[0];
112
- return createElement(ViewStateProvider, { value: provider }, props.children);
113
- }
1
+ import { createElement, useEffect, useRef, useState } from "react";
2
+ import { ViewStateProvider } from "zeta-dom-react";
3
+ import { definePrototype, extend, kv, setImmediateOnce, throwNotFunction, watch } from "./include/zeta-dom/util.js";
4
+ import { ZetaEventContainer } from "./include/zeta-dom/events.js";
5
+ import { app } from "./app.js";
6
+ import { useViewContainerState } from "./view.js";
7
+
8
+ const emitter = new ZetaEventContainer();
9
+ const states = {};
10
+
11
+ function getCurrentStates() {
12
+ return states[history.state] || (states[history.state] = {});
13
+ }
14
+
15
+ function ViewState(key, value) {
16
+ this.key = key;
17
+ this.value = value;
18
+ }
19
+
20
+ definePrototype(ViewState, {
21
+ get: function () {
22
+ return this.value;
23
+ },
24
+ set: function (value) {
25
+ this.value = value;
26
+ },
27
+ onPopState: function (callback) {
28
+ throwNotFunction(callback);
29
+ return emitter.add(this, 'popstate', function (e) {
30
+ callback.call(this, e.newValue);
31
+ });
32
+ }
33
+ });
34
+
35
+ export function useAppReady() {
36
+ const sReady = useState(false);
37
+ const ready = sReady[0], setReady = sReady[1];
38
+ useEffect(function () {
39
+ app.ready.then(function () {
40
+ setReady(true);
41
+ });
42
+ }, []);
43
+ return ready;
44
+ }
45
+
46
+ export function useRouteParam(name, defaultValue) {
47
+ const container = useViewContainerState();
48
+ const route = app.route;
49
+ const value = route[name] || '';
50
+ const ref = useRef(value);
51
+ const forceUpdate = useState()[1];
52
+ useEffect(function () {
53
+ var setValue = function () {
54
+ var current = route[name] || '';
55
+ if (current !== ref.current) {
56
+ if (container.active) {
57
+ ref.current = current;
58
+ forceUpdate({});
59
+ } else {
60
+ watch(container, 'active', setValue);
61
+ }
62
+ }
63
+ };
64
+ // route parameter might be changed after state initialization and before useEffect hook is called
65
+ setValue();
66
+ if (name in route) {
67
+ return route.watch(name, function () {
68
+ setImmediateOnce(setValue);
69
+ });
70
+ }
71
+ console.error('Route parameter ' + name + ' does not exist');
72
+ }, [name, defaultValue]);
73
+ ref.current = value;
74
+ if (!value && defaultValue !== undefined) {
75
+ app.navigate(route.getPath(extend({}, route, kv(name, defaultValue))), true);
76
+ }
77
+ return value;
78
+ }
79
+
80
+ export function useRouteState(key, defaultValue) {
81
+ const container = useViewContainerState();
82
+ const cur = getCurrentStates();
83
+ const state = useState(key in cur ? cur[key] : defaultValue);
84
+ if (container.active) {
85
+ cur[key] = state[0];
86
+ }
87
+ return state;
88
+ }
89
+
90
+ export function ViewStateContainer(props) {
91
+ const container = useViewContainerState();
92
+ const provider = useState(function () {
93
+ const cache = {};
94
+ return {
95
+ getState: function (uniqueId, key) {
96
+ var cur = getCurrentStates();
97
+ var state = cache[uniqueId] || (cache[uniqueId] = new ViewState(key, cur[key] && cur[key].value));
98
+ if (container.active) {
99
+ var stateId = state.stateId;
100
+ if (stateId && (stateId !== history.state || key !== state.key)) {
101
+ var newValue = cur[key] && cur[key].value;
102
+ emitter.emit('popstate', state, {
103
+ newValue: newValue
104
+ });
105
+ // detach value in previous history state from current one
106
+ var previous = new ViewState(state.key, state.value);
107
+ states[stateId][previous.key] = previous;
108
+ state.value = newValue;
109
+ state.key = key;
110
+ }
111
+ state.stateId = history.state;
112
+ cur[key] = state;
113
+ }
114
+ return state;
115
+ }
116
+ };
117
+ })[0];
118
+ return createElement(ViewStateProvider, { value: provider }, props.children);
119
+ }
@@ -0,0 +1 @@
1
+ export * from "brew-js/util/path";
@@ -5,7 +5,7 @@ import { AnimationEffect } from "./AnimateMixin";
5
5
  export interface FlyoutMixinOptions {
6
6
  modal?: boolean;
7
7
  tabThrough?: boolean;
8
- swipeToDismiss?: Zeta.Direction;
8
+ swipeToDismiss?: 'up' | 'down' | 'left' | 'right';
9
9
  }
10
10
 
11
11
  export default class FlyoutMixin extends ClassNameMixin {
@@ -1,12 +1,11 @@
1
- import { defineAliasProperty, definePrototype, each, extend, kv, makeArray, randomId } from "../include/zeta-dom/util.js";
1
+ import { defineAliasProperty, definePrototype, each, extend, makeArray } from "../include/zeta-dom/util.js";
2
2
  import { closeFlyout, openFlyout } from "../include/brew-js/domAction.js";
3
- import { declareVar, getVar } from "../include/brew-js/var.js";
4
3
  import { app } from "../app.js";
5
4
  import ClassNameMixin from "./ClassNameMixin.js";
6
5
  import FlyoutToggleMixin from "./FlyoutToggleMixin.js";
7
6
 
8
7
  const FlyoutMixinSuper = ClassNameMixin.prototype;
9
- const varname = '__flyout' + randomId();
8
+ const valueMap = new WeakMap();
10
9
  var flyoutMixinCounter = 0;
11
10
 
12
11
  export default function FlyoutMixin() {
@@ -49,7 +48,9 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
49
48
  });
50
49
  },
51
50
  open: function (value) {
52
- return openFlyout(this.elements()[0], kv(varname, value));
51
+ var element = this.elements()[0];
52
+ valueMap.set(element, value);
53
+ return openFlyout(element);
53
54
  },
54
55
  close: function (value) {
55
56
  return closeFlyout(this.elements()[0], value);
@@ -58,7 +59,7 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
58
59
  var element = this.elements()[0];
59
60
  return this.onToggleState(function (opened) {
60
61
  if (opened) {
61
- return callback(getVar(element, varname));
62
+ return callback(valueMap.get(element));
62
63
  }
63
64
  });
64
65
  },
@@ -73,7 +74,6 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
73
74
  FlyoutMixinSuper.initElement.call(self, element, state);
74
75
  if (!element.id) {
75
76
  element.id = 'flyout-' + (++flyoutMixinCounter);
76
- declareVar(element, varname, undefined);
77
77
  }
78
78
  app.on(element, {
79
79
  animationstart: function () {
@@ -1,4 +1,4 @@
1
- import { createPrivateStore, definePrototype, each, inherit, randomId, values } from "../include/zeta-dom/util.js";
1
+ import { createPrivateStore, definePrototype, each, extend, inherit, randomId, setAdd, values } from "../include/zeta-dom/util.js";
2
2
  import Mixin from "./Mixin.js";
3
3
 
4
4
  const _ = createPrivateStore();
@@ -16,6 +16,7 @@ definePrototype(MixinRefImpl, {
16
16
  export default function StatefulMixin() {
17
17
  Mixin.call(this);
18
18
  _(this, {
19
+ elements: new WeakSet(),
19
20
  states: {},
20
21
  prefix: '',
21
22
  counter: 0
@@ -46,8 +47,8 @@ definePrototype(StatefulMixin, Mixin, {
46
47
  const self = this;
47
48
  const state = self.state;
48
49
  return function (current) {
49
- if (current && current !== state.element) {
50
- state.element = current;
50
+ state.element = current;
51
+ if (current && setAdd(_(self).elements, current)) {
51
52
  self.initElement(current, state);
52
53
  }
53
54
  };
@@ -67,11 +68,10 @@ definePrototype(StatefulMixin, Mixin, {
67
68
  clone: function () {
68
69
  const self = this;
69
70
  const clone = inherit(Object.getPrototypeOf(self), self);
70
- _(clone, {
71
- states: _(self).states,
71
+ _(clone, extend({}, _(self), {
72
72
  prefix: randomId() + '.',
73
73
  counter: 0
74
- });
74
+ }));
75
75
  return clone;
76
76
  },
77
77
  dispose: function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brew-js-react",
3
- "version": "0.2.4",
3
+ "version": "0.2.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -17,7 +17,7 @@
17
17
  "homepage": "https://hackmd.io/@misonou/brew-js-react",
18
18
  "repository": "github:misonou/brew-js-react",
19
19
  "dependencies": {
20
- "brew-js": ">=0.3.2",
20
+ "brew-js": ">=0.3.11",
21
21
  "waterpipe": "^2.5.0",
22
22
  "zeta-dom": ">=0.2.3",
23
23
  "zeta-dom-react": ">=0.2.1"