@plasius/react-state 1.0.10 → 1.0.12
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/index.cjs +224 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +60 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +191 -0
- package/dist/index.js.map +1 -0
- package/package.json +18 -6
- package/.eslintrc.cjs +0 -7
- package/.github/workflows/cd.yml +0 -186
- package/.github/workflows/ci.yml +0 -16
- package/.nvmrc +0 -1
- package/.vscode/launch.json +0 -15
- package/CHANGELOG.md +0 -72
- package/CODE_OF_CONDUCT.md +0 -79
- package/CONTRIBUTING.md +0 -201
- package/CONTRIBUTORS.md +0 -27
- package/SECURITY.md +0 -17
- package/docs/adrs/adr-0001: React store.md +0 -42
- package/docs/adrs/adr-template.md +0 -65
- package/legal/CLA-REGISTRY.csv +0 -2
- package/legal/CLA.md +0 -22
- package/legal/CORPORATE_CLA.md +0 -57
- package/legal/INDIVIDUAL_CLA.md +0 -91
- package/sbom.cdx.json +0 -66
- package/src/create-scoped-store.tsx +0 -87
- package/src/index.ts +0 -5
- package/src/metadata-store.ts +0 -24
- package/src/provider.tsx +0 -52
- package/src/store.ts +0 -131
- package/src/types.ts +0 -3
- package/tests/scoped.store.test.tsx +0 -75
- package/tests/store.core.test.ts +0 -295
- package/tsconfig.build.json +0 -20
- package/tsconfig.json +0 -7
- package/tsup.config.ts +0 -10
- package/vitest.config.js +0 -20
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# Architectural Decision Record (ADR)
|
|
2
|
-
|
|
3
|
-
## Title
|
|
4
|
-
|
|
5
|
-
> _Concise, descriptive title of the decision (e.g., “Use Azure Container Apps for n8n Deployment”)_
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## Status
|
|
10
|
-
|
|
11
|
-
- Proposed | Accepted | Rejected | Superseded | Deprecated
|
|
12
|
-
- Date: YYYY-MM-DD
|
|
13
|
-
- Version: 1.0
|
|
14
|
-
- Supersedes: ADR-XXXX (if applicable)
|
|
15
|
-
- Superseded by: ADR-YYYY (if applicable)
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## Tags
|
|
20
|
-
|
|
21
|
-
> _Short keywords to help search and group ADRs (e.g., infra, frontend, security, devops, ai, database)._
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
## Context
|
|
26
|
-
|
|
27
|
-
> _Describe the problem we are solving, relevant background, and constraints.
|
|
28
|
-
> Why are we making this decision? What triggered it?_
|
|
29
|
-
|
|
30
|
-
---
|
|
31
|
-
|
|
32
|
-
## Decision
|
|
33
|
-
|
|
34
|
-
> _What is the decision we have made? Clear, affirmative statement of the chosen path._
|
|
35
|
-
|
|
36
|
-
---
|
|
37
|
-
|
|
38
|
-
## Alternatives Considered
|
|
39
|
-
|
|
40
|
-
- **Option A**: Description (pros/cons)
|
|
41
|
-
- **Option B**: Description (pros/cons)
|
|
42
|
-
- **Option C**: Description (pros/cons)
|
|
43
|
-
|
|
44
|
-
> _Why were these alternatives not chosen?_
|
|
45
|
-
|
|
46
|
-
---
|
|
47
|
-
|
|
48
|
-
## Consequences
|
|
49
|
-
|
|
50
|
-
- Positive outcomes (benefits, opportunities)
|
|
51
|
-
- Negative outcomes (risks, trade-offs)
|
|
52
|
-
- Any technical debt created or avoided
|
|
53
|
-
- Impact on future decisions
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## Related Decisions
|
|
58
|
-
|
|
59
|
-
> _Link to related ADRs (if any)_
|
|
60
|
-
|
|
61
|
-
---
|
|
62
|
-
|
|
63
|
-
## References
|
|
64
|
-
|
|
65
|
-
> _Links to docs, benchmarks, discussions, or external resources that influenced this decision_
|
package/legal/CLA-REGISTRY.csv
DELETED
package/legal/CLA.md
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# Contributor License Agreements (CLA)
|
|
2
|
-
|
|
3
|
-
To protect the intellectual property of this project and ensure clarity of rights, all contributors must sign a Contributor License Agreement (CLA) before their first contribution.
|
|
4
|
-
|
|
5
|
-
## Which CLA should I sign?
|
|
6
|
-
|
|
7
|
-
- **Individual CLA**: If you are contributing personally and not on behalf of an employer, sign the [Individual CLA](INDIVIDUAL_CLA.md).
|
|
8
|
-
- **Corporate CLA**: If you are contributing as part of your work for a company, the company should sign the [Corporate CLA](CORPORATE_CLA.md).
|
|
9
|
-
|
|
10
|
-
## How to sign
|
|
11
|
-
|
|
12
|
-
1. Download the appropriate CLA file (Individual or Corporate).
|
|
13
|
-
2. Fill in the required details, sign, and date it.
|
|
14
|
-
3. Email a PDF copy of the signed document to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject: `CLA – Individual` or `CLA – Corporate`.
|
|
15
|
-
|
|
16
|
-
## Registry
|
|
17
|
-
|
|
18
|
-
All signed CLAs are logged internally in the CLA registry (`CLA-REGISTRY.csv`).
|
|
19
|
-
|
|
20
|
-
## Questions?
|
|
21
|
-
|
|
22
|
-
If you have any questions about which CLA to sign or how the process works, please email **[contributors@plasius.co.uk](mailtocontributors@plasius.co.uk)**.
|
package/legal/CORPORATE_CLA.md
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# Corporate Contributor License Agreement (CLA)
|
|
2
|
-
|
|
3
|
-
## Purpose
|
|
4
|
-
|
|
5
|
-
This Corporate Contributor License Agreement ("Agreement") is intended to protect the intellectual property rights of the contributors and the project, ensure clear licensing terms for contributions, and maintain trust within the community. By signing this Agreement, the corporation agrees to the terms that facilitate the use, distribution, and modification of contributions under the project's licensing framework.
|
|
6
|
-
|
|
7
|
-
## Agreement
|
|
8
|
-
|
|
9
|
-
1. **Representation of Authority**
|
|
10
|
-
The undersigned individual represents and warrants that they have the full legal authority to enter into this Agreement on behalf of the corporation named below ("Corporation") and to grant the rights contained herein.
|
|
11
|
-
|
|
12
|
-
2. **Grant of Copyright License**
|
|
13
|
-
The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the contributions submitted to the project.
|
|
14
|
-
|
|
15
|
-
3. **Grant of Patent License**
|
|
16
|
-
The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under any patent claims that are necessarily infringed by the contributions to make, use, sell, offer for sale, import, and otherwise dispose of the contributions or derivative works thereof.
|
|
17
|
-
|
|
18
|
-
4. **Warranties and Representations**
|
|
19
|
-
The Corporation represents and warrants that:
|
|
20
|
-
|
|
21
|
-
- The contributions are the original work of the Corporation or that the Corporation has sufficient rights to grant the licenses herein.
|
|
22
|
-
- The submission of the contributions does not violate any agreements or rights of third parties.
|
|
23
|
-
|
|
24
|
-
5. **No Revocation**
|
|
25
|
-
This license is granted on a perpetual basis and cannot be revoked, provided that the terms of this Agreement are met.
|
|
26
|
-
|
|
27
|
-
6. **Governing Law**
|
|
28
|
-
This Agreement shall be governed by and construed in accordance with the laws of the United Kingdom, without regard to its conflict of laws principles.
|
|
29
|
-
|
|
30
|
-
7. **Execution**
|
|
31
|
-
|
|
32
|
-
This Agreement is effective upon signature by the authorized representative of the Corporation. Please sign and date this document, then email a scanned PDF copy to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
|
|
33
|
-
|
|
34
|
-
---
|
|
35
|
-
|
|
36
|
-
**@plasius/react-state**
|
|
37
|
-
|
|
38
|
-
**Corporation Legal Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
39
|
-
|
|
40
|
-
**Authorized Representative:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
41
|
-
|
|
42
|
-
**Title:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
43
|
-
|
|
44
|
-
**Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
45
|
-
|
|
46
|
-
**Date:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
47
|
-
|
|
48
|
-
**Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
49
|
-
|
|
50
|
-
---
|
|
51
|
-
|
|
52
|
-
## How to Sign
|
|
53
|
-
|
|
54
|
-
- Download this file as a template.
|
|
55
|
-
- Fill in the Corporation’s legal name, authorized representative, title, email, date, and provide a signature.
|
|
56
|
-
- Sign and date the document.
|
|
57
|
-
- Send a scanned copy of the signed Agreement to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
|
package/legal/INDIVIDUAL_CLA.md
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# Individual Contributor License Agreement (CLA)
|
|
2
|
-
|
|
3
|
-
**Project:** @plasius/react-state (Plasius LTD)
|
|
4
|
-
**Version:** 1.0 — 2025‑09‑12
|
|
5
|
-
**Contact:** [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 1. Definitions
|
|
10
|
-
|
|
11
|
-
- **"You"** (or **"Contributor"**) means the individual signing this CLA and submitting Contributions to the Project.
|
|
12
|
-
- **"Contribution"** means any original work of authorship, including code, documentation, data, designs, or feedback that You submit to the Project in any form (e.g., pull request, issue comment, email, file upload).
|
|
13
|
-
- **"Project"** means the Intuitive Purchases repositories and related materials owned or managed by **Plasius LTD**.
|
|
14
|
-
|
|
15
|
-
## 2. Copyright License Grant
|
|
16
|
-
|
|
17
|
-
You hereby grant to Plasius LTD a **perpetual, worldwide, non‑exclusive, transferable, sublicensable, royalty‑free, irrevocable** copyright license to:
|
|
18
|
-
|
|
19
|
-
- use, reproduce, publicly display, publicly perform, modify, create derivative works of, and
|
|
20
|
-
- distribute Contributions in source and object form,
|
|
21
|
-
- and to **sublicense** these rights under any terms Plasius LTD chooses, including proprietary or open‑source licenses.
|
|
22
|
-
|
|
23
|
-
## 3. Patent License Grant
|
|
24
|
-
|
|
25
|
-
You hereby grant to Plasius LTD and its sublicensees a **perpetual, worldwide, non‑exclusive, transferable, royalty‑free, irrevocable** patent license to **make, have made, use, offer to sell, sell, import, and otherwise transfer** the Contribution and derivative works thereof, where such license applies only to patent claims that You **own or control** and that would be infringed by Your Contribution or its combination with the Project.
|
|
26
|
-
|
|
27
|
-
## 4. Moral Rights & Attribution
|
|
28
|
-
|
|
29
|
-
To the maximum extent permitted by applicable law, You **waive** and agree not to assert any moral rights (e.g., rights of attribution or integrity) in or to the Contribution against Plasius LTD. Plasius LTD may, but is not required to, credit You.
|
|
30
|
-
|
|
31
|
-
## 5. Representations & Warranties
|
|
32
|
-
|
|
33
|
-
You represent that:
|
|
34
|
-
|
|
35
|
-
1. **Originality / Rights:** Each Contribution is Your original creation, or You have sufficient rights to submit it and grant the licenses above.
|
|
36
|
-
2. **No Confidential Info:** Contributions **do not** include confidential information or trade secrets of any third party.
|
|
37
|
-
3. **No Infringement:** To the best of Your knowledge, Contributions do not infringe any third‑party IP rights.
|
|
38
|
-
4. **Employment / Contractor Status:** If Your employer or a third party might claim rights in Your Contribution, You have obtained **written permission** to make the Contribution and grant these licenses (attach or reference below), or Your Contribution is made **outside the scope** of your employment and without using your employer’s confidential information or resources.
|
|
39
|
-
5. **Compliance:** You will follow the Project’s policies (e.g., Code of Conduct, Security Policy) and applicable laws.
|
|
40
|
-
|
|
41
|
-
## 6. Third‑Party Code
|
|
42
|
-
|
|
43
|
-
If Your Contribution includes code, data, or other material from a third party, You will **identify the material and its license** in the pull request or submission, and ensure it is **compatible** with the Project’s licensing model. You will not submit material subject to terms that require the Project to disclose proprietary source code (e.g., certain copyleft obligations) unless the Project has **pre‑approved** such inclusion in writing.
|
|
44
|
-
|
|
45
|
-
## 7. Scope & Duration
|
|
46
|
-
|
|
47
|
-
- This CLA covers **all past and future** Contributions You submit to the Project, unless and until You provide written notice to **revoke** it.
|
|
48
|
-
- Revocation is **not retroactive**: rights granted for prior Contributions remain in effect.
|
|
49
|
-
|
|
50
|
-
## 8. Disclaimer
|
|
51
|
-
|
|
52
|
-
THE CONTRIBUTION IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON‑INFRINGEMENT.
|
|
53
|
-
|
|
54
|
-
## 9. Governing Law & Jurisdiction
|
|
55
|
-
|
|
56
|
-
This CLA is governed by the **laws of England and Wales**, and the courts of England and Wales shall have **exclusive jurisdiction** over any dispute arising out of or relating to it.
|
|
57
|
-
|
|
58
|
-
## 10. Entire Agreement
|
|
59
|
-
|
|
60
|
-
This CLA is the entire agreement between You and Plasius LTD regarding Contributions. It supersedes any prior discussions relating to Contributions. If any provision is held unenforceable, the remaining provisions remain in full force.
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## 11. Contributor Information & Signature
|
|
65
|
-
|
|
66
|
-
By signing below, You agree to the terms of this CLA for Your Contributions to the Project.
|
|
67
|
-
|
|
68
|
-
**Full Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
69
|
-
|
|
70
|
-
**Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
71
|
-
|
|
72
|
-
**GitHub Handle:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
73
|
-
|
|
74
|
-
**Address (optional):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
75
|
-
|
|
76
|
-
**Employer (if applicable):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
77
|
-
|
|
78
|
-
**If employed:** ☐ I confirm Contributions are made outside the scope of employment **or** ☐ I have attached my employer’s written permission.
|
|
79
|
-
|
|
80
|
-
**Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
81
|
-
|
|
82
|
-
**Date (YYYY‑MM‑DD):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
83
|
-
|
|
84
|
-
_Electronic signatures are accepted. You may type your name in the Signature field and email a PDF copy._
|
|
85
|
-
|
|
86
|
-
**Submission:** Please email the signed CLA to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject line: `CLA – Individual – <GitHubHandle>`.
|
|
87
|
-
|
|
88
|
-
**(Optional) Attachments / Notes:**
|
|
89
|
-
|
|
90
|
-
- Employer permission letter (if required)
|
|
91
|
-
- Third‑party license disclosures (if any)
|
package/sbom.cdx.json
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
|
|
3
|
-
"bomFormat": "CycloneDX",
|
|
4
|
-
"specVersion": "1.5",
|
|
5
|
-
"serialNumber": "urn:uuid:f0ed2ffb-226d-413f-a182-36e60e9b39c6",
|
|
6
|
-
"version": 1,
|
|
7
|
-
"metadata": {
|
|
8
|
-
"timestamp": "2025-09-17T16:16:53.742Z",
|
|
9
|
-
"lifecycles": [
|
|
10
|
-
{
|
|
11
|
-
"phase": "build"
|
|
12
|
-
}
|
|
13
|
-
],
|
|
14
|
-
"tools": [
|
|
15
|
-
{
|
|
16
|
-
"vendor": "npm",
|
|
17
|
-
"name": "cli",
|
|
18
|
-
"version": "10.9.3"
|
|
19
|
-
}
|
|
20
|
-
],
|
|
21
|
-
"component": {
|
|
22
|
-
"bom-ref": "@plasius/react-state@1.0.10",
|
|
23
|
-
"type": "library",
|
|
24
|
-
"name": "react-state",
|
|
25
|
-
"version": "1.0.10",
|
|
26
|
-
"scope": "required",
|
|
27
|
-
"author": "Plasius LTD",
|
|
28
|
-
"description": "Tiny, testable, typesafe React Scoped Store helper.",
|
|
29
|
-
"purl": "pkg:npm/%40plasius/react-state@1.0.10",
|
|
30
|
-
"properties": [
|
|
31
|
-
{
|
|
32
|
-
"name": "cdx:npm:package:path",
|
|
33
|
-
"value": ""
|
|
34
|
-
}
|
|
35
|
-
],
|
|
36
|
-
"externalReferences": [
|
|
37
|
-
{
|
|
38
|
-
"type": "vcs",
|
|
39
|
-
"url": "git+https://github.com/Plasius-LTD/react-state.git"
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
"type": "website",
|
|
43
|
-
"url": "https://github.com/Plasius-LTD/react-state#readme"
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
"type": "issue-tracker",
|
|
47
|
-
"url": "https://github.com/Plasius-LTD/react-state/issues"
|
|
48
|
-
}
|
|
49
|
-
],
|
|
50
|
-
"licenses": [
|
|
51
|
-
{
|
|
52
|
-
"license": {
|
|
53
|
-
"id": "Apache-2.0"
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
]
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
"components": [],
|
|
60
|
-
"dependencies": [
|
|
61
|
-
{
|
|
62
|
-
"ref": "@plasius/react-state@1.0.10",
|
|
63
|
-
"dependsOn": []
|
|
64
|
-
}
|
|
65
|
-
]
|
|
66
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext, useRef, useSyncExternalStore } from "react";
|
|
2
|
-
import type { IState, IAction, Store } from "./store.js";
|
|
3
|
-
import { createStore } from "./store.js";
|
|
4
|
-
|
|
5
|
-
function shallowEqual(a: any, b: any) {
|
|
6
|
-
if (Object.is(a, b)) return true;
|
|
7
|
-
if (
|
|
8
|
-
typeof a !== "object" ||
|
|
9
|
-
a === null ||
|
|
10
|
-
typeof b !== "object" ||
|
|
11
|
-
b === null
|
|
12
|
-
)
|
|
13
|
-
return false;
|
|
14
|
-
const ak = Object.keys(a),
|
|
15
|
-
bk = Object.keys(b);
|
|
16
|
-
if (ak.length !== bk.length) return false;
|
|
17
|
-
for (let i = 0; i < ak.length; i++) {
|
|
18
|
-
const k = ak[i] as string;
|
|
19
|
-
if (
|
|
20
|
-
!Object.prototype.hasOwnProperty.call(b, k) ||
|
|
21
|
-
!Object.is((a as any)[k], (b as any)[k])
|
|
22
|
-
)
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function createScopedStoreContext<S extends IState, A extends IAction>(
|
|
29
|
-
reducer: (state: S, action: A) => S,
|
|
30
|
-
initialState: S
|
|
31
|
-
) {
|
|
32
|
-
const Context = createContext<Store<S, A> | null>(null);
|
|
33
|
-
|
|
34
|
-
const store = createStore(reducer, initialState);
|
|
35
|
-
|
|
36
|
-
const Provider = ({ children }: { children: React.ReactNode }) => (
|
|
37
|
-
<Context.Provider value={store}>{children}</Context.Provider>
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const useStore = (): S => {
|
|
41
|
-
const ctx = useContext(Context);
|
|
42
|
-
if (!ctx) throw new Error("Store not found in context");
|
|
43
|
-
return useSyncExternalStore(ctx.subscribe, ctx.getState, ctx.getState);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const useDispatch = (): ((action: A) => void) => {
|
|
47
|
-
const ctx = useContext(Context);
|
|
48
|
-
if (!ctx) throw new Error("Dispatch not found in context");
|
|
49
|
-
return (action: A) => ctx.dispatch(action);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
function useSelector<T>(
|
|
53
|
-
selector: (state: S) => T,
|
|
54
|
-
isEqual: (a: T, b: T) => boolean = shallowEqual
|
|
55
|
-
): T {
|
|
56
|
-
const ctx = useContext(Context);
|
|
57
|
-
if (!ctx) throw new Error("Store not found in context");
|
|
58
|
-
|
|
59
|
-
// Subscribe to the raw state snapshot (stable reference until a dispatch)
|
|
60
|
-
const state = useSyncExternalStore(
|
|
61
|
-
ctx.subscribe,
|
|
62
|
-
ctx.getState,
|
|
63
|
-
ctx.getState
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
// Cache the selected slice per state snapshot to avoid returning fresh objects during render
|
|
67
|
-
const lastRef = useRef<{ state: S; selected: T } | null>(null);
|
|
68
|
-
const last = lastRef.current;
|
|
69
|
-
const nextSelected = selector(state);
|
|
70
|
-
|
|
71
|
-
if (last && last.state === state && isEqual(last.selected, nextSelected)) {
|
|
72
|
-
return last.selected; // return cached reference to satisfy getSnapshot caching
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
lastRef.current = { state, selected: nextSelected };
|
|
76
|
-
return nextSelected;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
store,
|
|
81
|
-
Context,
|
|
82
|
-
Provider,
|
|
83
|
-
useStore,
|
|
84
|
-
useDispatch,
|
|
85
|
-
useSelector,
|
|
86
|
-
};
|
|
87
|
-
}
|
package/src/index.ts
DELETED
package/src/metadata-store.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// metadata-store.ts
|
|
2
|
-
export class MetadataStore<T extends object, Meta extends object> {
|
|
3
|
-
private readonly symbol: symbol;
|
|
4
|
-
|
|
5
|
-
constructor(description: string) {
|
|
6
|
-
this.symbol = Symbol(description);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
set(target: T, meta: Meta) {
|
|
10
|
-
Object.defineProperty(target, this.symbol as PropertyKey, {
|
|
11
|
-
value: meta,
|
|
12
|
-
writable: false,
|
|
13
|
-
enumerable: false,
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get(target: T): Meta | undefined {
|
|
18
|
-
return (target as Record<PropertyKey, Meta>)[this.symbol as PropertyKey];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
has(target: T): boolean {
|
|
22
|
-
return (this.symbol as PropertyKey) in target;
|
|
23
|
-
}
|
|
24
|
-
}
|
package/src/provider.tsx
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect, useState } from "react";
|
|
2
|
-
import type { ReactNode } from "react";
|
|
3
|
-
import type { Store, IState, IAction } from "./store.js";
|
|
4
|
-
|
|
5
|
-
const StoreContext = createContext<Store<IState, IAction> | undefined>(undefined);
|
|
6
|
-
|
|
7
|
-
function useStoreInstance<S extends IState, A extends IAction>(): Store<S, A> {
|
|
8
|
-
const store = useContext(StoreContext) as Store<S, A> | undefined;
|
|
9
|
-
if (!store) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
"StoreProvider is missing in the React tree. Wrap your app with <StoreProvider store={...}>."
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
return store;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface StoreProviderProps<S extends IState, A extends IAction> {
|
|
18
|
-
store: Store<S, A>;
|
|
19
|
-
children: ReactNode;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function StoreProvider<S extends IState, A extends IAction>({
|
|
23
|
-
store,
|
|
24
|
-
children,
|
|
25
|
-
}: StoreProviderProps<S, A>) {
|
|
26
|
-
return (
|
|
27
|
-
<StoreContext.Provider value={store as unknown as Store<IState, IAction>}>
|
|
28
|
-
{children}
|
|
29
|
-
</StoreContext.Provider>
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function useStore<S extends IState>(): S {
|
|
34
|
-
const store = useStoreInstance<S, IAction>();
|
|
35
|
-
const [state, setState] = useState<S>(() => store.getState());
|
|
36
|
-
|
|
37
|
-
useEffect(() => {
|
|
38
|
-
// Subscribe to store changes and update local state.
|
|
39
|
-
const unsubscribe = store.subscribe(() => {
|
|
40
|
-
setState(store.getState());
|
|
41
|
-
});
|
|
42
|
-
return unsubscribe;
|
|
43
|
-
}, [store]);
|
|
44
|
-
|
|
45
|
-
return state;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function useDispatch<A extends IAction>(): Store<IState, A>["dispatch"] {
|
|
49
|
-
const store = useStoreInstance<IState, A>();
|
|
50
|
-
// Return the store's dispatch directly; consumers can call dispatch(action).
|
|
51
|
-
return store.dispatch as Store<IState, A>["dispatch"];
|
|
52
|
-
}
|
package/src/store.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import type { Reducer, Listener } from "./types.js";
|
|
2
|
-
|
|
3
|
-
// Allow narrower parameter types for callbacks without fighting variance
|
|
4
|
-
type BivariantListener<T> = {
|
|
5
|
-
bivarianceHack(value: T): void;
|
|
6
|
-
}["bivarianceHack"];
|
|
7
|
-
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
9
|
-
export interface IState {}
|
|
10
|
-
export interface IAction {
|
|
11
|
-
type: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface Store<S extends IState, A extends IAction> {
|
|
15
|
-
getState(): S;
|
|
16
|
-
dispatch(action: A): void;
|
|
17
|
-
/**
|
|
18
|
-
* Subscribe to all state changes.
|
|
19
|
-
*/
|
|
20
|
-
subscribe(listener: Listener): () => void;
|
|
21
|
-
/**
|
|
22
|
-
* Subscribe to changes of a specific key in the state.
|
|
23
|
-
*/
|
|
24
|
-
subscribeToKey<K extends keyof S>(
|
|
25
|
-
key: K,
|
|
26
|
-
listener: (value: S[K]) => void
|
|
27
|
-
): () => void;
|
|
28
|
-
/**
|
|
29
|
-
* Subscribe to changes in a selected value from the state.
|
|
30
|
-
*/
|
|
31
|
-
subscribeWithSelector<T>(
|
|
32
|
-
selector: (state: S) => T,
|
|
33
|
-
listener: (selected: T) => void
|
|
34
|
-
): () => void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function createStore<S extends IState, A extends IAction>(
|
|
38
|
-
reducer: Reducer<S, A>,
|
|
39
|
-
initialState: S
|
|
40
|
-
): Store<S, A> {
|
|
41
|
-
let state = initialState;
|
|
42
|
-
const listeners = new Set<Listener>();
|
|
43
|
-
const keyListeners = new Map<keyof S, Set<BivariantListener<S[keyof S]>>>();
|
|
44
|
-
|
|
45
|
-
interface SelectorEntry<T> {
|
|
46
|
-
selector: (state: S) => T;
|
|
47
|
-
listener: BivariantListener<T>;
|
|
48
|
-
lastValue: T;
|
|
49
|
-
}
|
|
50
|
-
const selectorListeners = new Set<SelectorEntry<unknown>>();
|
|
51
|
-
|
|
52
|
-
const getState = () => state;
|
|
53
|
-
|
|
54
|
-
const dispatch = (action: A) => {
|
|
55
|
-
const prevState = state;
|
|
56
|
-
const nextState = reducer(state, action);
|
|
57
|
-
|
|
58
|
-
// Distinct-until-changed: if the reducer returns the same reference,
|
|
59
|
-
// skip all notifications (prevents unnecessary re-renders).
|
|
60
|
-
if (Object.is(prevState, nextState)) {
|
|
61
|
-
state = nextState; // keep any identity guarantees from reducer
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
state = nextState;
|
|
66
|
-
|
|
67
|
-
// Notify global listeners (iterate over a snapshot so unsubscribe during
|
|
68
|
-
// notify does not skip the next listener)
|
|
69
|
-
for (const listener of [...listeners]) listener();
|
|
70
|
-
|
|
71
|
-
// Notify key listeners only when that key actually changed (Object.is)
|
|
72
|
-
for (const [key, set] of keyListeners.entries()) {
|
|
73
|
-
if (!Object.is(prevState[key], state[key])) {
|
|
74
|
-
for (const listener of [...set]) listener(state[key]);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Notify selector listeners only when selected value changed (Object.is)
|
|
79
|
-
selectorListeners.forEach((entry) => {
|
|
80
|
-
const nextValue = (entry.selector as (s: S) => unknown)(state);
|
|
81
|
-
if (!Object.is(entry.lastValue, nextValue)) {
|
|
82
|
-
entry.lastValue = nextValue as unknown;
|
|
83
|
-
(entry.listener as (v: unknown) => void)(nextValue);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const subscribe = (listener: Listener) => {
|
|
89
|
-
listeners.add(listener);
|
|
90
|
-
return () => {
|
|
91
|
-
listeners.delete(listener);
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const subscribeToKey = <K extends keyof S>(
|
|
96
|
-
key: K,
|
|
97
|
-
listener: (value: S[K]) => void
|
|
98
|
-
) => {
|
|
99
|
-
const set =
|
|
100
|
-
keyListeners.get(key) ?? new Set<BivariantListener<S[keyof S]>>();
|
|
101
|
-
set.add(listener as unknown as BivariantListener<S[keyof S]>);
|
|
102
|
-
keyListeners.set(key, set);
|
|
103
|
-
return () => {
|
|
104
|
-
set.delete(listener as unknown as BivariantListener<S[keyof S]>);
|
|
105
|
-
if (set.size === 0) keyListeners.delete(key);
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const subscribeWithSelector = <T>(
|
|
110
|
-
selector: (state: S) => T,
|
|
111
|
-
listener: (selected: T) => void
|
|
112
|
-
) => {
|
|
113
|
-
const entry: SelectorEntry<T> = {
|
|
114
|
-
selector,
|
|
115
|
-
listener: listener as BivariantListener<T>,
|
|
116
|
-
lastValue: selector(state),
|
|
117
|
-
};
|
|
118
|
-
selectorListeners.add(entry as unknown as SelectorEntry<unknown>);
|
|
119
|
-
return () => {
|
|
120
|
-
selectorListeners.delete(entry as unknown as SelectorEntry<unknown>);
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
getState,
|
|
126
|
-
dispatch,
|
|
127
|
-
subscribe,
|
|
128
|
-
subscribeToKey,
|
|
129
|
-
subscribeWithSelector,
|
|
130
|
-
};
|
|
131
|
-
}
|
package/src/types.ts
DELETED