@cybertools/react 1.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/LICENSE +201 -0
- package/README.md +38 -0
- package/dist/ErrorBoundary.cjs +26 -0
- package/dist/ErrorBoundary.d.cts +16 -0
- package/dist/ErrorBoundary.d.mts +16 -0
- package/dist/ErrorBoundary.mjs +24 -0
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/index.cjs +16 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/makeContext.cjs +47 -0
- package/dist/makeContext.d.cts +21 -0
- package/dist/makeContext.d.mts +21 -0
- package/dist/makeContext.mjs +45 -0
- package/dist/makeProvider.cjs +66 -0
- package/dist/makeProvider.d.cts +22 -0
- package/dist/makeProvider.d.mts +22 -0
- package/dist/makeProvider.mjs +66 -0
- package/dist/useContextSelector.cjs +46 -0
- package/dist/useContextSelector.d.cts +4 -0
- package/dist/useContextSelector.d.mts +4 -0
- package/dist/useContextSelector.mjs +46 -0
- package/dist/useFetchFnPromise.cjs +31 -0
- package/dist/useFetchFnPromise.d.cts +11 -0
- package/dist/useFetchFnPromise.d.mts +11 -0
- package/dist/useFetchFnPromise.mjs +31 -0
- package/dist/useFetchPromise.cjs +61 -0
- package/dist/useFetchPromise.d.cts +19 -0
- package/dist/useFetchPromise.d.mts +19 -0
- package/dist/useFetchPromise.mjs +60 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @cybertools/react
|
|
2
|
+
|
|
3
|
+
React state factory (`makeProvider`), data-fetching hooks and `ErrorBoundary` for Cyberscope apps.
|
|
4
|
+
|
|
5
|
+
## API
|
|
6
|
+
|
|
7
|
+
All exports are named, from the single entry `@cybertools/react`.
|
|
8
|
+
|
|
9
|
+
| Export | Description |
|
|
10
|
+
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
11
|
+
| `makeProvider(options?)` | Builds a keyed collection store: `Provider`, `useCollection`, `useResource`, `useResourceValue`, `useCollectionDispatch`, `useResourceDispatch`. |
|
|
12
|
+
| `makeContext()` | Creates the selectable state/dispatch context pair used by `makeProvider`. |
|
|
13
|
+
| `createSelectableContext(defaultValue)` | Creates a context with listener-based selection (no `Consumer`). |
|
|
14
|
+
| `providerSymbol` | Marker symbol. |
|
|
15
|
+
| `useContextSelector(context, selector?)` | Subscribes to a selectable context and re-renders only when the selected value changes (deep equality). |
|
|
16
|
+
| `useFetchPromise(options, deps)` | `[data, loaded, loading, refresh]`. |
|
|
17
|
+
| `useFetchWithErrorPromise(options, deps)` | `{ data, loaded, loading, error, refresh }`. |
|
|
18
|
+
| `useFetchFnPromise(options, deps)` | `[func, data, loading, error]`; call `func(...args)` to run the action. |
|
|
19
|
+
| `ErrorBoundary` | Class component rendering `fallback` (or nothing) after a child throws. |
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { makeProvider } from "@cybertools/react";
|
|
23
|
+
|
|
24
|
+
const audits = makeProvider<{ name: string }>({
|
|
25
|
+
getResourceInitialState: () => ({ name: "" }),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// <audits.Provider value={{ a: { name: "x" } }}>...</audits.Provider>
|
|
29
|
+
const name = audits.useResource("a", (r) => r.name);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## React support
|
|
33
|
+
|
|
34
|
+
Peer range `react@^18.2.0 || ^19.0.0`. `makeProvider` relies on render-phase `setState` and `scheduler` priorities; it is a verbatim, typed port of the legacy implementation. CI tests run on React 19.
|
|
35
|
+
|
|
36
|
+
## Optional `@sentry/react`
|
|
37
|
+
|
|
38
|
+
`useFetchFnPromise({ sentry: true })` dynamically imports `@sentry/react` and calls `captureException` on failure. `@sentry/react` is an optional peer dependency; install it only if you use `sentry: true`.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
react = require_runtime.__toESM(react, 1);
|
|
4
|
+
//#region src/ErrorBoundary.tsx
|
|
5
|
+
var ErrorBoundary = class extends react.default.Component {
|
|
6
|
+
constructor(props) {
|
|
7
|
+
super(props);
|
|
8
|
+
this.state = {
|
|
9
|
+
error: null,
|
|
10
|
+
errorInfo: null
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
componentDidCatch(error, errorInfo) {
|
|
14
|
+
console.log("Error", error, errorInfo);
|
|
15
|
+
this.setState({
|
|
16
|
+
error,
|
|
17
|
+
errorInfo
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
render() {
|
|
21
|
+
if (this.state.errorInfo) return this.props.fallback || null;
|
|
22
|
+
return this.props.children;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.ErrorBoundary = ErrorBoundary;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
//#region src/ErrorBoundary.d.ts
|
|
3
|
+
export interface ErrorBoundaryProps {
|
|
4
|
+
fallback?: React.ReactNode;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export interface ErrorBoundaryState {
|
|
8
|
+
error: Error | null;
|
|
9
|
+
errorInfo: React.ErrorInfo | null;
|
|
10
|
+
}
|
|
11
|
+
export declare class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
12
|
+
constructor(props: ErrorBoundaryProps);
|
|
13
|
+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
|
|
14
|
+
render(): React.ReactNode;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
//#region src/ErrorBoundary.d.ts
|
|
3
|
+
export interface ErrorBoundaryProps {
|
|
4
|
+
fallback?: React.ReactNode;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export interface ErrorBoundaryState {
|
|
8
|
+
error: Error | null;
|
|
9
|
+
errorInfo: React.ErrorInfo | null;
|
|
10
|
+
}
|
|
11
|
+
export declare class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
12
|
+
constructor(props: ErrorBoundaryProps);
|
|
13
|
+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
|
|
14
|
+
render(): React.ReactNode;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
//#region src/ErrorBoundary.tsx
|
|
3
|
+
var ErrorBoundary = class extends React.Component {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
this.state = {
|
|
7
|
+
error: null,
|
|
8
|
+
errorInfo: null
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
componentDidCatch(error, errorInfo) {
|
|
12
|
+
console.log("Error", error, errorInfo);
|
|
13
|
+
this.setState({
|
|
14
|
+
error,
|
|
15
|
+
errorInfo
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
render() {
|
|
19
|
+
if (this.state.errorInfo) return this.props.fallback || null;
|
|
20
|
+
return this.props.children;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
export { ErrorBoundary };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.__toESM = __toESM;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_makeContext = require("./makeContext.cjs");
|
|
3
|
+
const require_useContextSelector = require("./useContextSelector.cjs");
|
|
4
|
+
const require_makeProvider = require("./makeProvider.cjs");
|
|
5
|
+
const require_useFetchPromise = require("./useFetchPromise.cjs");
|
|
6
|
+
const require_useFetchFnPromise = require("./useFetchFnPromise.cjs");
|
|
7
|
+
const require_ErrorBoundary = require("./ErrorBoundary.cjs");
|
|
8
|
+
exports.ErrorBoundary = require_ErrorBoundary.ErrorBoundary;
|
|
9
|
+
exports.createSelectableContext = require_makeContext.createSelectableContext;
|
|
10
|
+
exports.makeContext = require_makeContext.makeContext;
|
|
11
|
+
exports.makeProvider = require_makeProvider.makeProvider;
|
|
12
|
+
exports.providerSymbol = require_makeContext.providerSymbol;
|
|
13
|
+
exports.useContextSelector = require_useContextSelector.useContextSelector;
|
|
14
|
+
exports.useFetchFnPromise = require_useFetchFnPromise.useFetchFnPromise;
|
|
15
|
+
exports.useFetchPromise = require_useFetchPromise.useFetchPromise;
|
|
16
|
+
exports.useFetchWithErrorPromise = require_useFetchPromise.useFetchWithErrorPromise;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Collection, CollectionProviderProps, CollectionUpdate, MadeProvider, MakeProviderOptions, makeProvider } from "./makeProvider.cjs";
|
|
2
|
+
import { SelectableContext, SelectableContextValue, SelectableProvider, createSelectableContext, makeContext, providerSymbol } from "./makeContext.cjs";
|
|
3
|
+
import { useContextSelector } from "./useContextSelector.cjs";
|
|
4
|
+
import { UseFetchPromiseOptions, useFetchPromise, useFetchWithErrorPromise } from "./useFetchPromise.cjs";
|
|
5
|
+
import { UseFetchFnPromiseOptions, useFetchFnPromise } from "./useFetchFnPromise.cjs";
|
|
6
|
+
import { ErrorBoundary, ErrorBoundaryProps, ErrorBoundaryState } from "./ErrorBoundary.cjs";
|
|
7
|
+
export { type Collection, type CollectionProviderProps, type CollectionUpdate, ErrorBoundary, type ErrorBoundaryProps, type ErrorBoundaryState, type MadeProvider, type MakeProviderOptions, type SelectableContext, type SelectableContextValue, type SelectableProvider, type UseFetchFnPromiseOptions, type UseFetchPromiseOptions, createSelectableContext, makeContext, makeProvider, providerSymbol, useContextSelector, useFetchFnPromise, useFetchPromise, useFetchWithErrorPromise };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Collection, CollectionProviderProps, CollectionUpdate, MadeProvider, MakeProviderOptions, makeProvider } from "./makeProvider.mjs";
|
|
2
|
+
import { SelectableContext, SelectableContextValue, SelectableProvider, createSelectableContext, makeContext, providerSymbol } from "./makeContext.mjs";
|
|
3
|
+
import { useContextSelector } from "./useContextSelector.mjs";
|
|
4
|
+
import { UseFetchPromiseOptions, useFetchPromise, useFetchWithErrorPromise } from "./useFetchPromise.mjs";
|
|
5
|
+
import { UseFetchFnPromiseOptions, useFetchFnPromise } from "./useFetchFnPromise.mjs";
|
|
6
|
+
import { ErrorBoundary, ErrorBoundaryProps, ErrorBoundaryState } from "./ErrorBoundary.mjs";
|
|
7
|
+
export { type Collection, type CollectionProviderProps, type CollectionUpdate, ErrorBoundary, type ErrorBoundaryProps, type ErrorBoundaryState, type MadeProvider, type MakeProviderOptions, type SelectableContext, type SelectableContextValue, type SelectableProvider, type UseFetchFnPromiseOptions, type UseFetchPromiseOptions, createSelectableContext, makeContext, makeProvider, providerSymbol, useContextSelector, useFetchFnPromise, useFetchPromise, useFetchWithErrorPromise };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createSelectableContext, makeContext, providerSymbol } from "./makeContext.mjs";
|
|
2
|
+
import { useContextSelector } from "./useContextSelector.mjs";
|
|
3
|
+
import { makeProvider } from "./makeProvider.mjs";
|
|
4
|
+
import { useFetchPromise, useFetchWithErrorPromise } from "./useFetchPromise.mjs";
|
|
5
|
+
import { useFetchFnPromise } from "./useFetchFnPromise.mjs";
|
|
6
|
+
import { ErrorBoundary } from "./ErrorBoundary.mjs";
|
|
7
|
+
export { ErrorBoundary, createSelectableContext, makeContext, makeProvider, providerSymbol, useContextSelector, useFetchFnPromise, useFetchPromise, useFetchWithErrorPromise };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let scheduler = require("scheduler");
|
|
3
|
+
//#region src/makeContext.ts
|
|
4
|
+
const runWithNormalPriority = scheduler.unstable_runWithPriority ? (thunk) => (0, scheduler.unstable_runWithPriority)(scheduler.unstable_NormalPriority, thunk) : (thunk) => thunk();
|
|
5
|
+
const providerSymbol = Symbol();
|
|
6
|
+
const createProvider = (ProviderOrig) => {
|
|
7
|
+
const ContextProvider = ({ value, children }) => {
|
|
8
|
+
const valueRef = (0, react.useRef)(value);
|
|
9
|
+
const versionRef = (0, react.useRef)(0);
|
|
10
|
+
const contextValue = (0, react.useRef)(void 0);
|
|
11
|
+
if (!contextValue.current) contextValue.current = {
|
|
12
|
+
value: valueRef,
|
|
13
|
+
version: versionRef,
|
|
14
|
+
listeners: /* @__PURE__ */ new Set()
|
|
15
|
+
};
|
|
16
|
+
const current = contextValue.current;
|
|
17
|
+
if (valueRef.current !== value) {
|
|
18
|
+
valueRef.current = value;
|
|
19
|
+
versionRef.current += 1;
|
|
20
|
+
runWithNormalPriority(() => {
|
|
21
|
+
current.listeners.forEach((listener) => {
|
|
22
|
+
listener([versionRef.current, value]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return (0, react.createElement)(ProviderOrig, { value: current }, children);
|
|
27
|
+
};
|
|
28
|
+
return ContextProvider;
|
|
29
|
+
};
|
|
30
|
+
function createSelectableContext(defaultValue) {
|
|
31
|
+
const context = (0, react.createContext)({
|
|
32
|
+
value: { current: defaultValue },
|
|
33
|
+
version: { current: -1 },
|
|
34
|
+
listeners: /* @__PURE__ */ new Set()
|
|
35
|
+
});
|
|
36
|
+
const mutable = context;
|
|
37
|
+
mutable.Provider = createProvider(context.Provider);
|
|
38
|
+
delete mutable.Consumer;
|
|
39
|
+
return context;
|
|
40
|
+
}
|
|
41
|
+
function makeContext() {
|
|
42
|
+
return [createSelectableContext({}), createSelectableContext({})];
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.createSelectableContext = createSelectableContext;
|
|
46
|
+
exports.makeContext = makeContext;
|
|
47
|
+
exports.providerSymbol = providerSymbol;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ComponentType, Context, ReactNode } from "react";
|
|
2
|
+
//#region src/makeContext.d.ts
|
|
3
|
+
export interface SelectableContextValue<V> {
|
|
4
|
+
value: {
|
|
5
|
+
current: V;
|
|
6
|
+
};
|
|
7
|
+
version: {
|
|
8
|
+
current: number;
|
|
9
|
+
};
|
|
10
|
+
listeners: Set<(update: [number, V] | [number]) => void>;
|
|
11
|
+
}
|
|
12
|
+
export type SelectableContext<V> = Context<SelectableContextValue<V>>;
|
|
13
|
+
/** The replaced `Provider` takes the raw value, not a `SelectableContextValue`. */
|
|
14
|
+
export type SelectableProvider<V> = ComponentType<{
|
|
15
|
+
value: V;
|
|
16
|
+
children?: ReactNode;
|
|
17
|
+
}>;
|
|
18
|
+
export declare const providerSymbol: unique symbol;
|
|
19
|
+
export declare function createSelectableContext<V>(defaultValue: V): SelectableContext<V>;
|
|
20
|
+
export declare function makeContext(): [SelectableContext<unknown>, SelectableContext<unknown>];
|
|
21
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ComponentType, Context, ReactNode } from "react";
|
|
2
|
+
//#region src/makeContext.d.ts
|
|
3
|
+
export interface SelectableContextValue<V> {
|
|
4
|
+
value: {
|
|
5
|
+
current: V;
|
|
6
|
+
};
|
|
7
|
+
version: {
|
|
8
|
+
current: number;
|
|
9
|
+
};
|
|
10
|
+
listeners: Set<(update: [number, V] | [number]) => void>;
|
|
11
|
+
}
|
|
12
|
+
export type SelectableContext<V> = Context<SelectableContextValue<V>>;
|
|
13
|
+
/** The replaced `Provider` takes the raw value, not a `SelectableContextValue`. */
|
|
14
|
+
export type SelectableProvider<V> = ComponentType<{
|
|
15
|
+
value: V;
|
|
16
|
+
children?: ReactNode;
|
|
17
|
+
}>;
|
|
18
|
+
export declare const providerSymbol: unique symbol;
|
|
19
|
+
export declare function createSelectableContext<V>(defaultValue: V): SelectableContext<V>;
|
|
20
|
+
export declare function makeContext(): [SelectableContext<unknown>, SelectableContext<unknown>];
|
|
21
|
+
//#endregion
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createContext, createElement, useRef } from "react";
|
|
2
|
+
import { unstable_NormalPriority, unstable_runWithPriority } from "scheduler";
|
|
3
|
+
//#region src/makeContext.ts
|
|
4
|
+
const runWithNormalPriority = unstable_runWithPriority ? (thunk) => unstable_runWithPriority(unstable_NormalPriority, thunk) : (thunk) => thunk();
|
|
5
|
+
const providerSymbol = Symbol();
|
|
6
|
+
const createProvider = (ProviderOrig) => {
|
|
7
|
+
const ContextProvider = ({ value, children }) => {
|
|
8
|
+
const valueRef = useRef(value);
|
|
9
|
+
const versionRef = useRef(0);
|
|
10
|
+
const contextValue = useRef(void 0);
|
|
11
|
+
if (!contextValue.current) contextValue.current = {
|
|
12
|
+
value: valueRef,
|
|
13
|
+
version: versionRef,
|
|
14
|
+
listeners: /* @__PURE__ */ new Set()
|
|
15
|
+
};
|
|
16
|
+
const current = contextValue.current;
|
|
17
|
+
if (valueRef.current !== value) {
|
|
18
|
+
valueRef.current = value;
|
|
19
|
+
versionRef.current += 1;
|
|
20
|
+
runWithNormalPriority(() => {
|
|
21
|
+
current.listeners.forEach((listener) => {
|
|
22
|
+
listener([versionRef.current, value]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return createElement(ProviderOrig, { value: current }, children);
|
|
27
|
+
};
|
|
28
|
+
return ContextProvider;
|
|
29
|
+
};
|
|
30
|
+
function createSelectableContext(defaultValue) {
|
|
31
|
+
const context = createContext({
|
|
32
|
+
value: { current: defaultValue },
|
|
33
|
+
version: { current: -1 },
|
|
34
|
+
listeners: /* @__PURE__ */ new Set()
|
|
35
|
+
});
|
|
36
|
+
const mutable = context;
|
|
37
|
+
mutable.Provider = createProvider(context.Provider);
|
|
38
|
+
delete mutable.Consumer;
|
|
39
|
+
return context;
|
|
40
|
+
}
|
|
41
|
+
function makeContext() {
|
|
42
|
+
return [createSelectableContext({}), createSelectableContext({})];
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createSelectableContext, makeContext, providerSymbol };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const require_makeContext = require("./makeContext.cjs");
|
|
2
|
+
const require_useContextSelector = require("./useContextSelector.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let _cybertools_utils = require("@cybertools/utils");
|
|
5
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
6
|
+
//#region src/makeProvider.tsx
|
|
7
|
+
const defaultMergeCollection = (state, collection) => ({
|
|
8
|
+
...state,
|
|
9
|
+
...collection
|
|
10
|
+
});
|
|
11
|
+
function makeProvider({ getResourceInitialState = () => ({}), mergeCollection } = {}) {
|
|
12
|
+
const merge = mergeCollection || defaultMergeCollection;
|
|
13
|
+
const [ContextUntyped, DispatchContextUntyped] = require_makeContext.makeContext();
|
|
14
|
+
const Context = ContextUntyped;
|
|
15
|
+
const DispatchContext = DispatchContextUntyped;
|
|
16
|
+
const StateProvider = Context.Provider;
|
|
17
|
+
const DispatchProvider = DispatchContext.Provider;
|
|
18
|
+
const emptyObject = {};
|
|
19
|
+
const Provider = ({ value: initialValue = emptyObject, children }) => {
|
|
20
|
+
const [value, setValue] = (0, react.useState)(initialValue);
|
|
21
|
+
const initialValueRef = (0, react.useRef)(initialValue);
|
|
22
|
+
const stateRef = (0, react.useRef)(value);
|
|
23
|
+
let nextValue = value;
|
|
24
|
+
if (initialValueRef.current !== initialValue) {
|
|
25
|
+
initialValueRef.current = initialValue;
|
|
26
|
+
nextValue = merge(value, initialValue);
|
|
27
|
+
stateRef.current = nextValue;
|
|
28
|
+
}
|
|
29
|
+
(0, react.useEffect)(() => {
|
|
30
|
+
setValue(nextValue);
|
|
31
|
+
}, [nextValue]);
|
|
32
|
+
const dispatch = (0, react.useCallback)((state) => {
|
|
33
|
+
const nextState = typeof state === "function" ? state(stateRef.current) : state;
|
|
34
|
+
stateRef.current = nextState;
|
|
35
|
+
setValue(nextState);
|
|
36
|
+
}, []);
|
|
37
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DispatchProvider, {
|
|
38
|
+
value: dispatch,
|
|
39
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(StateProvider, {
|
|
40
|
+
value: stateRef.current,
|
|
41
|
+
children
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
const useCollection = (selector = _cybertools_utils.identity) => require_useContextSelector.useContextSelector(Context, selector);
|
|
46
|
+
const useResource = (id, selector = _cybertools_utils.identity) => require_useContextSelector.useContextSelector(Context, (s) => selector(s[id] || getResourceInitialState()));
|
|
47
|
+
const useResourceValue = (id, fieldKey) => useResource(id, (s) => s[fieldKey]);
|
|
48
|
+
const useCollectionDispatch = () => require_useContextSelector.useContextSelector(DispatchContext);
|
|
49
|
+
const useResourceDispatch = (id) => {
|
|
50
|
+
const dispatch = useCollectionDispatch();
|
|
51
|
+
return (0, react.useCallback)((action) => dispatch((state) => ({
|
|
52
|
+
...state,
|
|
53
|
+
[id]: action(state[id] || getResourceInitialState())
|
|
54
|
+
})), [id]);
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
Provider: (0, react.memo)(Provider),
|
|
58
|
+
useCollection,
|
|
59
|
+
useResource,
|
|
60
|
+
useResourceValue,
|
|
61
|
+
useCollectionDispatch,
|
|
62
|
+
useResourceDispatch
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
exports.makeProvider = makeProvider;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MemoExoticComponent, ReactElement, ReactNode } from "react";
|
|
2
|
+
//#region src/makeProvider.d.ts
|
|
3
|
+
export type Collection<T> = Record<string, T>;
|
|
4
|
+
export type CollectionUpdate<T> = Collection<T> | ((prev: Collection<T>) => Collection<T>);
|
|
5
|
+
export interface MakeProviderOptions<T> {
|
|
6
|
+
getResourceInitialState?: () => T;
|
|
7
|
+
mergeCollection?: (state: Collection<T>, collection: Collection<T>) => Collection<T>;
|
|
8
|
+
}
|
|
9
|
+
export interface CollectionProviderProps<T> {
|
|
10
|
+
value?: Collection<T>;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
export interface MadeProvider<T> {
|
|
14
|
+
Provider: MemoExoticComponent<(props: CollectionProviderProps<T>) => ReactElement>;
|
|
15
|
+
useCollection: <S = Collection<T>>(selector?: (state: Collection<T>) => S) => S;
|
|
16
|
+
useResource: <S = T>(id: string, selector?: (resource: T) => S) => S;
|
|
17
|
+
useResourceValue: <K extends keyof T>(id: string, fieldKey: K) => T[K];
|
|
18
|
+
useCollectionDispatch: () => (update: CollectionUpdate<T>) => void;
|
|
19
|
+
useResourceDispatch: (id: string) => (action: (resource: T) => T) => void;
|
|
20
|
+
}
|
|
21
|
+
export declare function makeProvider<T = Record<string, unknown>>({ getResourceInitialState, mergeCollection }?: MakeProviderOptions<T>): MadeProvider<T>;
|
|
22
|
+
//#endregion
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MemoExoticComponent, ReactElement, ReactNode } from "react";
|
|
2
|
+
//#region src/makeProvider.d.ts
|
|
3
|
+
export type Collection<T> = Record<string, T>;
|
|
4
|
+
export type CollectionUpdate<T> = Collection<T> | ((prev: Collection<T>) => Collection<T>);
|
|
5
|
+
export interface MakeProviderOptions<T> {
|
|
6
|
+
getResourceInitialState?: () => T;
|
|
7
|
+
mergeCollection?: (state: Collection<T>, collection: Collection<T>) => Collection<T>;
|
|
8
|
+
}
|
|
9
|
+
export interface CollectionProviderProps<T> {
|
|
10
|
+
value?: Collection<T>;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
export interface MadeProvider<T> {
|
|
14
|
+
Provider: MemoExoticComponent<(props: CollectionProviderProps<T>) => ReactElement>;
|
|
15
|
+
useCollection: <S = Collection<T>>(selector?: (state: Collection<T>) => S) => S;
|
|
16
|
+
useResource: <S = T>(id: string, selector?: (resource: T) => S) => S;
|
|
17
|
+
useResourceValue: <K extends keyof T>(id: string, fieldKey: K) => T[K];
|
|
18
|
+
useCollectionDispatch: () => (update: CollectionUpdate<T>) => void;
|
|
19
|
+
useResourceDispatch: (id: string) => (action: (resource: T) => T) => void;
|
|
20
|
+
}
|
|
21
|
+
export declare function makeProvider<T = Record<string, unknown>>({ getResourceInitialState, mergeCollection }?: MakeProviderOptions<T>): MadeProvider<T>;
|
|
22
|
+
//#endregion
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { makeContext } from "./makeContext.mjs";
|
|
2
|
+
import { useContextSelector } from "./useContextSelector.mjs";
|
|
3
|
+
import { memo, useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { identity } from "@cybertools/utils";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
//#region src/makeProvider.tsx
|
|
7
|
+
const defaultMergeCollection = (state, collection) => ({
|
|
8
|
+
...state,
|
|
9
|
+
...collection
|
|
10
|
+
});
|
|
11
|
+
function makeProvider({ getResourceInitialState = () => ({}), mergeCollection } = {}) {
|
|
12
|
+
const merge = mergeCollection || defaultMergeCollection;
|
|
13
|
+
const [ContextUntyped, DispatchContextUntyped] = makeContext();
|
|
14
|
+
const Context = ContextUntyped;
|
|
15
|
+
const DispatchContext = DispatchContextUntyped;
|
|
16
|
+
const StateProvider = Context.Provider;
|
|
17
|
+
const DispatchProvider = DispatchContext.Provider;
|
|
18
|
+
const emptyObject = {};
|
|
19
|
+
const Provider = ({ value: initialValue = emptyObject, children }) => {
|
|
20
|
+
const [value, setValue] = useState(initialValue);
|
|
21
|
+
const initialValueRef = useRef(initialValue);
|
|
22
|
+
const stateRef = useRef(value);
|
|
23
|
+
let nextValue = value;
|
|
24
|
+
if (initialValueRef.current !== initialValue) {
|
|
25
|
+
initialValueRef.current = initialValue;
|
|
26
|
+
nextValue = merge(value, initialValue);
|
|
27
|
+
stateRef.current = nextValue;
|
|
28
|
+
}
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
setValue(nextValue);
|
|
31
|
+
}, [nextValue]);
|
|
32
|
+
const dispatch = useCallback((state) => {
|
|
33
|
+
const nextState = typeof state === "function" ? state(stateRef.current) : state;
|
|
34
|
+
stateRef.current = nextState;
|
|
35
|
+
setValue(nextState);
|
|
36
|
+
}, []);
|
|
37
|
+
return /* @__PURE__ */ jsx(DispatchProvider, {
|
|
38
|
+
value: dispatch,
|
|
39
|
+
children: /* @__PURE__ */ jsx(StateProvider, {
|
|
40
|
+
value: stateRef.current,
|
|
41
|
+
children
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
const useCollection = (selector = identity) => useContextSelector(Context, selector);
|
|
46
|
+
const useResource = (id, selector = identity) => useContextSelector(Context, (s) => selector(s[id] || getResourceInitialState()));
|
|
47
|
+
const useResourceValue = (id, fieldKey) => useResource(id, (s) => s[fieldKey]);
|
|
48
|
+
const useCollectionDispatch = () => useContextSelector(DispatchContext);
|
|
49
|
+
const useResourceDispatch = (id) => {
|
|
50
|
+
const dispatch = useCollectionDispatch();
|
|
51
|
+
return useCallback((action) => dispatch((state) => ({
|
|
52
|
+
...state,
|
|
53
|
+
[id]: action(state[id] || getResourceInitialState())
|
|
54
|
+
})), [id]);
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
Provider: memo(Provider),
|
|
58
|
+
useCollection,
|
|
59
|
+
useResource,
|
|
60
|
+
useResourceValue,
|
|
61
|
+
useCollectionDispatch,
|
|
62
|
+
useResourceDispatch
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
export { makeProvider };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let _cybertools_utils = require("@cybertools/utils");
|
|
3
|
+
let es_toolkit_compat = require("es-toolkit/compat");
|
|
4
|
+
//#region src/useContextSelector.ts
|
|
5
|
+
const useIsomorphicLayoutEffect = typeof window === "undefined" || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent) ? react.useEffect : react.useLayoutEffect;
|
|
6
|
+
const isEqual = (prev, next) => {
|
|
7
|
+
if (typeof prev === "function" && typeof next === "function") return prev.toString() === next.toString();
|
|
8
|
+
if (Object.is(prev, next)) return true;
|
|
9
|
+
return (0, es_toolkit_compat.isEqual)(prev, next);
|
|
10
|
+
};
|
|
11
|
+
function useContextSelector(context, selector = _cybertools_utils.identity) {
|
|
12
|
+
const { value, version, listeners } = (0, react.useContext)(context);
|
|
13
|
+
const [state, setState] = (0, react.useState)(() => [value.current, selector(value.current)]);
|
|
14
|
+
const selectorRef = (0, react.useRef)(selector);
|
|
15
|
+
selectorRef.current = selector;
|
|
16
|
+
const dispatch = (0, react.useCallback)((next) => {
|
|
17
|
+
setState((prev) => {
|
|
18
|
+
if (version.current === next[0]) {
|
|
19
|
+
const selected = selectorRef.current(next[1]);
|
|
20
|
+
if (isEqual(prev[1], selected)) return prev;
|
|
21
|
+
return [next[1], selected];
|
|
22
|
+
}
|
|
23
|
+
if (next.length === 2) {
|
|
24
|
+
if (isEqual(prev[0], next[1])) return prev;
|
|
25
|
+
const nextSelected = selectorRef.current(next[1]);
|
|
26
|
+
if (isEqual(prev[1], nextSelected)) return prev;
|
|
27
|
+
return [next[1], nextSelected];
|
|
28
|
+
}
|
|
29
|
+
return [...prev];
|
|
30
|
+
});
|
|
31
|
+
}, []);
|
|
32
|
+
useIsomorphicLayoutEffect(() => {
|
|
33
|
+
listeners.add(dispatch);
|
|
34
|
+
return () => {
|
|
35
|
+
listeners.delete(dispatch);
|
|
36
|
+
};
|
|
37
|
+
}, [listeners]);
|
|
38
|
+
const selected = selector(value.current);
|
|
39
|
+
if (!isEqual(state[1], selected)) {
|
|
40
|
+
setState([value.current, selected]);
|
|
41
|
+
return selected;
|
|
42
|
+
}
|
|
43
|
+
return state[1];
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.useContextSelector = useContextSelector;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { identity } from "@cybertools/utils";
|
|
3
|
+
import { isEqual } from "es-toolkit/compat";
|
|
4
|
+
//#region src/useContextSelector.ts
|
|
5
|
+
const useIsomorphicLayoutEffect = typeof window === "undefined" || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent) ? useEffect : useLayoutEffect;
|
|
6
|
+
const isEqual$1 = (prev, next) => {
|
|
7
|
+
if (typeof prev === "function" && typeof next === "function") return prev.toString() === next.toString();
|
|
8
|
+
if (Object.is(prev, next)) return true;
|
|
9
|
+
return isEqual(prev, next);
|
|
10
|
+
};
|
|
11
|
+
function useContextSelector(context, selector = identity) {
|
|
12
|
+
const { value, version, listeners } = useContext(context);
|
|
13
|
+
const [state, setState] = useState(() => [value.current, selector(value.current)]);
|
|
14
|
+
const selectorRef = useRef(selector);
|
|
15
|
+
selectorRef.current = selector;
|
|
16
|
+
const dispatch = useCallback((next) => {
|
|
17
|
+
setState((prev) => {
|
|
18
|
+
if (version.current === next[0]) {
|
|
19
|
+
const selected = selectorRef.current(next[1]);
|
|
20
|
+
if (isEqual$1(prev[1], selected)) return prev;
|
|
21
|
+
return [next[1], selected];
|
|
22
|
+
}
|
|
23
|
+
if (next.length === 2) {
|
|
24
|
+
if (isEqual$1(prev[0], next[1])) return prev;
|
|
25
|
+
const nextSelected = selectorRef.current(next[1]);
|
|
26
|
+
if (isEqual$1(prev[1], nextSelected)) return prev;
|
|
27
|
+
return [next[1], nextSelected];
|
|
28
|
+
}
|
|
29
|
+
return [...prev];
|
|
30
|
+
});
|
|
31
|
+
}, []);
|
|
32
|
+
useIsomorphicLayoutEffect(() => {
|
|
33
|
+
listeners.add(dispatch);
|
|
34
|
+
return () => {
|
|
35
|
+
listeners.delete(dispatch);
|
|
36
|
+
};
|
|
37
|
+
}, [listeners]);
|
|
38
|
+
const selected = selector(value.current);
|
|
39
|
+
if (!isEqual$1(state[1], selected)) {
|
|
40
|
+
setState([value.current, selected]);
|
|
41
|
+
return selected;
|
|
42
|
+
}
|
|
43
|
+
return state[1];
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { useContextSelector };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
//#region src/useFetchFnPromise.ts
|
|
3
|
+
function useFetchFnPromise({ action, initialData, onSuccess, onError, sentry = false }, dependencies) {
|
|
4
|
+
const [loading, setLoading] = (0, react.useState)(false);
|
|
5
|
+
const [error, setError] = (0, react.useState)();
|
|
6
|
+
const [data, setData] = (0, react.useState)(initialData);
|
|
7
|
+
return [
|
|
8
|
+
(0, react.useCallback)((...args) => {
|
|
9
|
+
setLoading(true);
|
|
10
|
+
return action(...args).then((response) => {
|
|
11
|
+
const payload = response?.data || response;
|
|
12
|
+
setError(void 0);
|
|
13
|
+
setData(payload);
|
|
14
|
+
setLoading(false);
|
|
15
|
+
if (onSuccess) onSuccess(payload);
|
|
16
|
+
return payload;
|
|
17
|
+
}).catch((e) => {
|
|
18
|
+
setError(e);
|
|
19
|
+
setData(initialData);
|
|
20
|
+
setLoading(false);
|
|
21
|
+
if (onError) onError(e);
|
|
22
|
+
if (sentry) import("@sentry/react").then(({ captureException }) => captureException(e));
|
|
23
|
+
});
|
|
24
|
+
}, dependencies),
|
|
25
|
+
data,
|
|
26
|
+
loading,
|
|
27
|
+
error
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.useFetchFnPromise = useFetchFnPromise;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DependencyList } from "react";
|
|
2
|
+
//#region src/useFetchFnPromise.d.ts
|
|
3
|
+
export interface UseFetchFnPromiseOptions<TArgs extends unknown[], TData> {
|
|
4
|
+
action: (...args: TArgs) => Promise<unknown>;
|
|
5
|
+
initialData?: TData;
|
|
6
|
+
onSuccess?: (payload: TData) => void;
|
|
7
|
+
onError?: (error: unknown) => void;
|
|
8
|
+
sentry?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function useFetchFnPromise<TArgs extends unknown[], TData = unknown>({ action, initialData, onSuccess, onError, sentry }: UseFetchFnPromiseOptions<TArgs, TData>, dependencies?: DependencyList): [(...args: TArgs) => Promise<TData | undefined>, TData | undefined, boolean, unknown];
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DependencyList } from "react";
|
|
2
|
+
//#region src/useFetchFnPromise.d.ts
|
|
3
|
+
export interface UseFetchFnPromiseOptions<TArgs extends unknown[], TData> {
|
|
4
|
+
action: (...args: TArgs) => Promise<unknown>;
|
|
5
|
+
initialData?: TData;
|
|
6
|
+
onSuccess?: (payload: TData) => void;
|
|
7
|
+
onError?: (error: unknown) => void;
|
|
8
|
+
sentry?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function useFetchFnPromise<TArgs extends unknown[], TData = unknown>({ action, initialData, onSuccess, onError, sentry }: UseFetchFnPromiseOptions<TArgs, TData>, dependencies?: DependencyList): [(...args: TArgs) => Promise<TData | undefined>, TData | undefined, boolean, unknown];
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
//#region src/useFetchFnPromise.ts
|
|
3
|
+
function useFetchFnPromise({ action, initialData, onSuccess, onError, sentry = false }, dependencies) {
|
|
4
|
+
const [loading, setLoading] = useState(false);
|
|
5
|
+
const [error, setError] = useState();
|
|
6
|
+
const [data, setData] = useState(initialData);
|
|
7
|
+
return [
|
|
8
|
+
useCallback((...args) => {
|
|
9
|
+
setLoading(true);
|
|
10
|
+
return action(...args).then((response) => {
|
|
11
|
+
const payload = response?.data || response;
|
|
12
|
+
setError(void 0);
|
|
13
|
+
setData(payload);
|
|
14
|
+
setLoading(false);
|
|
15
|
+
if (onSuccess) onSuccess(payload);
|
|
16
|
+
return payload;
|
|
17
|
+
}).catch((e) => {
|
|
18
|
+
setError(e);
|
|
19
|
+
setData(initialData);
|
|
20
|
+
setLoading(false);
|
|
21
|
+
if (onError) onError(e);
|
|
22
|
+
if (sentry) import("@sentry/react").then(({ captureException }) => captureException(e));
|
|
23
|
+
});
|
|
24
|
+
}, dependencies),
|
|
25
|
+
data,
|
|
26
|
+
loading,
|
|
27
|
+
error
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { useFetchFnPromise };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
//#region src/useFetchPromise.ts
|
|
3
|
+
function useFetchPromise({ action, enabled = true, initialData, onSuccess }, dependencies) {
|
|
4
|
+
const [loaded, setLoaded] = (0, react.useState)(false);
|
|
5
|
+
const [loading, setLoading] = (0, react.useState)(true);
|
|
6
|
+
const [data, setData] = (0, react.useState)(initialData);
|
|
7
|
+
const refresh = (0, react.useCallback)(() => {
|
|
8
|
+
if (!enabled) return;
|
|
9
|
+
setLoading(true);
|
|
10
|
+
action().then((response) => {
|
|
11
|
+
setData(response);
|
|
12
|
+
setLoaded(true);
|
|
13
|
+
if (onSuccess) onSuccess(response);
|
|
14
|
+
}).finally(() => {
|
|
15
|
+
setLoading(false);
|
|
16
|
+
});
|
|
17
|
+
}, dependencies);
|
|
18
|
+
(0, react.useEffect)(() => {
|
|
19
|
+
refresh();
|
|
20
|
+
}, dependencies);
|
|
21
|
+
return [
|
|
22
|
+
data,
|
|
23
|
+
loaded,
|
|
24
|
+
loading,
|
|
25
|
+
refresh
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
function useFetchWithErrorPromise({ action, enabled = true, initialData, onSuccess, onFail }, dependencies) {
|
|
29
|
+
const [loaded, setLoaded] = (0, react.useState)(false);
|
|
30
|
+
const [loading, setLoading] = (0, react.useState)(true);
|
|
31
|
+
const [data, setData] = (0, react.useState)(initialData);
|
|
32
|
+
const [error, setError] = (0, react.useState)(null);
|
|
33
|
+
const refresh = (0, react.useCallback)(() => {
|
|
34
|
+
if (!enabled) return;
|
|
35
|
+
setLoading(true);
|
|
36
|
+
action().then((response) => {
|
|
37
|
+
setError(null);
|
|
38
|
+
setData(response);
|
|
39
|
+
setLoaded(true);
|
|
40
|
+
if (onSuccess) onSuccess(response);
|
|
41
|
+
}).catch((error) => {
|
|
42
|
+
setError(error);
|
|
43
|
+
if (onFail) onFail(error);
|
|
44
|
+
}).finally(() => {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
});
|
|
47
|
+
}, dependencies);
|
|
48
|
+
(0, react.useEffect)(() => {
|
|
49
|
+
refresh();
|
|
50
|
+
}, dependencies);
|
|
51
|
+
return {
|
|
52
|
+
data,
|
|
53
|
+
loaded,
|
|
54
|
+
loading,
|
|
55
|
+
error,
|
|
56
|
+
refresh
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
exports.useFetchPromise = useFetchPromise;
|
|
61
|
+
exports.useFetchWithErrorPromise = useFetchWithErrorPromise;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DependencyList } from "react";
|
|
2
|
+
//#region src/useFetchPromise.d.ts
|
|
3
|
+
export interface UseFetchPromiseOptions<T> {
|
|
4
|
+
action: () => Promise<T>;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
initialData?: T;
|
|
7
|
+
onSuccess?: (data: T) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function useFetchPromise<T>({ action, enabled, initialData, onSuccess }: UseFetchPromiseOptions<T>, dependencies?: DependencyList): [T | undefined, boolean, boolean, () => void];
|
|
10
|
+
export declare function useFetchWithErrorPromise<T>({ action, enabled, initialData, onSuccess, onFail }: UseFetchPromiseOptions<T> & {
|
|
11
|
+
onFail?: (error: unknown) => void;
|
|
12
|
+
}, dependencies?: DependencyList): {
|
|
13
|
+
data: T | undefined;
|
|
14
|
+
loaded: boolean;
|
|
15
|
+
loading: boolean;
|
|
16
|
+
error: unknown;
|
|
17
|
+
refresh: () => void;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DependencyList } from "react";
|
|
2
|
+
//#region src/useFetchPromise.d.ts
|
|
3
|
+
export interface UseFetchPromiseOptions<T> {
|
|
4
|
+
action: () => Promise<T>;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
initialData?: T;
|
|
7
|
+
onSuccess?: (data: T) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function useFetchPromise<T>({ action, enabled, initialData, onSuccess }: UseFetchPromiseOptions<T>, dependencies?: DependencyList): [T | undefined, boolean, boolean, () => void];
|
|
10
|
+
export declare function useFetchWithErrorPromise<T>({ action, enabled, initialData, onSuccess, onFail }: UseFetchPromiseOptions<T> & {
|
|
11
|
+
onFail?: (error: unknown) => void;
|
|
12
|
+
}, dependencies?: DependencyList): {
|
|
13
|
+
data: T | undefined;
|
|
14
|
+
loaded: boolean;
|
|
15
|
+
loading: boolean;
|
|
16
|
+
error: unknown;
|
|
17
|
+
refresh: () => void;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
//#region src/useFetchPromise.ts
|
|
3
|
+
function useFetchPromise({ action, enabled = true, initialData, onSuccess }, dependencies) {
|
|
4
|
+
const [loaded, setLoaded] = useState(false);
|
|
5
|
+
const [loading, setLoading] = useState(true);
|
|
6
|
+
const [data, setData] = useState(initialData);
|
|
7
|
+
const refresh = useCallback(() => {
|
|
8
|
+
if (!enabled) return;
|
|
9
|
+
setLoading(true);
|
|
10
|
+
action().then((response) => {
|
|
11
|
+
setData(response);
|
|
12
|
+
setLoaded(true);
|
|
13
|
+
if (onSuccess) onSuccess(response);
|
|
14
|
+
}).finally(() => {
|
|
15
|
+
setLoading(false);
|
|
16
|
+
});
|
|
17
|
+
}, dependencies);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
refresh();
|
|
20
|
+
}, dependencies);
|
|
21
|
+
return [
|
|
22
|
+
data,
|
|
23
|
+
loaded,
|
|
24
|
+
loading,
|
|
25
|
+
refresh
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
function useFetchWithErrorPromise({ action, enabled = true, initialData, onSuccess, onFail }, dependencies) {
|
|
29
|
+
const [loaded, setLoaded] = useState(false);
|
|
30
|
+
const [loading, setLoading] = useState(true);
|
|
31
|
+
const [data, setData] = useState(initialData);
|
|
32
|
+
const [error, setError] = useState(null);
|
|
33
|
+
const refresh = useCallback(() => {
|
|
34
|
+
if (!enabled) return;
|
|
35
|
+
setLoading(true);
|
|
36
|
+
action().then((response) => {
|
|
37
|
+
setError(null);
|
|
38
|
+
setData(response);
|
|
39
|
+
setLoaded(true);
|
|
40
|
+
if (onSuccess) onSuccess(response);
|
|
41
|
+
}).catch((error) => {
|
|
42
|
+
setError(error);
|
|
43
|
+
if (onFail) onFail(error);
|
|
44
|
+
}).finally(() => {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
});
|
|
47
|
+
}, dependencies);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
refresh();
|
|
50
|
+
}, dependencies);
|
|
51
|
+
return {
|
|
52
|
+
data,
|
|
53
|
+
loaded,
|
|
54
|
+
loading,
|
|
55
|
+
error,
|
|
56
|
+
refresh
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { useFetchPromise, useFetchWithErrorPromise };
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cybertools/react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React state factory (makeProvider), data-fetching hooks and ErrorBoundary for Cyberscope apps.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/cyberscope-io/cybertools.git",
|
|
9
|
+
"directory": "packages/react"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@cybertools/utils": "^1.0.0",
|
|
21
|
+
"es-toolkit": "^1.52.0",
|
|
22
|
+
"scheduler": "^0.23.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@sentry/react": ">=7.56.0",
|
|
26
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"@sentry/react": {
|
|
30
|
+
"optional": true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
35
|
+
"@cybertools/eslint-config": "0.0.0",
|
|
36
|
+
"@cybertools/tsconfig": "0.0.0",
|
|
37
|
+
"@sentry/react": "^11.4.0",
|
|
38
|
+
"@testing-library/dom": "^10.4.2",
|
|
39
|
+
"@testing-library/react": "^16.3.3",
|
|
40
|
+
"@types/node": "^24.19.1",
|
|
41
|
+
"@types/react": "^19.3.0",
|
|
42
|
+
"@types/react-dom": "^19.3.0",
|
|
43
|
+
"@types/scheduler": "^0.28.0",
|
|
44
|
+
"eslint": "^10.12.0",
|
|
45
|
+
"jsdom": "^30.1.2",
|
|
46
|
+
"publint": "^0.3.25",
|
|
47
|
+
"react": "^19.3.0",
|
|
48
|
+
"react-dom": "^19.3.0",
|
|
49
|
+
"tsdown": "0.23.0",
|
|
50
|
+
"typescript": "6.0.3",
|
|
51
|
+
"vite": "^8.3.2",
|
|
52
|
+
"vitest": "^5.0.3"
|
|
53
|
+
},
|
|
54
|
+
"main": "./dist/index.cjs",
|
|
55
|
+
"module": "./dist/index.mjs",
|
|
56
|
+
"types": "./dist/index.d.cts",
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"import": "./dist/index.mjs",
|
|
60
|
+
"require": "./dist/index.cjs"
|
|
61
|
+
},
|
|
62
|
+
"./package.json": "./package.json"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsdown",
|
|
66
|
+
"typecheck": "tsc --noEmit",
|
|
67
|
+
"lint": "eslint .",
|
|
68
|
+
"check:package": "publint && attw --pack . --profile node16",
|
|
69
|
+
"test": "vitest run"
|
|
70
|
+
}
|
|
71
|
+
}
|