@graffy/react 0.15.25-alpha.1 → 0.15.25-alpha.2
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 +30 -59
- package/index.mjs +21 -42
- package/package.json +1 -1
- package/types/useQuery.d.ts +17 -3
package/index.cjs
CHANGED
|
@@ -1,72 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
__defNormalProp(a, prop, b[prop]);
|
|
13
|
-
if (__getOwnPropSymbols)
|
|
14
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
-
if (__propIsEnum.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
}
|
|
18
|
-
return a;
|
|
19
|
-
};
|
|
20
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
-
var __objRest = (source, exclude) => {
|
|
22
|
-
var target = {};
|
|
23
|
-
for (var prop in source)
|
|
24
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
25
|
-
target[prop] = source[prop];
|
|
26
|
-
if (source != null && __getOwnPropSymbols)
|
|
27
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
28
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
}
|
|
31
|
-
return target;
|
|
32
|
-
};
|
|
33
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
-
exports[Symbol.toStringTag] = "Module";
|
|
35
|
-
var React = require("react");
|
|
36
|
-
var PropTypes = require("prop-types");
|
|
37
|
-
var isEqual = require("lodash/isEqual.js");
|
|
38
|
-
function _interopDefaultLegacy(e) {
|
|
39
|
-
return e && typeof e === "object" && "default" in e ? e : { "default": e };
|
|
40
|
-
}
|
|
41
|
-
var React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
|
|
42
|
-
var PropTypes__default = /* @__PURE__ */ _interopDefaultLegacy(PropTypes);
|
|
43
|
-
var isEqual__default = /* @__PURE__ */ _interopDefaultLegacy(isEqual);
|
|
44
|
-
const { createContext } = React__default["default"];
|
|
45
|
-
const GraffyContext = createContext();
|
|
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);
|
|
46
12
|
function GraffyProvider({ store, children }) {
|
|
47
|
-
return /* @__PURE__ */ React__default
|
|
13
|
+
return /* @__PURE__ */ React__default.default.createElement(GraffyContext.Provider, {
|
|
48
14
|
value: store
|
|
49
15
|
}, children);
|
|
50
16
|
}
|
|
51
17
|
GraffyProvider.propTypes = {
|
|
52
|
-
store: PropTypes__default
|
|
53
|
-
children: PropTypes__default
|
|
18
|
+
store: PropTypes__default.default.object.isRequired,
|
|
19
|
+
children: PropTypes__default.default.node
|
|
54
20
|
};
|
|
55
|
-
const { useRef, useState, useEffect, useContext: useContext$1 } = React__default
|
|
21
|
+
const { useRef, useState, useEffect, useContext: useContext$1 } = React__default.default;
|
|
56
22
|
const consumeSubscription = async (subscription, setState) => {
|
|
57
23
|
try {
|
|
58
24
|
for await (const data of subscription) {
|
|
59
25
|
if (subscription.closed) {
|
|
60
26
|
break;
|
|
61
27
|
}
|
|
62
|
-
setState((prevState) =>
|
|
28
|
+
setState((prevState) => ({
|
|
29
|
+
...prevState,
|
|
63
30
|
loading: false,
|
|
64
31
|
data,
|
|
65
32
|
error: null
|
|
66
33
|
}));
|
|
67
34
|
}
|
|
68
35
|
} catch (error) {
|
|
69
|
-
setState((prevState) =>
|
|
36
|
+
setState((prevState) => ({
|
|
37
|
+
...prevState,
|
|
70
38
|
loading: false,
|
|
71
39
|
data: null,
|
|
72
40
|
error
|
|
@@ -76,30 +44,33 @@ const consumeSubscription = async (subscription, setState) => {
|
|
|
76
44
|
const retrieveResult = async (promise, setState) => {
|
|
77
45
|
try {
|
|
78
46
|
const data = await promise;
|
|
79
|
-
setState((prevState) =>
|
|
47
|
+
setState((prevState) => ({
|
|
48
|
+
...prevState,
|
|
80
49
|
loading: false,
|
|
81
50
|
data,
|
|
82
51
|
error: null
|
|
83
52
|
}));
|
|
84
53
|
} catch (error) {
|
|
85
|
-
setState((prevState) =>
|
|
54
|
+
setState((prevState) => ({
|
|
55
|
+
...prevState,
|
|
86
56
|
loading: false,
|
|
87
57
|
data: null,
|
|
88
58
|
error
|
|
89
59
|
}));
|
|
90
60
|
}
|
|
91
61
|
};
|
|
92
|
-
function useQuery(query,
|
|
93
|
-
var _b = _a, { once } = _b, other = __objRest(_b, ["once"]);
|
|
62
|
+
function useQuery(query, { once = false, ...other } = {}) {
|
|
94
63
|
const store = useContext$1(GraffyContext);
|
|
95
64
|
const queryRef = useRef(null);
|
|
96
|
-
|
|
65
|
+
if (!store)
|
|
66
|
+
throw Error("graffy_react.no_store_in_context");
|
|
67
|
+
const queryHasChanged = !isEqual__default.default(queryRef.current, query);
|
|
97
68
|
if (queryHasChanged) {
|
|
98
69
|
queryRef.current = query;
|
|
99
70
|
}
|
|
100
71
|
const fetchData = (options = other) => {
|
|
101
72
|
if (state.loading !== true)
|
|
102
|
-
setState(
|
|
73
|
+
setState({ ...state, loading: true });
|
|
103
74
|
if (once) {
|
|
104
75
|
retrieveResult(store.read(query, options), setState);
|
|
105
76
|
} else {
|
|
@@ -111,16 +82,16 @@ function useQuery(query, _a = {}) {
|
|
|
111
82
|
};
|
|
112
83
|
}
|
|
113
84
|
};
|
|
114
|
-
const refetch = fetchData.bind(null,
|
|
85
|
+
const refetch = fetchData.bind(null, { ...other, skipCache: true });
|
|
115
86
|
const [state, setState] = useState({ loading: true });
|
|
116
87
|
useEffect(fetchData, [queryRef.current, store]);
|
|
117
|
-
return once ?
|
|
88
|
+
return once ? { ...state, refetch } : state;
|
|
118
89
|
}
|
|
119
90
|
function Query({ query, options, children }) {
|
|
120
91
|
const { data, loading, error } = useQuery(query, options);
|
|
121
92
|
return children({ data, loading, error });
|
|
122
93
|
}
|
|
123
|
-
const { useContext } = React__default
|
|
94
|
+
const { useContext } = React__default.default;
|
|
124
95
|
function useStore() {
|
|
125
96
|
const store = useContext(GraffyContext);
|
|
126
97
|
return store;
|
package/index.mjs
CHANGED
|
@@ -1,39 +1,8 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __objRest = (source, exclude) => {
|
|
21
|
-
var target = {};
|
|
22
|
-
for (var prop in source)
|
|
23
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
-
target[prop] = source[prop];
|
|
25
|
-
if (source != null && __getOwnPropSymbols)
|
|
26
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
}
|
|
30
|
-
return target;
|
|
31
|
-
};
|
|
32
1
|
import React from "react";
|
|
33
2
|
import PropTypes from "prop-types";
|
|
34
3
|
import isEqual from "lodash/isEqual.js";
|
|
35
4
|
const { createContext } = React;
|
|
36
|
-
const GraffyContext = createContext();
|
|
5
|
+
const GraffyContext = createContext(null);
|
|
37
6
|
function GraffyProvider({ store, children }) {
|
|
38
7
|
return /* @__PURE__ */ React.createElement(GraffyContext.Provider, {
|
|
39
8
|
value: store
|
|
@@ -50,14 +19,16 @@ const consumeSubscription = async (subscription, setState) => {
|
|
|
50
19
|
if (subscription.closed) {
|
|
51
20
|
break;
|
|
52
21
|
}
|
|
53
|
-
setState((prevState) =>
|
|
22
|
+
setState((prevState) => ({
|
|
23
|
+
...prevState,
|
|
54
24
|
loading: false,
|
|
55
25
|
data,
|
|
56
26
|
error: null
|
|
57
27
|
}));
|
|
58
28
|
}
|
|
59
29
|
} catch (error) {
|
|
60
|
-
setState((prevState) =>
|
|
30
|
+
setState((prevState) => ({
|
|
31
|
+
...prevState,
|
|
61
32
|
loading: false,
|
|
62
33
|
data: null,
|
|
63
34
|
error
|
|
@@ -67,30 +38,33 @@ const consumeSubscription = async (subscription, setState) => {
|
|
|
67
38
|
const retrieveResult = async (promise, setState) => {
|
|
68
39
|
try {
|
|
69
40
|
const data = await promise;
|
|
70
|
-
setState((prevState) =>
|
|
41
|
+
setState((prevState) => ({
|
|
42
|
+
...prevState,
|
|
71
43
|
loading: false,
|
|
72
44
|
data,
|
|
73
45
|
error: null
|
|
74
46
|
}));
|
|
75
47
|
} catch (error) {
|
|
76
|
-
setState((prevState) =>
|
|
48
|
+
setState((prevState) => ({
|
|
49
|
+
...prevState,
|
|
77
50
|
loading: false,
|
|
78
51
|
data: null,
|
|
79
52
|
error
|
|
80
53
|
}));
|
|
81
54
|
}
|
|
82
55
|
};
|
|
83
|
-
function useQuery(query,
|
|
84
|
-
var _b = _a, { once } = _b, other = __objRest(_b, ["once"]);
|
|
56
|
+
function useQuery(query, { once = false, ...other } = {}) {
|
|
85
57
|
const store = useContext$1(GraffyContext);
|
|
86
58
|
const queryRef = useRef(null);
|
|
59
|
+
if (!store)
|
|
60
|
+
throw Error("graffy_react.no_store_in_context");
|
|
87
61
|
const queryHasChanged = !isEqual(queryRef.current, query);
|
|
88
62
|
if (queryHasChanged) {
|
|
89
63
|
queryRef.current = query;
|
|
90
64
|
}
|
|
91
65
|
const fetchData = (options = other) => {
|
|
92
66
|
if (state.loading !== true)
|
|
93
|
-
setState(
|
|
67
|
+
setState({ ...state, loading: true });
|
|
94
68
|
if (once) {
|
|
95
69
|
retrieveResult(store.read(query, options), setState);
|
|
96
70
|
} else {
|
|
@@ -102,10 +76,10 @@ function useQuery(query, _a = {}) {
|
|
|
102
76
|
};
|
|
103
77
|
}
|
|
104
78
|
};
|
|
105
|
-
const refetch = fetchData.bind(null,
|
|
79
|
+
const refetch = fetchData.bind(null, { ...other, skipCache: true });
|
|
106
80
|
const [state, setState] = useState({ loading: true });
|
|
107
81
|
useEffect(fetchData, [queryRef.current, store]);
|
|
108
|
-
return once ?
|
|
82
|
+
return once ? { ...state, refetch } : state;
|
|
109
83
|
}
|
|
110
84
|
function Query({ query, options, children }) {
|
|
111
85
|
const { data, loading, error } = useQuery(query, options);
|
|
@@ -116,4 +90,9 @@ function useStore() {
|
|
|
116
90
|
const store = useContext(GraffyContext);
|
|
117
91
|
return store;
|
|
118
92
|
}
|
|
119
|
-
export {
|
|
93
|
+
export {
|
|
94
|
+
GraffyProvider,
|
|
95
|
+
Query,
|
|
96
|
+
useQuery,
|
|
97
|
+
useStore
|
|
98
|
+
};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
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.
|
|
5
|
+
"version": "0.15.25-alpha.2",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
package/types/useQuery.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param {any} query
|
|
4
|
+
* @param {any} options
|
|
5
|
+
* @returns {{
|
|
6
|
+
* data?: any,
|
|
7
|
+
* loading: boolean,
|
|
8
|
+
* error?: any,
|
|
9
|
+
* refetch?: () => void
|
|
10
|
+
* }}
|
|
11
|
+
*/
|
|
12
|
+
export default function useQuery(query: any, { once, ...other }?: any): {
|
|
13
|
+
data?: any;
|
|
14
|
+
loading: boolean;
|
|
15
|
+
error?: any;
|
|
16
|
+
refetch?: () => void;
|
|
17
|
+
};
|