@civet/core 1.4.2 → 2.0.0
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/dist/AbortSignal.d.ts +16 -0
- package/dist/ChannelNotifier.d.ts +6 -0
- package/dist/ConfigProvider.d.ts +8 -0
- package/dist/DataProvider.d.ts +110 -0
- package/dist/Meta.d.ts +18 -0
- package/dist/Notifier.d.ts +6 -0
- package/dist/Resource.d.ts +20 -0
- package/dist/ResourceProvider.d.ts +5 -0
- package/dist/compose.d.ts +1 -0
- package/dist/context.d.ts +15 -0
- package/dist/createPlugin.d.ts +4 -0
- package/dist/main.d.ts +18 -0
- package/dist/main.js +1473 -0
- package/dist/uniqueIdentifier.d.ts +8 -0
- package/dist/useResource.d.ts +29 -0
- package/package.json +50 -49
- package/lib/AbortSignal.js +0 -76
- package/lib/ChannelNotifier.js +0 -47
- package/lib/ConfigProvider.js +0 -32
- package/lib/DataProvider.js +0 -222
- package/lib/Meta.js +0 -63
- package/lib/Notifier.js +0 -39
- package/lib/Resource.js +0 -81
- package/lib/compose.js +0 -21
- package/lib/context.js +0 -21
- package/lib/createPlugin.js +0 -44
- package/lib/index.js +0 -109
- package/lib/uniqueIdentifier.js +0 -41
- package/lib/useResource.js +0 -335
- package/src/AbortSignal.js +0 -68
- package/src/ChannelNotifier.js +0 -35
- package/src/ConfigProvider.jsx +0 -22
- package/src/DataProvider.js +0 -228
- package/src/Meta.js +0 -50
- package/src/Notifier.js +0 -21
- package/src/Resource.jsx +0 -68
- package/src/compose.js +0 -9
- package/src/context.js +0 -11
- package/src/createPlugin.js +0 -23
- package/src/index.js +0 -25
- package/src/uniqueIdentifier.js +0 -27
- package/src/useResource.js +0 -321
package/lib/useResource.js
DELETED
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _react = require("react");
|
|
8
|
-
var _AbortSignal = _interopRequireDefault(require("./AbortSignal"));
|
|
9
|
-
var _Meta = _interopRequireDefault(require("./Meta"));
|
|
10
|
-
var _context2 = require("./context");
|
|
11
|
-
var _uniqueIdentifier = _interopRequireDefault(require("./uniqueIdentifier"));
|
|
12
|
-
var _excluded = ["dataProvider", "name", "query", "empty", "options", "persistent"];
|
|
13
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
14
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
15
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
16
|
-
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
17
|
-
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
18
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
19
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
20
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
21
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
22
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
23
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
24
|
-
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
25
|
-
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
26
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
27
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
28
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
29
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
30
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
31
|
-
/**
|
|
32
|
-
* Appends a new requestInstruction to the state, causing the resource to fetch data.
|
|
33
|
-
**/
|
|
34
|
-
function createRequestInstruction(state) {
|
|
35
|
-
return _objectSpread(_objectSpread({}, state), {}, {
|
|
36
|
-
requestInstruction: {
|
|
37
|
-
dataProvider: state.dataProvider,
|
|
38
|
-
requestDetails: state.requestDetails,
|
|
39
|
-
request: state.request,
|
|
40
|
-
revision: state.revision,
|
|
41
|
-
value: state.value
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* State reducer for the resource.
|
|
48
|
-
*/
|
|
49
|
-
function reducer(state, action) {
|
|
50
|
-
switch (action.type) {
|
|
51
|
-
// Creates a new request and instructs the resource to fetch data.
|
|
52
|
-
case 'next-request':
|
|
53
|
-
{
|
|
54
|
-
var nextRequestDetails = action.requestDetails,
|
|
55
|
-
nextPersistent = action.persistent;
|
|
56
|
-
var nextRequest = (0, _uniqueIdentifier["default"])(state.request);
|
|
57
|
-
var nextRevision = (0, _uniqueIdentifier["default"])(state.revision);
|
|
58
|
-
var isPersistent = false;
|
|
59
|
-
if (state.persistent === 'very' && nextPersistent === 'very') {
|
|
60
|
-
isPersistent = 'very';
|
|
61
|
-
} else if (state.persistent && nextPersistent) {
|
|
62
|
-
isPersistent = true;
|
|
63
|
-
}
|
|
64
|
-
var shouldValuePersist = !nextRequestDetails.empty && state.dataProvider.shouldPersist(nextRequestDetails, state.requestDetails, isPersistent, state.value);
|
|
65
|
-
return createRequestInstruction({
|
|
66
|
-
dataProvider: state.dataProvider,
|
|
67
|
-
requestDetails: nextRequestDetails,
|
|
68
|
-
request: nextRequest,
|
|
69
|
-
revision: nextRevision,
|
|
70
|
-
isLoading: !nextRequestDetails.empty,
|
|
71
|
-
value: shouldValuePersist ? state.value : {
|
|
72
|
-
name: nextRequestDetails.name,
|
|
73
|
-
query: nextRequestDetails.query,
|
|
74
|
-
options: nextRequestDetails.options,
|
|
75
|
-
request: nextRequest,
|
|
76
|
-
revision: nextRevision,
|
|
77
|
-
data: [],
|
|
78
|
-
meta: {},
|
|
79
|
-
error: undefined,
|
|
80
|
-
isEmpty: !!nextRequestDetails.empty,
|
|
81
|
-
isIncomplete: !nextRequestDetails.empty,
|
|
82
|
-
isInitial: !nextRequestDetails.empty
|
|
83
|
-
},
|
|
84
|
-
persistent: nextPersistent
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Creates a new revision for the current request and instructs the resource to fetch data.
|
|
89
|
-
case 'next-revision':
|
|
90
|
-
{
|
|
91
|
-
var notify = action.notify;
|
|
92
|
-
var _nextRevision = (0, _uniqueIdentifier["default"])(state.revision);
|
|
93
|
-
notify({
|
|
94
|
-
request: state.request,
|
|
95
|
-
revison: _nextRevision
|
|
96
|
-
});
|
|
97
|
-
return createRequestInstruction(_objectSpread(_objectSpread({}, state), {}, {
|
|
98
|
-
revision: _nextRevision,
|
|
99
|
-
isLoading: !state.requestDetails.empty
|
|
100
|
-
}));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Sets a new persistence level.
|
|
104
|
-
case 'set-persistence':
|
|
105
|
-
{
|
|
106
|
-
var _nextPersistent = action.persistent;
|
|
107
|
-
return _objectSpread(_objectSpread({}, state), {}, {
|
|
108
|
-
persistent: _nextPersistent
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Updates the current request's data.
|
|
113
|
-
case 'update-data':
|
|
114
|
-
{
|
|
115
|
-
var request = action.request,
|
|
116
|
-
revision = action.revision,
|
|
117
|
-
value = action.value;
|
|
118
|
-
if (request !== state.request || revision !== state.revision) {
|
|
119
|
-
return state;
|
|
120
|
-
}
|
|
121
|
-
return _objectSpread(_objectSpread({}, state), {}, {
|
|
122
|
-
isLoading: value.isIncomplete,
|
|
123
|
-
value: value
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return state;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Starts fetching data and updates the resource when new data is available.
|
|
132
|
-
*/
|
|
133
|
-
function fetchData(requestInstruction, instance, abortSignal, dispatch) {
|
|
134
|
-
var dataProvider = requestInstruction.dataProvider,
|
|
135
|
-
requestDetails = requestInstruction.requestDetails,
|
|
136
|
-
request = requestInstruction.request,
|
|
137
|
-
revision = requestInstruction.revision,
|
|
138
|
-
value = requestInstruction.value;
|
|
139
|
-
var meta = new _Meta["default"](_objectSpread({}, value.meta), instance);
|
|
140
|
-
var promise = Promise.resolve(value);
|
|
141
|
-
var callback = function callback(error, done, data) {
|
|
142
|
-
promise = promise.then(function (prevValue) {
|
|
143
|
-
try {
|
|
144
|
-
var nextValue;
|
|
145
|
-
if (error != null) {
|
|
146
|
-
nextValue = _objectSpread(_objectSpread({}, prevValue), {}, {
|
|
147
|
-
error: error,
|
|
148
|
-
isIncomplete: false
|
|
149
|
-
});
|
|
150
|
-
} else {
|
|
151
|
-
var context = {
|
|
152
|
-
name: requestDetails.name,
|
|
153
|
-
query: requestDetails.query,
|
|
154
|
-
options: requestDetails.options,
|
|
155
|
-
request: request,
|
|
156
|
-
revision: revision,
|
|
157
|
-
data: data,
|
|
158
|
-
meta: meta.commit(prevValue.meta),
|
|
159
|
-
error: undefined,
|
|
160
|
-
isEmpty: false,
|
|
161
|
-
isIncomplete: !done,
|
|
162
|
-
isInitial: !!prevValue.isInitial && !done
|
|
163
|
-
};
|
|
164
|
-
context.data = dataProvider.transition(context, prevValue);
|
|
165
|
-
context.data = dataProvider.recycleItems(context, prevValue);
|
|
166
|
-
nextValue = context;
|
|
167
|
-
}
|
|
168
|
-
dispatch({
|
|
169
|
-
type: 'update-data',
|
|
170
|
-
request: request,
|
|
171
|
-
revision: revision,
|
|
172
|
-
value: nextValue
|
|
173
|
-
});
|
|
174
|
-
return nextValue;
|
|
175
|
-
} catch (_unused) {
|
|
176
|
-
return prevValue;
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
};
|
|
180
|
-
dataProvider.continuousGet(requestDetails.name, requestDetails.query, requestDetails.options, meta, callback, abortSignal);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Provides data based on the given request details and DataProvider.
|
|
185
|
-
*
|
|
186
|
-
* Necessary configuration that is not directly specified is taken from the ConfigContext.
|
|
187
|
-
*
|
|
188
|
-
* The provided DataProvider must not be changed.
|
|
189
|
-
*/
|
|
190
|
-
function useResource(_ref) {
|
|
191
|
-
var dataProviderProp = _ref.dataProvider,
|
|
192
|
-
nextName = _ref.name,
|
|
193
|
-
nextQuery = _ref.query,
|
|
194
|
-
nextEmpty = _ref.empty,
|
|
195
|
-
nextOptions = _ref.options,
|
|
196
|
-
nextPersistent = _ref.persistent,
|
|
197
|
-
rest = _objectWithoutProperties(_ref, _excluded);
|
|
198
|
-
var configContext = (0, _context2.useConfigContext)();
|
|
199
|
-
var currentDataProvider = dataProviderProp || configContext.dataProvider;
|
|
200
|
-
var nextRequestDetails = (0, _react.useMemo)(function () {
|
|
201
|
-
return {
|
|
202
|
-
name: nextName,
|
|
203
|
-
query: nextQuery,
|
|
204
|
-
empty: nextEmpty,
|
|
205
|
-
options: nextOptions
|
|
206
|
-
};
|
|
207
|
-
}, [nextName, nextQuery, nextEmpty, nextOptions]);
|
|
208
|
-
var _useReducer = (0, _react.useReducer)(reducer, undefined, function () {
|
|
209
|
-
var request = (0, _uniqueIdentifier["default"])();
|
|
210
|
-
var revision = (0, _uniqueIdentifier["default"])();
|
|
211
|
-
return createRequestInstruction({
|
|
212
|
-
dataProvider: currentDataProvider,
|
|
213
|
-
requestDetails: nextRequestDetails,
|
|
214
|
-
request: request,
|
|
215
|
-
revision: revision,
|
|
216
|
-
isLoading: !nextRequestDetails.empty,
|
|
217
|
-
value: {
|
|
218
|
-
name: nextRequestDetails.name,
|
|
219
|
-
query: nextRequestDetails.query,
|
|
220
|
-
options: nextRequestDetails.options,
|
|
221
|
-
request: request,
|
|
222
|
-
revision: revision,
|
|
223
|
-
data: [],
|
|
224
|
-
meta: {},
|
|
225
|
-
error: undefined,
|
|
226
|
-
isEmpty: !!nextRequestDetails.empty,
|
|
227
|
-
isIncomplete: !nextRequestDetails.empty,
|
|
228
|
-
isInitial: !nextRequestDetails.empty
|
|
229
|
-
},
|
|
230
|
-
persistent: nextPersistent
|
|
231
|
-
});
|
|
232
|
-
}),
|
|
233
|
-
_useReducer2 = _slicedToArray(_useReducer, 2),
|
|
234
|
-
state = _useReducer2[0],
|
|
235
|
-
dispatch = _useReducer2[1];
|
|
236
|
-
var dataProvider = state.dataProvider,
|
|
237
|
-
requestDetails = state.requestDetails,
|
|
238
|
-
request = state.request,
|
|
239
|
-
revision = state.revision,
|
|
240
|
-
isLoading = state.isLoading,
|
|
241
|
-
value = state.value,
|
|
242
|
-
persistent = state.persistent,
|
|
243
|
-
requestInstruction = state.requestInstruction;
|
|
244
|
-
if (dataProvider == null) {
|
|
245
|
-
throw new Error('Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext provider and the dataProvider property');
|
|
246
|
-
}
|
|
247
|
-
if (dataProvider !== currentDataProvider) {
|
|
248
|
-
throw new Error('Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext provider and the dataProvider property');
|
|
249
|
-
}
|
|
250
|
-
var _useState = (0, _react.useState)(),
|
|
251
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
252
|
-
instance = _useState2[0],
|
|
253
|
-
setInstance = _useState2[1];
|
|
254
|
-
(0, _react.useEffect)(function () {
|
|
255
|
-
var i = dataProvider.createInstance();
|
|
256
|
-
setInstance(i !== null && i !== void 0 ? i : {});
|
|
257
|
-
return function () {
|
|
258
|
-
dataProvider.releaseInstance(i);
|
|
259
|
-
};
|
|
260
|
-
}, [dataProvider]);
|
|
261
|
-
if (requestDetails !== nextRequestDetails && !dataProvider.compareRequests(nextRequestDetails, requestDetails)) {
|
|
262
|
-
dispatch({
|
|
263
|
-
type: 'next-request',
|
|
264
|
-
requestDetails: nextRequestDetails,
|
|
265
|
-
persistent: nextPersistent
|
|
266
|
-
});
|
|
267
|
-
} else if (persistent !== nextPersistent) {
|
|
268
|
-
dispatch({
|
|
269
|
-
type: 'set-persistence',
|
|
270
|
-
persistent: nextPersistent
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
var notify = (0, _react.useCallback)(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
274
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
275
|
-
while (1) switch (_context.prev = _context.next) {
|
|
276
|
-
case 0:
|
|
277
|
-
return _context.abrupt("return", new Promise(function (resolve) {
|
|
278
|
-
dispatch({
|
|
279
|
-
type: 'next-revision',
|
|
280
|
-
notify: resolve
|
|
281
|
-
});
|
|
282
|
-
}));
|
|
283
|
-
case 1:
|
|
284
|
-
case "end":
|
|
285
|
-
return _context.stop();
|
|
286
|
-
}
|
|
287
|
-
}, _callee);
|
|
288
|
-
})), []);
|
|
289
|
-
|
|
290
|
-
// DataProvider events
|
|
291
|
-
(0, _react.useEffect)(function () {
|
|
292
|
-
if (requestDetails.empty) return undefined;
|
|
293
|
-
var unsubscribe = dataProvider.subscribe(requestDetails.name, notify);
|
|
294
|
-
return unsubscribe;
|
|
295
|
-
}, [requestDetails.empty, dataProvider, requestDetails.name, notify]);
|
|
296
|
-
|
|
297
|
-
// Fetch data when instructed
|
|
298
|
-
(0, _react.useEffect)(function () {
|
|
299
|
-
if (instance == null || requestInstruction.requestDetails.empty) {
|
|
300
|
-
return undefined;
|
|
301
|
-
}
|
|
302
|
-
var abortSignal = new _AbortSignal["default"]();
|
|
303
|
-
|
|
304
|
-
// Start fetching data.
|
|
305
|
-
fetchData(requestInstruction, instance, abortSignal, dispatch);
|
|
306
|
-
return function () {
|
|
307
|
-
// Abort fetching data when another request is pending or the React component is unmounted.
|
|
308
|
-
abortSignal.abort();
|
|
309
|
-
};
|
|
310
|
-
}, [instance, requestInstruction]);
|
|
311
|
-
var isStale = revision !== value.revision;
|
|
312
|
-
var nextRequest = isStale ? request : value.request;
|
|
313
|
-
var nextRevision = isStale ? revision : value.revision;
|
|
314
|
-
var next = (0, _react.useMemo)(function () {
|
|
315
|
-
return {
|
|
316
|
-
request: nextRequest,
|
|
317
|
-
revision: nextRevision
|
|
318
|
-
};
|
|
319
|
-
}, [nextRequest, nextRevision]);
|
|
320
|
-
var context = (0, _react.useMemo)(function () {
|
|
321
|
-
return _objectSpread(_objectSpread({}, value), {}, {
|
|
322
|
-
dataProvider: dataProvider,
|
|
323
|
-
isLoading: isLoading,
|
|
324
|
-
isStale: isStale,
|
|
325
|
-
next: next,
|
|
326
|
-
notify: notify
|
|
327
|
-
});
|
|
328
|
-
}, [value, dataProvider, isLoading, isStale, next, notify]);
|
|
329
|
-
|
|
330
|
-
// Apply context plugins and return the final context.
|
|
331
|
-
return dataProvider.contextPlugins.reduce(function (result, fn) {
|
|
332
|
-
return fn(result, rest);
|
|
333
|
-
}, context);
|
|
334
|
-
}
|
|
335
|
-
var _default = exports["default"] = useResource;
|
package/src/AbortSignal.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import Notifier from './Notifier';
|
|
2
|
-
|
|
3
|
-
class AbortSignal {
|
|
4
|
-
constructor() {
|
|
5
|
-
this.notifier = new Notifier();
|
|
6
|
-
Object.defineProperties(this, {
|
|
7
|
-
locked: {
|
|
8
|
-
value: false,
|
|
9
|
-
enumerable: true,
|
|
10
|
-
writable: false,
|
|
11
|
-
configurable: true,
|
|
12
|
-
},
|
|
13
|
-
aborted: {
|
|
14
|
-
value: false,
|
|
15
|
-
enumerable: true,
|
|
16
|
-
writable: false,
|
|
17
|
-
configurable: true,
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
listen = (cb) => {
|
|
23
|
-
if (this.locked) return () => {};
|
|
24
|
-
const alreadySubscribed = this.notifier.isSubscribed(cb);
|
|
25
|
-
const unsubscribe = this.notifier.subscribe(cb);
|
|
26
|
-
if (this.aborted && !alreadySubscribed) cb();
|
|
27
|
-
return unsubscribe;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
abort = () => {
|
|
31
|
-
if (this.locked) return;
|
|
32
|
-
this.lock();
|
|
33
|
-
Object.defineProperty(this, 'aborted', {
|
|
34
|
-
value: true,
|
|
35
|
-
enumerable: true,
|
|
36
|
-
writable: false,
|
|
37
|
-
configurable: false,
|
|
38
|
-
});
|
|
39
|
-
this.notifier.trigger();
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
lock = () => {
|
|
43
|
-
if (this.locked) return;
|
|
44
|
-
Object.defineProperty(this, 'locked', {
|
|
45
|
-
value: true,
|
|
46
|
-
enumerable: true,
|
|
47
|
-
writable: false,
|
|
48
|
-
configurable: false,
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
proxy = () => {
|
|
53
|
-
const s = this;
|
|
54
|
-
return {
|
|
55
|
-
listen(cb) {
|
|
56
|
-
return s.listen(cb);
|
|
57
|
-
},
|
|
58
|
-
get locked() {
|
|
59
|
-
return s.locked;
|
|
60
|
-
},
|
|
61
|
-
get aborted() {
|
|
62
|
-
return s.locked;
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export default AbortSignal;
|
package/src/ChannelNotifier.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import Notifier from './Notifier';
|
|
2
|
-
|
|
3
|
-
class ChannelNotifier {
|
|
4
|
-
channels = {};
|
|
5
|
-
|
|
6
|
-
subscribe = (channel, handler) => {
|
|
7
|
-
if (channel == null || !`${channel}`) {
|
|
8
|
-
throw new Error('Channel is required');
|
|
9
|
-
}
|
|
10
|
-
if (this.channels[channel] == null) this.channels[channel] = new Notifier();
|
|
11
|
-
return this.channels[channel].subscribe(handler);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
isSubscribed = (channel, handler) => {
|
|
15
|
-
if (channel == null || !`${channel}`) {
|
|
16
|
-
throw new Error('Channel is required');
|
|
17
|
-
}
|
|
18
|
-
return (
|
|
19
|
-
this.channels[channel] != null &&
|
|
20
|
-
this.channels[channel].isSubscribed(handler)
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
trigger = (channel, ...args) => {
|
|
25
|
-
if (channel == null) {
|
|
26
|
-
Object.values(this.channels).forEach((notifier) =>
|
|
27
|
-
notifier.trigger(...args),
|
|
28
|
-
);
|
|
29
|
-
} else if (this.channels[channel] != null) {
|
|
30
|
-
this.channels[channel].trigger(...args);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export default ChannelNotifier;
|
package/src/ConfigProvider.jsx
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import PropTypes from 'prop-types';
|
|
2
|
-
import { useMemo } from 'react';
|
|
3
|
-
import { dataProviderPropType } from './DataProvider';
|
|
4
|
-
import { ConfigContext } from './context';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Provides general configuration to its descendants using React's context API.
|
|
8
|
-
*/
|
|
9
|
-
function ConfigProvider({ dataProvider, children }) {
|
|
10
|
-
const context = useMemo(() => ({ dataProvider }), [dataProvider]);
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<ConfigContext.Provider value={context}>{children}</ConfigContext.Provider>
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
ConfigProvider.propTypes = {
|
|
18
|
-
dataProvider: dataProviderPropType,
|
|
19
|
-
children: PropTypes.node,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export default ConfigProvider;
|