@depository/preact 0.57.0 → 0.58.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +3 -4
- package/src/createBinding.js +33 -15
- package/src/hooks/useData.spec.js +0 -3
- package/src/index.spec.js +47 -8
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"type": "module",
|
3
3
|
"name": "@depository/preact",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.58.0",
|
5
5
|
"description": "Preact bindings for depository",
|
6
6
|
"module": "./src/index.js",
|
7
7
|
"exports": {
|
@@ -27,8 +27,7 @@
|
|
27
27
|
"access": "public"
|
28
28
|
},
|
29
29
|
"devDependencies": {
|
30
|
-
"@depository/
|
31
|
-
"@depository/store": "^0.57.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",
|
@@ -44,5 +43,5 @@
|
|
44
43
|
"engines": {
|
45
44
|
"node": ">=12"
|
46
45
|
},
|
47
|
-
"gitHead": "
|
46
|
+
"gitHead": "0f1070ade2785e4c20b10e58cfba56fa12106733"
|
48
47
|
}
|
package/src/createBinding.js
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
export const createBinding = ({ h, Component, StoreContext }) => {
|
2
|
-
const connect = (
|
2
|
+
const connect = (bindingOrFunction, ChildComponent) => {
|
3
3
|
if (!ChildComponent) {
|
4
|
-
ChildComponent =
|
5
|
-
|
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
|
-
|
14
|
-
|
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 = ({ h, Component, StoreContext }) => {
|
|
20
21
|
return store.dispatch(action);
|
21
22
|
};
|
22
23
|
|
23
|
-
this.
|
24
|
+
this._observable = bindings && store.observe(bindings);
|
24
25
|
|
25
|
-
this.state = this.
|
26
|
+
this.state = this._observable ? this._observable.value : {};
|
26
27
|
}
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
this.
|
31
|
-
|
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.
|
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({ children, store, ...other }, state) {
|
@@ -1,7 +1,6 @@
|
|
1
1
|
import unexpected from "unexpected";
|
2
2
|
import unexpectedDom from "unexpected-dom";
|
3
3
|
import { Store } from "@depository/store";
|
4
|
-
import { functionMiddleware } from "@depository/function-middleware";
|
5
4
|
import { StoreProvider, useData, useDispatch } from "./index.js";
|
6
5
|
import { h, render } from "preact";
|
7
6
|
import htm from "htm";
|
@@ -57,8 +56,6 @@ describe("useData", () => {
|
|
57
56
|
b: 10,
|
58
57
|
});
|
59
58
|
|
60
|
-
store.useMiddleware(functionMiddleware());
|
61
|
-
|
62
59
|
render(
|
63
60
|
html`<${StoreProvider} value=${store}><${Calculator} /><//>`,
|
64
61
|
container
|
package/src/index.spec.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import unexpected from "unexpected";
|
2
2
|
import unexpectedDom from "unexpected-dom";
|
3
3
|
import { Store } from "@depository/store";
|
4
|
-
import { functionMiddleware } from "@depository/function-middleware";
|
5
4
|
import { StoreProvider, connect } from "./index.js";
|
6
5
|
import { h, render } from "preact";
|
7
6
|
import htm from "htm";
|
@@ -46,30 +45,40 @@ const Calculator = connect(
|
|
46
45
|
}
|
47
46
|
);
|
48
47
|
|
48
|
+
const Quote = connect(
|
49
|
+
({ id }) => ({ quote: `quote.${id}` }),
|
50
|
+
({ quote }) => quote
|
51
|
+
);
|
52
|
+
|
53
|
+
const Quotes = connect(
|
54
|
+
{ quoteId: "selectedQuote" },
|
55
|
+
({ quoteId }) => html`<${Quote} id=${quoteId} />`
|
56
|
+
);
|
57
|
+
|
49
58
|
describe("preact", () => {
|
50
59
|
let container, store;
|
51
60
|
|
52
61
|
beforeEach(() => {
|
53
62
|
container = document.createElement("div");
|
63
|
+
});
|
54
64
|
|
55
|
-
|
56
|
-
|
57
|
-
b: 10,
|
58
|
-
});
|
59
|
-
|
60
|
-
store.useMiddleware(functionMiddleware());
|
65
|
+
const renderCalculator = () => {
|
66
|
+
store = new Store({ a: 10, b: 10 });
|
61
67
|
|
62
68
|
render(
|
63
69
|
html`<${StoreProvider} value=${store}><${Calculator} /><//>`,
|
64
70
|
container
|
65
71
|
);
|
66
|
-
}
|
72
|
+
};
|
67
73
|
|
68
74
|
it("handles rendering paths and computeds", () => {
|
75
|
+
renderCalculator();
|
69
76
|
expect(container, "queried for test id", "sum", "to have text", "10+10=20");
|
70
77
|
});
|
71
78
|
|
72
79
|
it("handles dispatching actions", async () => {
|
80
|
+
renderCalculator();
|
81
|
+
|
73
82
|
const incrementButton = container.querySelector("[data-test-id=increment]");
|
74
83
|
incrementButton.dispatchEvent(new CustomEvent("click"));
|
75
84
|
|
@@ -77,4 +86,34 @@ describe("preact", () => {
|
|
77
86
|
|
78
87
|
expect(container, "queried for test id", "sum", "to have text", "11+11=22");
|
79
88
|
});
|
89
|
+
|
90
|
+
const renderQuotes = () => {
|
91
|
+
store = new Store({
|
92
|
+
selectedQuote: 0,
|
93
|
+
quote: {
|
94
|
+
0: "The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela",
|
95
|
+
1: "The way to get started is to quit talking and begin doing. -Walt Disney",
|
96
|
+
},
|
97
|
+
});
|
98
|
+
|
99
|
+
render(html`<${StoreProvider} value=${store}><${Quotes} /><//>`, container);
|
100
|
+
};
|
101
|
+
|
102
|
+
it("handles function bindings with changing props", async () => {
|
103
|
+
renderQuotes();
|
104
|
+
|
105
|
+
expect(
|
106
|
+
container,
|
107
|
+
"to have text",
|
108
|
+
"The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela"
|
109
|
+
);
|
110
|
+
|
111
|
+
await store.dispatch({ payload: { selectedQuote: 1 } });
|
112
|
+
|
113
|
+
expect(
|
114
|
+
container,
|
115
|
+
"to have text",
|
116
|
+
"The way to get started is to quit talking and begin doing. -Walt Disney"
|
117
|
+
);
|
118
|
+
});
|
80
119
|
});
|