@graffy/react 0.15.25-alpha.5 → 0.15.25-alpha.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/index.cjs +102 -0
- package/package.json +7 -2
package/index.cjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const React = require("react");
|
|
4
|
+
const PropTypes = require("prop-types");
|
|
5
|
+
const isEqual = require("lodash/isEqual.js");
|
|
6
|
+
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
|
|
7
|
+
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
|
|
8
|
+
const PropTypes__default = /* @__PURE__ */ _interopDefaultLegacy(PropTypes);
|
|
9
|
+
const isEqual__default = /* @__PURE__ */ _interopDefaultLegacy(isEqual);
|
|
10
|
+
const { createContext } = React__default.default;
|
|
11
|
+
const GraffyContext = createContext(null);
|
|
12
|
+
function GraffyProvider({ store, children }) {
|
|
13
|
+
return /* @__PURE__ */ React__default.default.createElement(GraffyContext.Provider, {
|
|
14
|
+
value: store
|
|
15
|
+
}, children);
|
|
16
|
+
}
|
|
17
|
+
GraffyProvider.propTypes = {
|
|
18
|
+
store: PropTypes__default.default.object.isRequired,
|
|
19
|
+
children: PropTypes__default.default.node
|
|
20
|
+
};
|
|
21
|
+
const { useRef, useState, useEffect, useContext: useContext$1 } = React__default.default;
|
|
22
|
+
const consumeSubscription = async (subscription, setState) => {
|
|
23
|
+
try {
|
|
24
|
+
for await (const data of subscription) {
|
|
25
|
+
if (subscription.closed) {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
setState((prevState) => ({
|
|
29
|
+
...prevState,
|
|
30
|
+
loading: false,
|
|
31
|
+
data,
|
|
32
|
+
error: null
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
} catch (error) {
|
|
36
|
+
setState((prevState) => ({
|
|
37
|
+
...prevState,
|
|
38
|
+
loading: false,
|
|
39
|
+
data: null,
|
|
40
|
+
error
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const retrieveResult = async (promise, setState) => {
|
|
45
|
+
try {
|
|
46
|
+
const data = await promise;
|
|
47
|
+
setState((prevState) => ({
|
|
48
|
+
...prevState,
|
|
49
|
+
loading: false,
|
|
50
|
+
data,
|
|
51
|
+
error: null
|
|
52
|
+
}));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
setState((prevState) => ({
|
|
55
|
+
...prevState,
|
|
56
|
+
loading: false,
|
|
57
|
+
data: null,
|
|
58
|
+
error
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
function useQuery(query, { once = false, ...other } = {}) {
|
|
63
|
+
const store = useContext$1(GraffyContext);
|
|
64
|
+
const queryRef = useRef(null);
|
|
65
|
+
if (!store)
|
|
66
|
+
throw Error("graffy_react.no_store_in_context");
|
|
67
|
+
const queryHasChanged = !isEqual__default.default(queryRef.current, query);
|
|
68
|
+
if (queryHasChanged) {
|
|
69
|
+
queryRef.current = query;
|
|
70
|
+
}
|
|
71
|
+
const fetchData = (options = other) => {
|
|
72
|
+
if (state.loading !== true)
|
|
73
|
+
setState({ ...state, loading: true });
|
|
74
|
+
if (once) {
|
|
75
|
+
retrieveResult(store.read(query, options), setState);
|
|
76
|
+
} else {
|
|
77
|
+
const subscription = store.watch(query, options);
|
|
78
|
+
consumeSubscription(subscription, setState);
|
|
79
|
+
return () => {
|
|
80
|
+
subscription.closed = true;
|
|
81
|
+
subscription.return();
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const refetch = fetchData.bind(null, { ...other, skipCache: true });
|
|
86
|
+
const [state, setState] = useState({ loading: true });
|
|
87
|
+
useEffect(fetchData, [queryRef.current, store]);
|
|
88
|
+
return once ? { ...state, refetch } : state;
|
|
89
|
+
}
|
|
90
|
+
function Query({ query, options, children }) {
|
|
91
|
+
const { data, loading, error } = useQuery(query, options);
|
|
92
|
+
return children({ data, loading, error });
|
|
93
|
+
}
|
|
94
|
+
const { useContext } = React__default.default;
|
|
95
|
+
function useStore() {
|
|
96
|
+
const store = useContext(GraffyContext);
|
|
97
|
+
return store;
|
|
98
|
+
}
|
|
99
|
+
exports.GraffyProvider = GraffyProvider;
|
|
100
|
+
exports.Query = Query;
|
|
101
|
+
exports.useQuery = useQuery;
|
|
102
|
+
exports.useStore = useStore;
|
package/package.json
CHANGED
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
"name": "@graffy/react",
|
|
3
3
|
"description": "This built-in module provides an idiomatic React API to a Graffy store.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.15.25-alpha.
|
|
6
|
-
"main": "./index.
|
|
5
|
+
"version": "0.15.25-alpha.7",
|
|
6
|
+
"main": "./index.cjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./index.mjs",
|
|
9
|
+
"require": "./index.cjs"
|
|
10
|
+
},
|
|
11
|
+
"module": "./index.mjs",
|
|
7
12
|
"types": "./types/index.d.ts",
|
|
8
13
|
"repository": {
|
|
9
14
|
"type": "git",
|