@based/react 0.6.1 → 0.7.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/build/index.js +32 -0
- package/dist/bundle.js +1 -0
- package/dist/clients.d.ts +30 -0
- package/dist/clients.js +139 -0
- package/dist/clients.js.map +1 -0
- package/dist/genOptsId.d.ts +4 -0
- package/dist/genOptsId.js +14 -0
- package/dist/genOptsId.js.map +1 -0
- package/dist/gql.d.ts +9 -0
- package/dist/gql.js +158 -0
- package/dist/gql.js.map +1 -0
- package/dist/index.d.ts +5 -39
- package/dist/index.js +25 -229
- package/dist/index.js.map +1 -1
- package/dist/meta.d.ts +8 -0
- package/dist/meta.js +70 -0
- package/dist/meta.js.map +1 -0
- package/dist/reducer.d.ts +18 -0
- package/dist/reducer.js +35 -0
- package/dist/reducer.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +9 -5
- package/src/clients.ts +160 -0
- package/src/genOptsId.ts +13 -0
- package/src/gql.ts +210 -0
- package/src/index.ts +12 -266
- package/src/meta.ts +74 -0
- package/src/reducer.ts +42 -0
- package/src/types.ts +5 -0
- package/tsconfig.tsbuildinfo +0 -1
package/dist/index.js
CHANGED
|
@@ -6,173 +6,25 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
11
|
};
|
|
21
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.
|
|
23
|
-
const react_1 =
|
|
24
|
-
const client_1 =
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return `${opts.env}_${opts.project}_${opts.org}_${opts.cluster || ''}_${opts.name || ''}`;
|
|
33
|
-
};
|
|
34
|
-
exports.BasedContext = (0, react_1.createContext)({
|
|
35
|
-
clients: {},
|
|
36
|
-
removeClient: (...args) => { },
|
|
37
|
-
});
|
|
38
|
-
const errors = {};
|
|
39
|
-
const errorListeners = new Set();
|
|
40
|
-
const loadings = new Set();
|
|
41
|
-
const loadingListeners = new Set();
|
|
42
|
-
const newClientListeners = new Set();
|
|
43
|
-
let isLoading = false;
|
|
44
|
-
let lastError = '';
|
|
45
|
-
let errorCnt = 0;
|
|
46
|
-
let errorKey = errorCnt + lastError;
|
|
47
|
-
const defaultCreateClient = (selector) => {
|
|
48
|
-
if (typeof selector === 'object') {
|
|
49
|
-
if (process.env.CLUSTER &&
|
|
50
|
-
process.env.CLUSTER.startsWith('local') &&
|
|
51
|
-
!selector.cluster) {
|
|
52
|
-
selector.cluster = process.env.CLUSTER;
|
|
53
|
-
}
|
|
54
|
-
return (0, client_1.default)(selector);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
// default
|
|
58
|
-
console.error('Cannot create client from ' + selector);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
exports.defaultCreateClient = defaultCreateClient;
|
|
62
|
-
const Provider = ({ client, children, clients, createClient }) => {
|
|
63
|
-
if (!clients && client) {
|
|
64
|
-
clients = {
|
|
65
|
-
default: client,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
else if (clients && client) {
|
|
69
|
-
clients.default = client;
|
|
70
|
-
}
|
|
71
|
-
const ctx = react_1.default.createElement(exports.BasedContext.Provider, {
|
|
72
|
-
value: {
|
|
73
|
-
clients,
|
|
74
|
-
createClient: createClient || exports.defaultCreateClient,
|
|
75
|
-
removeClient: (selector) => {
|
|
76
|
-
if (selector instanceof client_1.Based) {
|
|
77
|
-
for (const cl in clients) {
|
|
78
|
-
if (clients[cl] === selector) {
|
|
79
|
-
selector = cl;
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (typeof selector !== 'string') {
|
|
84
|
-
console.error('Cannot find client to remove from ctx', selector);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
else if (typeof selector !== 'string') {
|
|
89
|
-
selector = genOptsId(selector);
|
|
90
|
-
}
|
|
91
|
-
// @ts-ignore
|
|
92
|
-
if (clients[selector]) {
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
clients[selector].disconnect();
|
|
95
|
-
// @ts-ignore
|
|
96
|
-
delete clients[selector];
|
|
97
|
-
newClientListeners.forEach((fn) => fn());
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
}, children);
|
|
102
|
-
return ctx;
|
|
103
|
-
};
|
|
104
|
-
exports.Provider = Provider;
|
|
105
|
-
const useBasedContext = () => {
|
|
106
|
-
return (0, react_1.useContext)(exports.BasedContext);
|
|
107
|
-
};
|
|
108
|
-
exports.useBasedContext = useBasedContext;
|
|
109
|
-
function forceUpdate(state) {
|
|
110
|
-
return state + 1;
|
|
111
|
-
}
|
|
112
|
-
const useClients = () => {
|
|
113
|
-
const ctx = (0, exports.useBasedContext)();
|
|
114
|
-
const [, update] = (0, react_1.useReducer)(forceUpdate, 0);
|
|
115
|
-
(0, react_1.useEffect)(() => {
|
|
116
|
-
let timer;
|
|
117
|
-
const fn = () => {
|
|
118
|
-
timer = setTimeout(update, 0);
|
|
119
|
-
};
|
|
120
|
-
newClientListeners.add(fn);
|
|
121
|
-
return () => {
|
|
122
|
-
newClientListeners.delete(fn);
|
|
123
|
-
clearTimeout(timer);
|
|
124
|
-
};
|
|
125
|
-
}, []);
|
|
126
|
-
return Object.values(ctx.clients);
|
|
127
|
-
};
|
|
128
|
-
exports.useClients = useClients;
|
|
129
|
-
const useClient = (selector = 'default') => {
|
|
130
|
-
const basedCtx = (0, react_1.useContext)(exports.BasedContext);
|
|
131
|
-
if (typeof selector === 'object') {
|
|
132
|
-
if (!(selector.env && selector.project && selector.org)) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
let key;
|
|
137
|
-
if (typeof selector === 'string') {
|
|
138
|
-
key = selector;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
key = selector.key || genOptsId(selector);
|
|
142
|
-
}
|
|
143
|
-
let client = basedCtx.clients[key];
|
|
144
|
-
if (!client && basedCtx.createClient) {
|
|
145
|
-
client = basedCtx.createClient(selector);
|
|
146
|
-
if (client) {
|
|
147
|
-
basedCtx.clients[key] = client;
|
|
148
|
-
newClientListeners.forEach((fn) => fn());
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return client;
|
|
152
|
-
};
|
|
153
|
-
exports.useClient = useClient;
|
|
154
|
-
function resultReducer(state, action) {
|
|
155
|
-
if (action.error) {
|
|
156
|
-
state.error = action.error;
|
|
157
|
-
}
|
|
158
|
-
if (action.data) {
|
|
159
|
-
state.checksum = action.checksum || 0;
|
|
160
|
-
state.data = action.data;
|
|
161
|
-
state.loading = false;
|
|
162
|
-
if (state.error) {
|
|
163
|
-
delete state.error;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (action.loading) {
|
|
167
|
-
state.loading = action.loading;
|
|
168
|
-
}
|
|
169
|
-
return { ...state };
|
|
170
|
-
}
|
|
13
|
+
exports.useData = exports.useTrack = exports.useSchema = exports.useAuth = void 0;
|
|
14
|
+
const react_1 = require("react");
|
|
15
|
+
const client_1 = require("@based/client");
|
|
16
|
+
const reducer_1 = require("./reducer");
|
|
17
|
+
const genOptsId_1 = require("./genOptsId");
|
|
18
|
+
const clients_1 = require("./clients");
|
|
19
|
+
const meta_1 = require("./meta");
|
|
20
|
+
__exportStar(require("./meta"), exports);
|
|
21
|
+
__exportStar(require("./clients"), exports);
|
|
22
|
+
__exportStar(require("./gql"), exports);
|
|
171
23
|
function useAuth(clientSelector) {
|
|
172
|
-
const client = (0,
|
|
24
|
+
const client = (0, clients_1.useClient)(clientSelector);
|
|
173
25
|
const [token, setToken] = (0, react_1.useState)(false);
|
|
174
26
|
(0, react_1.useEffect)(() => {
|
|
175
|
-
const t = (
|
|
27
|
+
const t = () => setToken(client.getToken());
|
|
176
28
|
client.on('auth', t);
|
|
177
29
|
return () => {
|
|
178
30
|
client.removeListener('auth', t);
|
|
@@ -197,7 +49,7 @@ exports.useSchema = useSchema;
|
|
|
197
49
|
function useTrack(type, params, clientSelector) {
|
|
198
50
|
if (type) {
|
|
199
51
|
const selector = clientSelector || 'default';
|
|
200
|
-
const client = (0,
|
|
52
|
+
const client = (0, clients_1.useClient)(selector);
|
|
201
53
|
const id = (0, react_1.useMemo)(() => {
|
|
202
54
|
return (0, client_1.generateSubscriptionId)(params, type);
|
|
203
55
|
}, [type, params]);
|
|
@@ -209,14 +61,14 @@ function useTrack(type, params, clientSelector) {
|
|
|
209
61
|
}, [id]);
|
|
210
62
|
}
|
|
211
63
|
else {
|
|
212
|
-
(0,
|
|
64
|
+
(0, clients_1.useClient)(clientSelector);
|
|
213
65
|
(0, react_1.useMemo)(() => { }, []);
|
|
214
66
|
(0, react_1.useEffect)(stubFn, [null, null]);
|
|
215
67
|
}
|
|
216
68
|
}
|
|
217
69
|
exports.useTrack = useTrack;
|
|
218
70
|
function useData(a, payload, clientSelector) {
|
|
219
|
-
const [result, dispatch] = (0, react_1.useReducer)(resultReducer, {
|
|
71
|
+
const [result, dispatch] = (0, react_1.useReducer)(reducer_1.resultReducer, {
|
|
220
72
|
loading: true,
|
|
221
73
|
data: {},
|
|
222
74
|
checksum: 0,
|
|
@@ -227,29 +79,29 @@ function useData(a, payload, clientSelector) {
|
|
|
227
79
|
}, [payload, a]);
|
|
228
80
|
const isNamed = typeof a === 'string';
|
|
229
81
|
const selector = clientSelector || (!isNamed ? payload : 'default');
|
|
230
|
-
const client = (0,
|
|
231
|
-
const clientKey = typeof selector === 'string' ? selector : genOptsId(selector);
|
|
82
|
+
const client = (0, clients_1.useClient)(selector);
|
|
83
|
+
const clientKey = typeof selector === 'string' ? selector : (0, genOptsId_1.genOptsId)(selector);
|
|
232
84
|
if (client) {
|
|
233
85
|
const subKey = clientKey + subId;
|
|
234
86
|
(0, react_1.useEffect)(() => {
|
|
235
|
-
updateMeta(subKey, true, false);
|
|
87
|
+
(0, meta_1.updateMeta)(subKey, true, false);
|
|
236
88
|
dispatch({ error: null, loading: true, data: {} });
|
|
237
89
|
const [, subscriberId] = (0, client_1.addSubscriber)(client.client, isNamed ? payload : a, (d, checksum) => {
|
|
238
|
-
updateMeta(subKey, false, false);
|
|
90
|
+
(0, meta_1.updateMeta)(subKey, false, false);
|
|
239
91
|
dispatch({ data: d, checksum });
|
|
240
92
|
}, (err) => {
|
|
241
93
|
if (err) {
|
|
242
94
|
console.error(err);
|
|
243
|
-
updateMeta(subKey, false, err);
|
|
95
|
+
(0, meta_1.updateMeta)(subKey, false, err);
|
|
244
96
|
dispatch({ error: err, loading: false });
|
|
245
97
|
}
|
|
246
98
|
}, (err) => {
|
|
247
99
|
console.error(err);
|
|
248
|
-
updateMeta(subKey, false, err);
|
|
100
|
+
(0, meta_1.updateMeta)(subKey, false, err);
|
|
249
101
|
dispatch({ error: err });
|
|
250
102
|
}, subId, typeof a === 'string' ? a : undefined);
|
|
251
103
|
return () => {
|
|
252
|
-
updateMeta(subKey, false, false);
|
|
104
|
+
(0, meta_1.updateMeta)(subKey, false, false);
|
|
253
105
|
(0, client_1.removeSubscriber)(client.client, subId, subscriberId);
|
|
254
106
|
};
|
|
255
107
|
}, [subId, clientKey]);
|
|
@@ -260,67 +112,11 @@ function useData(a, payload, clientSelector) {
|
|
|
260
112
|
}
|
|
261
113
|
else {
|
|
262
114
|
(0, react_1.useMemo)(stubFn, [payload, a]);
|
|
263
|
-
(0,
|
|
115
|
+
(0, clients_1.useClient)(clientSelector);
|
|
264
116
|
(0, react_1.useEffect)(stubFn, [null, null]);
|
|
265
117
|
}
|
|
266
118
|
return result;
|
|
267
119
|
}
|
|
268
120
|
exports.useData = useData;
|
|
269
|
-
function useLoading() {
|
|
270
|
-
const [, setLoading] = (0, react_1.useState)(isLoading);
|
|
271
|
-
loadingListeners.add(setLoading);
|
|
272
|
-
(0, react_1.useEffect)(() => {
|
|
273
|
-
return () => {
|
|
274
|
-
loadingListeners.delete(setLoading);
|
|
275
|
-
};
|
|
276
|
-
}, []);
|
|
277
|
-
return { loading: isLoading };
|
|
278
|
-
}
|
|
279
|
-
exports.useLoading = useLoading;
|
|
280
|
-
function useError() {
|
|
281
|
-
const [, setError] = (0, react_1.useState)(errorKey);
|
|
282
|
-
errorListeners.add(setError);
|
|
283
|
-
(0, react_1.useEffect)(() => {
|
|
284
|
-
return () => {
|
|
285
|
-
errorListeners.delete(setError);
|
|
286
|
-
};
|
|
287
|
-
}, []);
|
|
288
|
-
return { error: errorCnt ? lastError : null, errors: Object.values(errors) };
|
|
289
|
-
}
|
|
290
|
-
exports.useError = useError;
|
|
291
|
-
function updateMeta(subKey, loading, error) {
|
|
292
|
-
if (error) {
|
|
293
|
-
lastError = error;
|
|
294
|
-
if (subKey in errors) {
|
|
295
|
-
errors[subKey] = error;
|
|
296
|
-
}
|
|
297
|
-
else {
|
|
298
|
-
errors[subKey] = error;
|
|
299
|
-
errorCnt++;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
if (subKey in errors) {
|
|
304
|
-
errorCnt--;
|
|
305
|
-
delete errors[subKey];
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
const newErrorKey = errorCnt + lastError;
|
|
309
|
-
if (newErrorKey !== errorKey) {
|
|
310
|
-
errorKey = newErrorKey;
|
|
311
|
-
errorListeners.forEach((fn) => fn(errorKey));
|
|
312
|
-
}
|
|
313
|
-
if (loading) {
|
|
314
|
-
loadings.add(subKey);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
loadings.delete(subKey);
|
|
318
|
-
}
|
|
319
|
-
const newLoading = !!loadings.size;
|
|
320
|
-
if (newLoading !== isLoading) {
|
|
321
|
-
isLoading = newLoading;
|
|
322
|
-
loadingListeners.forEach((fn) => fn(isLoading));
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
121
|
function stubFn() { }
|
|
326
122
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,iCAAgE;AAEhE,0CAMsB;AAEtB,uCAAyC;AAIzC,2CAAuC;AAEvC,uCAAqC;AAErC,iCAAmC;AAEnC,yCAAsB;AACtB,4CAAyB;AACzB,wCAAqB;AAErB,SAAgB,OAAO,CACrB,cAAwD;IAExD,MAAM,MAAM,GAAG,IAAA,mBAAS,EAAC,cAAc,CAAC,CAAA;IACxC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAiB,KAAK,CAAC,CAAA;IACzD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,CAAC,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC3C,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;QACpB,OAA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,EAAE,SAAS,CAAC,CAAC,CAAA;SACvB;aAAM;YACL,IAAA,iBAAS,EAAC,MAAM,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;SAChC;KACF;SAAM;QACL,IAAA,eAAO,EAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAA;QAC7B,IAAA,mBAAS,EAAC,cAAc,CAAC,CAAA;QACzB,IAAA,iBAAS,EAAC,MAAM,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;KAChC;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAxED,0BAwEC;AAED,SAAS,MAAM,KAAI,CAAC"}
|
package/dist/meta.d.ts
ADDED
package/dist/meta.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useError = exports.useLoading = exports.updateMeta = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const errors = {};
|
|
6
|
+
const errorListeners = new Set();
|
|
7
|
+
const loadings = new Set();
|
|
8
|
+
const loadingListeners = new Set();
|
|
9
|
+
let isLoading = false;
|
|
10
|
+
let lastError = '';
|
|
11
|
+
let errorCnt = 0;
|
|
12
|
+
let errorKey = errorCnt + lastError;
|
|
13
|
+
function updateMeta(subKey, loading, error) {
|
|
14
|
+
if (error) {
|
|
15
|
+
lastError = error;
|
|
16
|
+
if (subKey in errors) {
|
|
17
|
+
errors[subKey] = error;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
errors[subKey] = error;
|
|
21
|
+
errorCnt++;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
if (subKey in errors) {
|
|
26
|
+
errorCnt--;
|
|
27
|
+
delete errors[subKey];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const newErrorKey = errorCnt + lastError;
|
|
31
|
+
if (newErrorKey !== errorKey) {
|
|
32
|
+
errorKey = newErrorKey;
|
|
33
|
+
errorListeners.forEach((fn) => fn(errorKey));
|
|
34
|
+
}
|
|
35
|
+
if (loading) {
|
|
36
|
+
loadings.add(subKey);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
loadings.delete(subKey);
|
|
40
|
+
}
|
|
41
|
+
const newLoading = !!loadings.size;
|
|
42
|
+
if (newLoading !== isLoading) {
|
|
43
|
+
isLoading = newLoading;
|
|
44
|
+
loadingListeners.forEach((fn) => fn(isLoading));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.updateMeta = updateMeta;
|
|
48
|
+
function useLoading() {
|
|
49
|
+
const [, setLoading] = (0, react_1.useState)(isLoading);
|
|
50
|
+
loadingListeners.add(setLoading);
|
|
51
|
+
(0, react_1.useEffect)(() => {
|
|
52
|
+
return () => {
|
|
53
|
+
loadingListeners.delete(setLoading);
|
|
54
|
+
};
|
|
55
|
+
}, []);
|
|
56
|
+
return { loading: isLoading };
|
|
57
|
+
}
|
|
58
|
+
exports.useLoading = useLoading;
|
|
59
|
+
function useError() {
|
|
60
|
+
const [, setError] = (0, react_1.useState)(errorKey);
|
|
61
|
+
errorListeners.add(setError);
|
|
62
|
+
(0, react_1.useEffect)(() => {
|
|
63
|
+
return () => {
|
|
64
|
+
errorListeners.delete(setError);
|
|
65
|
+
};
|
|
66
|
+
}, []);
|
|
67
|
+
return { error: errorCnt ? lastError : null, errors: Object.values(errors) };
|
|
68
|
+
}
|
|
69
|
+
exports.useError = useError;
|
|
70
|
+
//# sourceMappingURL=meta.js.map
|
package/dist/meta.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meta.js","sourceRoot":"","sources":["../src/meta.ts"],"names":[],"mappings":";;;AAAA,iCAA2C;AAE3C,MAAM,MAAM,GAAG,EAAE,CAAA;AACjB,MAAM,cAAc,GAAG,IAAI,GAAG,EAAE,CAAA;AAChC,MAAM,QAAQ,GAAG,IAAI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H,CAAC;AAjCD,gCAiCC;AAED,SAAgB,UAAU;IACxB,MAAM,CAAC,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAC,SAAS,CAAC,CAAA;IAE1C,gBAAgB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;IAEhC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,gBAAgB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;QACrC,CAAC,CAAA;IACH,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAA;AAC/B,CAAC;AAZD,gCAYC;AAED,SAAgB,QAAQ;IACtB,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAC,QAAQ,CAAC,CAAA;IAEvC,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAE5B,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QACjC,CAAC,CAAA;IACH,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAA;AAC9E,CAAC;AAZD,4BAYC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Data, Loading } from './types';
|
|
2
|
+
export declare function resultReducer(state: {
|
|
3
|
+
data: Data;
|
|
4
|
+
error?: Error;
|
|
5
|
+
loading: Loading;
|
|
6
|
+
checksum: number;
|
|
7
|
+
}, action: {
|
|
8
|
+
merge?: Data;
|
|
9
|
+
data?: Data;
|
|
10
|
+
error?: Error;
|
|
11
|
+
loading?: Loading;
|
|
12
|
+
checksum?: number;
|
|
13
|
+
}): {
|
|
14
|
+
data: GenericObject;
|
|
15
|
+
error?: Error;
|
|
16
|
+
loading: Loading;
|
|
17
|
+
checksum: number;
|
|
18
|
+
};
|
package/dist/reducer.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resultReducer = void 0;
|
|
4
|
+
function resultReducer(state, action) {
|
|
5
|
+
if (action.error) {
|
|
6
|
+
state.error = action.error;
|
|
7
|
+
}
|
|
8
|
+
if (action.data) {
|
|
9
|
+
state.checksum = action.checksum || 0;
|
|
10
|
+
state.data = action.data;
|
|
11
|
+
state.loading = false;
|
|
12
|
+
if (state.error) {
|
|
13
|
+
delete state.error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (action.merge) {
|
|
17
|
+
state.checksum = action.checksum || 0;
|
|
18
|
+
if (!state.data) {
|
|
19
|
+
state.data = action.merge;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
Object.assign(state.data, action.merge);
|
|
23
|
+
}
|
|
24
|
+
state.loading = false;
|
|
25
|
+
if (state.error) {
|
|
26
|
+
delete state.error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (action.loading) {
|
|
30
|
+
state.loading = action.loading;
|
|
31
|
+
}
|
|
32
|
+
return { ...state };
|
|
33
|
+
}
|
|
34
|
+
exports.resultReducer = resultReducer;
|
|
35
|
+
//# sourceMappingURL=reducer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reducer.js","sourceRoot":"","sources":["../src/reducer.ts"],"names":[],"mappings":";;;AAEA,SAAgB,aAAa,CAC3B,KAAwE,EACxE,MAMC;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;KAC3B;IACD,IAAI,MAAM,CAAC,IAAI,EAAE;QACf,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAA;QACrC,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;QACxB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAA;QACrB,IAAI,KAAK,CAAC,KAAK,EAAE;YACf,OAAO,KAAK,CAAC,KAAK,CAAA;SACnB;KACF;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAA;QACrC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACf,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAA;SAC1B;aAAM;YACL,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SACxC;QACD,KAAK,CAAC,OAAO,GAAG,KAAK,CAAA;QACrB,IAAI,KAAK,CAAC,KAAK,EAAE;YACf,OAAO,KAAK,CAAC,KAAK,CAAA;SACnB;KACF;IAED,IAAI,MAAM,CAAC,OAAO,EAAE;QAClB,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;KAC/B;IACD,OAAO,EAAE,GAAG,KAAK,EAAE,CAAA;AACrB,CAAC;AAvCD,sCAuCC"}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"source": "src/index.tsx",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"buildBundle": "node ./build/index.js",
|
|
8
|
+
"build": "npx tsc && npm run buildBundle",
|
|
9
|
+
"watch": "npm run build -- --watch"
|
|
9
10
|
},
|
|
10
11
|
"sideEffects": false,
|
|
11
12
|
"peerDependencies": {
|
|
12
|
-
"@based/client": "^
|
|
13
|
+
"@based/client": "^1.1.0",
|
|
13
14
|
"react": "^17.0.2"
|
|
14
15
|
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@saulx/hash": "^1.1.0"
|
|
18
|
+
},
|
|
15
19
|
"devDependencies": {
|
|
16
|
-
"
|
|
20
|
+
"esbuild": "^0.14.29",
|
|
17
21
|
"@types/react": "^17.0.14",
|
|
18
22
|
"ts-node": "^10.1.0",
|
|
19
23
|
"typescript": "^4.3.5"
|