@cosystem/svelte 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Readable } from "svelte/store";
|
|
2
|
+
import { App, InjectionToken } from "@cosystem/core";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
interface SelectorStoreOptions<T> {
|
|
6
|
+
readonly equals?: (value: T, previous: T) => boolean;
|
|
7
|
+
}
|
|
8
|
+
type AppSelector<T> = (app: App) => T;
|
|
9
|
+
type ModuleSelector<TModule, TValue> = (module: TModule, app: App) => TValue;
|
|
10
|
+
declare const CoSystemContextKey: unique symbol;
|
|
11
|
+
declare function setCoSystemApp(app: App): App;
|
|
12
|
+
declare function clearCoSystemApp(): void;
|
|
13
|
+
declare function setCoSystemContext(app: App): App;
|
|
14
|
+
declare function getCoSystemApp(): App;
|
|
15
|
+
declare function moduleStore<T>(token: InjectionToken<T>, app?: App): Readable<T>;
|
|
16
|
+
declare function selectorStore<T>(selector: AppSelector<T>, options?: SelectorStoreOptions<T> & {
|
|
17
|
+
readonly app?: App;
|
|
18
|
+
}): Readable<T>;
|
|
19
|
+
declare function selectedModuleStore<TModule, TValue>(token: InjectionToken<TModule>, selector: ModuleSelector<TModule, TValue>, options?: SelectorStoreOptions<TValue> & {
|
|
20
|
+
readonly app?: App;
|
|
21
|
+
}): Readable<TValue>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { AppSelector, CoSystemContextKey, ModuleSelector, SelectorStoreOptions, clearCoSystemApp, getCoSystemApp, moduleStore, selectedModuleStore, selectorStore, setCoSystemApp, setCoSystemContext };
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;UAKiB,oBAAA;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,kBAAA;AAAA,iBAIG,cAAA,CAAe,GAAA,EAAK,GAAA,GAAM,GAAA;AAAA,iBAK1B,gBAAA;AAAA,iBAIA,kBAAA,CAAmB,GAAA,EAAK,GAAA,GAAM,GAAA;AAAA,iBAK9B,cAAA,IAAkB,GAAA;AAAA,iBAgBlB,WAAA,IAAe,KAAA,EAAO,cAAA,CAAe,CAAA,GAAI,GAAA,GAAM,GAAA,GAAM,QAAA,CAAS,CAAA;AAAA,iBAK9D,aAAA,IACd,QAAA,EAAU,WAAA,CAAY,CAAA,GACtB,OAAA,GAAS,oBAAA,CAAqB,CAAA;EAAA,SAAgB,GAAA,GAAM,GAAA;AAAA,IACnD,QAAA,CAAS,CAAA;AAAA,iBAuBI,mBAAA,kBACd,KAAA,EAAO,cAAA,CAAe,OAAA,GACtB,QAAA,EAAU,cAAA,CAAe,OAAA,EAAS,MAAA,GAClC,OAAA,GAAS,oBAAA,CAAqB,MAAA;EAAA,SAAqB,GAAA,GAAM,GAAA;AAAA,IACxD,QAAA,CAAS,MAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getContext, hasContext, setContext } from "svelte";
|
|
2
|
+
import { readable } from "svelte/store";
|
|
3
|
+
import { CosystemError } from "@cosystem/core";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const CoSystemContextKey = Symbol("CoSystem");
|
|
6
|
+
let defaultApp;
|
|
7
|
+
function setCoSystemApp(app) {
|
|
8
|
+
defaultApp = app;
|
|
9
|
+
return app;
|
|
10
|
+
}
|
|
11
|
+
function clearCoSystemApp() {
|
|
12
|
+
defaultApp = void 0;
|
|
13
|
+
}
|
|
14
|
+
function setCoSystemContext(app) {
|
|
15
|
+
setContext(CoSystemContextKey, app);
|
|
16
|
+
return app;
|
|
17
|
+
}
|
|
18
|
+
function getCoSystemApp() {
|
|
19
|
+
if (defaultApp !== void 0) return defaultApp;
|
|
20
|
+
const contextApp = getCoSystemContextApp();
|
|
21
|
+
if (contextApp !== void 0) return contextApp;
|
|
22
|
+
throw new CosystemError("Missing CoSystem Svelte app. Call setCoSystemApp(app) or setCoSystemContext(app).");
|
|
23
|
+
}
|
|
24
|
+
function moduleStore(token, app) {
|
|
25
|
+
const resolvedApp = app ?? getCoSystemApp();
|
|
26
|
+
return selectorStore(() => resolvedApp.getModule(token), { app: resolvedApp });
|
|
27
|
+
}
|
|
28
|
+
function selectorStore(selector, options = {}) {
|
|
29
|
+
const app = options.app ?? getCoSystemApp();
|
|
30
|
+
const equals = options.equals ?? Object.is;
|
|
31
|
+
return readable(selector(app), (set) => {
|
|
32
|
+
let current = selector(app);
|
|
33
|
+
set(current);
|
|
34
|
+
return app.watch(() => selector(app), (value) => {
|
|
35
|
+
if (equals(value, current)) return;
|
|
36
|
+
current = value;
|
|
37
|
+
set(value);
|
|
38
|
+
}, { equals });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function selectedModuleStore(token, selector, options = {}) {
|
|
42
|
+
return selectorStore((app) => selector(app.getModule(token), app), options);
|
|
43
|
+
}
|
|
44
|
+
function getCoSystemContextApp() {
|
|
45
|
+
try {
|
|
46
|
+
return hasContext(CoSystemContextKey) ? getContext(CoSystemContextKey) : void 0;
|
|
47
|
+
} catch {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { CoSystemContextKey, clearCoSystemApp, getCoSystemApp, moduleStore, selectedModuleStore, selectorStore, setCoSystemApp, setCoSystemContext };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { getContext, hasContext, setContext } from \"svelte\";\nimport { readable, type Readable } from \"svelte/store\";\n\nimport { CosystemError, type App, type InjectionToken } from \"@cosystem/core\";\n\nexport interface SelectorStoreOptions<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 CoSystemContextKey: unique symbol = Symbol(\"CoSystem\");\n\nlet defaultApp: App | undefined;\n\nexport function setCoSystemApp(app: App): App {\n defaultApp = app;\n return app;\n}\n\nexport function clearCoSystemApp(): void {\n defaultApp = undefined;\n}\n\nexport function setCoSystemContext(app: App): App {\n setContext(CoSystemContextKey, app);\n return app;\n}\n\nexport function getCoSystemApp(): App {\n if (defaultApp !== undefined) {\n return defaultApp;\n }\n\n const contextApp = getCoSystemContextApp();\n\n if (contextApp !== undefined) {\n return contextApp;\n }\n\n throw new CosystemError(\n \"Missing CoSystem Svelte app. Call setCoSystemApp(app) or setCoSystemContext(app).\",\n );\n}\n\nexport function moduleStore<T>(token: InjectionToken<T>, app?: App): Readable<T> {\n const resolvedApp = app ?? getCoSystemApp();\n return selectorStore(() => resolvedApp.getModule(token), { app: resolvedApp });\n}\n\nexport function selectorStore<T>(\n selector: AppSelector<T>,\n options: SelectorStoreOptions<T> & { readonly app?: App } = {},\n): Readable<T> {\n const app = options.app ?? getCoSystemApp();\n const equals = options.equals ?? Object.is;\n\n return readable(selector(app), (set) => {\n let current = selector(app);\n set(current);\n\n return app.watch(\n () => selector(app),\n (value) => {\n if (equals(value, current)) {\n return;\n }\n\n current = value;\n set(value);\n },\n { equals },\n );\n });\n}\n\nexport function selectedModuleStore<TModule, TValue>(\n token: InjectionToken<TModule>,\n selector: ModuleSelector<TModule, TValue>,\n options: SelectorStoreOptions<TValue> & { readonly app?: App } = {},\n): Readable<TValue> {\n return selectorStore((app) => selector(app.getModule(token), app), options);\n}\n\nfunction getCoSystemContextApp(): App | undefined {\n try {\n return hasContext(CoSystemContextKey) ? getContext<App>(CoSystemContextKey) : undefined;\n } catch {\n return undefined;\n }\n}\n"],"mappings":";;;;AAYA,MAAa,qBAAoC,OAAO,UAAU;AAElE,IAAI;AAEJ,SAAgB,eAAe,KAAe;CAC5C,aAAa;CACb,OAAO;AACT;AAEA,SAAgB,mBAAyB;CACvC,aAAa,KAAA;AACf;AAEA,SAAgB,mBAAmB,KAAe;CAChD,WAAW,oBAAoB,GAAG;CAClC,OAAO;AACT;AAEA,SAAgB,iBAAsB;CACpC,IAAI,eAAe,KAAA,GACjB,OAAO;CAGT,MAAM,aAAa,sBAAsB;CAEzC,IAAI,eAAe,KAAA,GACjB,OAAO;CAGT,MAAM,IAAI,cACR,mFACF;AACF;AAEA,SAAgB,YAAe,OAA0B,KAAwB;CAC/E,MAAM,cAAc,OAAO,eAAe;CAC1C,OAAO,oBAAoB,YAAY,UAAU,KAAK,GAAG,EAAE,KAAK,YAAY,CAAC;AAC/E;AAEA,SAAgB,cACd,UACA,UAA4D,CAAC,GAChD;CACb,MAAM,MAAM,QAAQ,OAAO,eAAe;CAC1C,MAAM,SAAS,QAAQ,UAAU,OAAO;CAExC,OAAO,SAAS,SAAS,GAAG,IAAI,QAAQ;EACtC,IAAI,UAAU,SAAS,GAAG;EAC1B,IAAI,OAAO;EAEX,OAAO,IAAI,YACH,SAAS,GAAG,IACjB,UAAU;GACT,IAAI,OAAO,OAAO,OAAO,GACvB;GAGF,UAAU;GACV,IAAI,KAAK;EACX,GACA,EAAE,OAAO,CACX;CACF,CAAC;AACH;AAEA,SAAgB,oBACd,OACA,UACA,UAAiE,CAAC,GAChD;CAClB,OAAO,eAAe,QAAQ,SAAS,IAAI,UAAU,KAAK,GAAG,GAAG,GAAG,OAAO;AAC5E;AAEA,SAAS,wBAAyC;CAChD,IAAI;EACF,OAAO,WAAW,kBAAkB,IAAI,WAAgB,kBAAkB,IAAI,KAAA;CAChF,QAAQ;EACN;CACF;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cosystem/svelte",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Svelte 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.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"svelte": "5.56.3",
|
|
27
|
+
"tsdown": "0.22.3",
|
|
28
|
+
"typescript": "6.0.3",
|
|
29
|
+
"vitest": "4.1.9",
|
|
30
|
+
"@cosystem/tsconfig": "0.0.2"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"svelte": ">=4.0.0 || >=5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"clean": "rm -rf coverage dist .turbo",
|
|
38
|
+
"dev": "tsdown --watch",
|
|
39
|
+
"test": "cd ../.. && vitest run --project @cosystem/svelte",
|
|
40
|
+
"test:coverage": "cd ../.. && vitest run --coverage --project @cosystem/svelte",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
42
|
+
}
|
|
43
|
+
}
|