@cosystem/react 0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Coaction
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ import { Context, ReactElement, ReactNode } from "react";
2
+ import { App, InjectionToken } from "@cosystem/core";
3
+
4
+ //#region src/index.d.ts
5
+ interface CoSystemProviderProps {
6
+ readonly app: App;
7
+ readonly children?: ReactNode;
8
+ }
9
+ interface UseSelectorOptions<T> {
10
+ readonly equals?: (value: T, previous: T) => boolean;
11
+ }
12
+ type AppSelector<T> = (app: App) => T;
13
+ type ModuleSelector<TModule, TValue> = (module: TModule, app: App) => TValue;
14
+ declare const CoSystemContext: Context<App | null>;
15
+ declare function CoSystemProvider({
16
+ app,
17
+ children
18
+ }: CoSystemProviderProps): ReactElement;
19
+ declare function useCoSystem(): App;
20
+ declare function useApp(): App;
21
+ declare function useModule<T>(token: InjectionToken<T>): T;
22
+ declare function useSelector<T>(selector: AppSelector<T>, options?: UseSelectorOptions<T>): T;
23
+ declare function useSelector<TModule, TValue>(token: InjectionToken<TModule>, selector: ModuleSelector<TModule, TValue>, options?: UseSelectorOptions<TValue>): TValue;
24
+ //#endregion
25
+ export { AppSelector, CoSystemContext, CoSystemProvider, CoSystemProviderProps, ModuleSelector, UseSelectorOptions, useApp, useCoSystem, useModule, useSelector };
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;UAciB,qBAAA;EAAA,SACN,GAAA,EAAK,GAAA;EAAA,SACL,QAAA,GAAW,SAAA;AAAA;AAAA,UAGL,kBAAA;EAAA,SACN,MAAA,IAAU,KAAA,EAAO,CAAA,EAAG,QAAA,EAAU,CAAA;AAAA;AAAA,KAG7B,WAAA,OAAkB,GAAA,EAAK,GAAA,KAAQ,CAAA;AAAA,KAC/B,cAAA,qBAAmC,MAAA,EAAQ,OAAA,EAAS,GAAA,EAAK,GAAA,KAAQ,MAAA;AAAA,cAEhE,eAAA,EAAiB,OAAA,CAAQ,GAAA;AAAA,iBAEtB,gBAAA;EAAmB,GAAA;EAAK;AAAA,GAAY,qBAAA,GAAwB,YAAA;AAAA,iBAI5D,WAAA,IAAe,GAAA;AAAA,iBAUf,MAAA,IAAU,GAAA;AAAA,iBAIV,SAAA,IAAa,KAAA,EAAO,cAAA,CAAe,CAAA,IAAK,CAAA;AAAA,iBAIxC,WAAA,IAAe,QAAA,EAAU,WAAA,CAAY,CAAA,GAAI,OAAA,GAAU,kBAAA,CAAmB,CAAA,IAAK,CAAA;AAAA,iBAC3E,WAAA,kBACd,KAAA,EAAO,cAAA,CAAe,OAAA,GACtB,QAAA,EAAU,cAAA,CAAe,OAAA,EAAS,MAAA,GAClC,OAAA,GAAU,kBAAA,CAAmB,MAAA,IAC5B,MAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ import { createContext, createElement, useCallback, useContext, useRef, useSyncExternalStore } from "react";
2
+ import { CosystemError } from "@cosystem/core";
3
+ //#region src/index.ts
4
+ const CoSystemContext = createContext(null);
5
+ function CoSystemProvider({ app, children }) {
6
+ return createElement(CoSystemContext.Provider, { value: app }, children);
7
+ }
8
+ function useCoSystem() {
9
+ const app = useContext(CoSystemContext);
10
+ if (app === null) throw new CosystemError("Missing CoSystemProvider.");
11
+ return app;
12
+ }
13
+ function useApp() {
14
+ return useCoSystem();
15
+ }
16
+ function useModule(token) {
17
+ return useCoSystem().getModule(token);
18
+ }
19
+ function useSelector(first, second, third) {
20
+ return useSelectedValue(useCoSystem(), typeof second === "function" ? (currentApp) => second(currentApp.getModule(first), currentApp) : first, (typeof second === "function" ? third : second) ?? {});
21
+ }
22
+ function useSelectedValue(app, selector, options) {
23
+ const selectorRef = useRef(selector);
24
+ const equalsRef = useRef(options.equals ?? objectIs);
25
+ const snapshotRef = useRef(void 0);
26
+ selectorRef.current = selector;
27
+ equalsRef.current = options.equals ?? objectIs;
28
+ const getSnapshot = useCallback(() => {
29
+ const next = selectorRef.current(app);
30
+ const current = snapshotRef.current;
31
+ if (current === void 0 || !equalsRef.current(next, current.value)) {
32
+ const snapshot = { value: next };
33
+ snapshotRef.current = snapshot;
34
+ return snapshot.value;
35
+ }
36
+ return current.value;
37
+ }, [app]);
38
+ return useSyncExternalStore(useCallback((notify) => app.watch(() => selectorRef.current(app), (value) => {
39
+ const current = snapshotRef.current;
40
+ if (current !== void 0 && equalsRef.current(value, current.value)) return;
41
+ snapshotRef.current = { value };
42
+ notify();
43
+ }, { equals: (value, previous) => equalsRef.current(value, previous) }), [app]), getSnapshot, getSnapshot);
44
+ }
45
+ function objectIs(value, previous) {
46
+ return Object.is(value, previous);
47
+ }
48
+ //#endregion
49
+ export { CoSystemContext, CoSystemProvider, useApp, useCoSystem, useModule, useSelector };
50
+
51
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {\n createContext,\n createElement,\n useCallback,\n useContext,\n useRef,\n useSyncExternalStore,\n type Context,\n type ReactElement,\n type ReactNode,\n} from \"react\";\n\nimport { CosystemError, type App, type InjectionToken } from \"@cosystem/core\";\n\nexport interface CoSystemProviderProps {\n readonly app: App;\n readonly children?: ReactNode;\n}\n\nexport interface UseSelectorOptions<T> {\n readonly equals?: (value: T, previous: T) => boolean;\n}\n\nexport type AppSelector<T> = (app: App) => T;\nexport type ModuleSelector<TModule, TValue> = (module: TModule, app: App) => TValue;\n\nexport const CoSystemContext: Context<App | null> = createContext<App | null>(null);\n\nexport function CoSystemProvider({ app, children }: CoSystemProviderProps): ReactElement {\n return createElement(CoSystemContext.Provider, { value: app }, children);\n}\n\nexport function useCoSystem(): App {\n const app = useContext(CoSystemContext);\n\n if (app === null) {\n throw new CosystemError(\"Missing CoSystemProvider.\");\n }\n\n return app;\n}\n\nexport function useApp(): App {\n return useCoSystem();\n}\n\nexport function useModule<T>(token: InjectionToken<T>): T {\n return useCoSystem().getModule(token);\n}\n\nexport function useSelector<T>(selector: AppSelector<T>, options?: UseSelectorOptions<T>): T;\nexport function useSelector<TModule, TValue>(\n token: InjectionToken<TModule>,\n selector: ModuleSelector<TModule, TValue>,\n options?: UseSelectorOptions<TValue>,\n): TValue;\nexport function useSelector<TModule, TValue>(\n first: AppSelector<TValue> | InjectionToken<TModule>,\n second?: ModuleSelector<TModule, TValue> | UseSelectorOptions<TValue>,\n third?: UseSelectorOptions<TValue>,\n): TValue {\n const app = useCoSystem();\n const selector =\n typeof second === \"function\"\n ? (currentApp: App) =>\n second(currentApp.getModule(first as InjectionToken<TModule>), currentApp)\n : (first as AppSelector<TValue>);\n const options = typeof second === \"function\" ? third : second;\n\n return useSelectedValue(app, selector, options ?? {});\n}\n\nfunction useSelectedValue<T>(\n app: App,\n selector: AppSelector<T>,\n options: UseSelectorOptions<T>,\n): T {\n const selectorRef = useRef(selector);\n const equalsRef = useRef(options.equals ?? objectIs);\n const snapshotRef = useRef<{ readonly value: T }>(undefined);\n\n selectorRef.current = selector;\n equalsRef.current = options.equals ?? objectIs;\n\n const getSnapshot = useCallback(() => {\n const next = selectorRef.current(app);\n const current = snapshotRef.current;\n\n if (current === undefined || !equalsRef.current(next, current.value)) {\n const snapshot = { value: next };\n snapshotRef.current = snapshot;\n return snapshot.value;\n }\n\n return current.value;\n }, [app]);\n\n const subscribe = useCallback(\n (notify: () => void) =>\n app.watch(\n () => selectorRef.current(app),\n (value) => {\n const current = snapshotRef.current;\n\n if (current !== undefined && equalsRef.current(value, current.value)) {\n return;\n }\n\n snapshotRef.current = { value };\n notify();\n },\n {\n equals: (value, previous) => equalsRef.current(value, previous),\n },\n ),\n [app],\n );\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n\nfunction objectIs<T>(value: T, previous: T): boolean {\n return Object.is(value, previous);\n}\n"],"mappings":";;;AA0BA,MAAa,kBAAuC,cAA0B,IAAI;AAElF,SAAgB,iBAAiB,EAAE,KAAK,YAAiD;CACvF,OAAO,cAAc,gBAAgB,UAAU,EAAE,OAAO,IAAI,GAAG,QAAQ;AACzE;AAEA,SAAgB,cAAmB;CACjC,MAAM,MAAM,WAAW,eAAe;CAEtC,IAAI,QAAQ,MACV,MAAM,IAAI,cAAc,2BAA2B;CAGrD,OAAO;AACT;AAEA,SAAgB,SAAc;CAC5B,OAAO,YAAY;AACrB;AAEA,SAAgB,UAAa,OAA6B;CACxD,OAAO,YAAY,CAAC,CAAC,UAAU,KAAK;AACtC;AAQA,SAAgB,YACd,OACA,QACA,OACQ;CASR,OAAO,iBARK,YAQc,GANxB,OAAO,WAAW,cACb,eACC,OAAO,WAAW,UAAU,KAAgC,GAAG,UAAU,IAC1E,QACS,OAAO,WAAW,aAAa,QAAQ,WAEL,CAAC,CAAC;AACtD;AAEA,SAAS,iBACP,KACA,UACA,SACG;CACH,MAAM,cAAc,OAAO,QAAQ;CACnC,MAAM,YAAY,OAAO,QAAQ,UAAU,QAAQ;CACnD,MAAM,cAAc,OAA8B,KAAA,CAAS;CAE3D,YAAY,UAAU;CACtB,UAAU,UAAU,QAAQ,UAAU;CAEtC,MAAM,cAAc,kBAAkB;EACpC,MAAM,OAAO,YAAY,QAAQ,GAAG;EACpC,MAAM,UAAU,YAAY;EAE5B,IAAI,YAAY,KAAA,KAAa,CAAC,UAAU,QAAQ,MAAM,QAAQ,KAAK,GAAG;GACpE,MAAM,WAAW,EAAE,OAAO,KAAK;GAC/B,YAAY,UAAU;GACtB,OAAO,SAAS;EAClB;EAEA,OAAO,QAAQ;CACjB,GAAG,CAAC,GAAG,CAAC;CAuBR,OAAO,qBArBW,aACf,WACC,IAAI,YACI,YAAY,QAAQ,GAAG,IAC5B,UAAU;EACT,MAAM,UAAU,YAAY;EAE5B,IAAI,YAAY,KAAA,KAAa,UAAU,QAAQ,OAAO,QAAQ,KAAK,GACjE;EAGF,YAAY,UAAU,EAAE,MAAM;EAC9B,OAAO;CACT,GACA,EACE,SAAS,OAAO,aAAa,UAAU,QAAQ,OAAO,QAAQ,EAChE,CACF,GACF,CAAC,GAAG,CAG8B,GAAG,aAAa,WAAW;AACjE;AAEA,SAAS,SAAY,OAAU,UAAsB;CACnD,OAAO,OAAO,GAAG,OAAO,QAAQ;AAClC"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@cosystem/react",
3
+ "version": "0.0.0",
4
+ "description": "React adapter for CoSystem.",
5
+ "license": "MIT",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "type": "module",
10
+ "sideEffects": false,
11
+ "module": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@cosystem/core": "0.0.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/react": "19.2.17",
27
+ "@types/react-test-renderer": "19.1.0",
28
+ "react": "19.2.7",
29
+ "react-test-renderer": "19.2.7",
30
+ "tsdown": "0.22.3",
31
+ "typescript": "6.0.3",
32
+ "vitest": "4.1.9",
33
+ "@cosystem/tsconfig": "0.0.0"
34
+ },
35
+ "peerDependencies": {
36
+ "react": ">=18.3.0 || >=19.0.0"
37
+ },
38
+ "scripts": {
39
+ "build": "tsdown",
40
+ "clean": "rm -rf coverage dist .turbo",
41
+ "dev": "tsdown --watch",
42
+ "test": "cd ../.. && vitest run --project @cosystem/react",
43
+ "test:coverage": "cd ../.. && vitest run --coverage --project @cosystem/react",
44
+ "typecheck": "tsc -p tsconfig.json --noEmit"
45
+ }
46
+ }