@depository/react 0.54.0 → 0.58.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@depository/react",
4
- "version": "0.54.0",
4
+ "version": "0.58.0",
5
5
  "description": "React bindings for depository",
6
6
  "module": "./src/index.js",
7
7
  "exports": {
@@ -27,16 +27,15 @@
27
27
  "access": "public"
28
28
  },
29
29
  "devDependencies": {
30
- "@depository/function-middleware": "^0.54.0",
31
- "@depository/store": "^0.54.0",
30
+ "@depository/store": "^0.58.0",
32
31
  "htm": "^3.0.4",
33
32
  "jsdom": "^16.4.0",
34
33
  "mocha": "8.3.2",
35
34
  "mocha-dominate": "^2.0.0",
36
35
  "react": "^17.0.2",
37
36
  "react-dom": "^17.0.2",
38
- "serve": "^11.3.2",
39
- "unexpected": "12.0.0",
37
+ "serve": "^12.0.1",
38
+ "unexpected": "12.0.3",
40
39
  "unexpected-dom": "^4.18.0",
41
40
  "unexpected-reaction": "^3.0.0"
42
41
  },
@@ -48,5 +47,5 @@
48
47
  "engines": {
49
48
  "node": ">=12"
50
49
  },
51
- "gitHead": "ea8c27f90f70f8307139c669aee402299efce95d"
50
+ "gitHead": "0f1070ade2785e4c20b10e58cfba56fa12106733"
52
51
  }
@@ -1,18 +1,19 @@
1
1
  export const createBinding = ({ createElement, Component, StoreContext }) => {
2
- const connect = (functionbindings, ChildComponent) => {
2
+ const connect = (bindingOrFunction, ChildComponent) => {
3
3
  if (!ChildComponent) {
4
- ChildComponent = functionbindings;
5
- functionbindings = null;
4
+ ChildComponent = bindingOrFunction;
5
+ bindingOrFunction = null;
6
6
  }
7
7
 
8
+ const isFunctionBindings = typeof bindingOrFunction === "function";
9
+
8
10
  class Connected extends Component {
9
11
  constructor(props) {
10
12
  super(props);
11
13
 
12
- const bindings =
13
- typeof functionbindings === "function"
14
- ? functionbindings(props)
15
- : functionbindings;
14
+ const bindings = isFunctionBindings
15
+ ? bindingOrFunction(props)
16
+ : bindingOrFunction;
16
17
 
17
18
  const store = props.store;
18
19
 
@@ -20,21 +21,38 @@ export const createBinding = ({ createElement, Component, StoreContext }) => {
20
21
  return store.dispatch(action);
21
22
  };
22
23
 
23
- this.observer = bindings && store.observe(bindings);
24
+ this._observable = bindings && store.observe(bindings);
24
25
 
25
- this.state = this.observer ? this.observer.value : {};
26
+ this.state = this._observable ? this._observable.value : {};
26
27
  }
27
28
 
28
- componentDidMount() {
29
- if (this.observer) {
30
- this.subscription = this.observer.subscribe((value) => {
31
- this.setState(value);
29
+ _subscribe() {
30
+ this._subscription =
31
+ this._observable &&
32
+ this._observable.subscribe((data) => {
33
+ this.setState(data);
32
34
  });
33
- }
35
+ }
36
+
37
+ componentDidMount() {
38
+ this._subscribe();
34
39
  }
35
40
 
36
41
  componentWillUnmount() {
37
- this.subscription && this.subscription.unsubscribe();
42
+ this._subscription && this._subscription.unsubscribe();
43
+ }
44
+
45
+ componentDidUpdate() {
46
+ if (this._observable && isFunctionBindings) {
47
+ const bindings = bindingOrFunction(this.props);
48
+ const _observable = this.props.store.observe(bindings);
49
+ if (this._observable !== _observable) {
50
+ this._subscription.unsubscribe();
51
+ this._observable = _observable;
52
+ this._subscribe();
53
+ this.setState(this._observable.value);
54
+ }
55
+ }
38
56
  }
39
57
 
40
58
  render() {
package/src/index.spec.js CHANGED
@@ -2,7 +2,6 @@ import unexpected from "unexpected";
2
2
  import unexpectedDom from "unexpected-dom";
3
3
  import unexpectedReaction from "unexpected-reaction";
4
4
  import { Store } from "@depository/store";
5
- import { functionMiddleware } from "@depository/function-middleware";
6
5
  import { StoreProvider, connect } from "./index.js";
7
6
  import { html } from "./html.js";
8
7
 
@@ -47,28 +46,36 @@ const Calculator = connect(
47
46
  }
48
47
  );
49
48
 
49
+ const Quote = connect(
50
+ ({ id }) => ({ quote: `quote.${id}` }),
51
+ ({ quote }) => quote
52
+ );
53
+
54
+ const Quotes = connect(
55
+ { quoteId: "selectedQuote" },
56
+ ({ quoteId }) => html`<${Quote} id=${quoteId} />`
57
+ );
58
+
50
59
  describe("react", () => {
51
60
  let store;
52
61
  let component;
53
62
 
54
- beforeEach(() => {
55
- store = new Store({
56
- a: 10,
57
- b: 10,
58
- });
59
-
60
- store.useMiddleware(functionMiddleware());
63
+ const renderCalculator = () => {
64
+ store = new Store({ a: 10, b: 10 });
61
65
 
62
66
  component = mount(
63
67
  html`<${StoreProvider} value=${store}><${Calculator} /><//>`
64
68
  );
65
- });
69
+ };
66
70
 
67
71
  it("handles rendering paths and computeds", () => {
72
+ renderCalculator();
68
73
  expect(component, "queried for test id", "sum", "to have text", "10+10=20");
69
74
  });
70
75
 
71
76
  it("handles dispatching actions", async () => {
77
+ renderCalculator();
78
+
72
79
  simulate(component, {
73
80
  type: "click",
74
81
  target: "[data-test-id=increment]",
@@ -78,4 +85,34 @@ describe("react", () => {
78
85
 
79
86
  expect(component, "queried for test id", "sum", "to have text", "11+11=22");
80
87
  });
88
+
89
+ const renderQuotes = () => {
90
+ store = new Store({
91
+ selectedQuote: 0,
92
+ quote: {
93
+ 0: "The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela",
94
+ 1: "The way to get started is to quit talking and begin doing. -Walt Disney",
95
+ },
96
+ });
97
+
98
+ component = mount(html`<${StoreProvider} value=${store}><${Quotes} /><//>`);
99
+ };
100
+
101
+ it("handles function bindings with changing props", async () => {
102
+ renderQuotes();
103
+
104
+ expect(
105
+ component,
106
+ "to satisfy",
107
+ "The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela"
108
+ );
109
+
110
+ await store.dispatch({ payload: { selectedQuote: 1 } });
111
+
112
+ expect(
113
+ component,
114
+ "to satisfy",
115
+ "The way to get started is to quit talking and begin doing. -Walt Disney"
116
+ );
117
+ });
81
118
  });
@@ -2,7 +2,6 @@ import unexpected from "unexpected";
2
2
  import unexpectedDom from "unexpected-dom";
3
3
  import unexpectedReaction from "unexpected-reaction";
4
4
  import { Store } from "@depository/store";
5
- import { functionMiddleware } from "@depository/function-middleware";
6
5
  import { StoreProvider, useData, useDispatch } from "./index.js";
7
6
  import { html } from "./html.js";
8
7
 
@@ -56,8 +55,6 @@ describe("useData", () => {
56
55
  b: 10,
57
56
  });
58
57
 
59
- store.useMiddleware(functionMiddleware());
60
-
61
58
  component = mount(
62
59
  html`<${StoreProvider} value=${store}><${Calculator} /><//>`
63
60
  );