@sveltebase/state 0.1.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.
@@ -0,0 +1,2 @@
1
+ export { PersistentState, State } from "./state.svelte.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { PersistentState, State } from "./state.svelte.js";
@@ -0,0 +1,23 @@
1
+ import { z } from "zod";
2
+ export declare class PersistentState<TSchema extends z.ZodTypeAny> {
3
+ #private;
4
+ private storageKey;
5
+ private schema;
6
+ constructor(key: string, schema: TSchema);
7
+ get current(): z.output<TSchema>;
8
+ set current(newValue: z.output<TSchema>);
9
+ init(cookies: {
10
+ name: string;
11
+ value: string;
12
+ }[]): void;
13
+ set(fn: (value: z.output<TSchema>) => z.output<TSchema>): void;
14
+ private static hydrate;
15
+ }
16
+ export declare class State<T> {
17
+ #private;
18
+ constructor(initialValue: T);
19
+ get current(): T;
20
+ set current(value: T);
21
+ set(fn: (value: T) => T): void;
22
+ }
23
+ //# sourceMappingURL=state.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.svelte.d.ts","sourceRoot":"","sources":["../src/state.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,qBAAa,eAAe,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU;;IAGvD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAU;gBAEZ,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;IAmBxC,IAAI,OAAO,IAIW,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAFtC;IAED,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAEtC;IAEM,IAAI,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE;IAsB/C,GAAG,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;IAI9D,OAAO,CAAC,MAAM,CAAC,OAAO;CAiBvB;AAED,qBAAa,KAAK,CAAC,CAAC;;gBAGN,YAAY,EAAE,CAAC;IAI3B,IAAI,OAAO,IAIQ,CAAC,CAFnB;IAED,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC,EAEnB;IAED,GAAG,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC;CAGxB"}
@@ -0,0 +1,83 @@
1
+ import { z } from "zod";
2
+ import { Cookies } from "@svelte-essentials/utils";
3
+ export class PersistentState {
4
+ #value = $state();
5
+ storageKey;
6
+ schema;
7
+ constructor(key, schema) {
8
+ this.storageKey = key;
9
+ this.schema = schema;
10
+ this.#value = PersistentState.hydrate(key, schema);
11
+ $effect.root(() => {
12
+ $effect(() => {
13
+ if (!hasWindow()) {
14
+ return;
15
+ }
16
+ Cookies.set(this.storageKey, JSON.stringify(this.#value), {
17
+ sameSite: "Lax",
18
+ expires: 365
19
+ });
20
+ });
21
+ });
22
+ }
23
+ get current() {
24
+ return this.#value;
25
+ }
26
+ set current(newValue) {
27
+ this.#value = this.schema.parse(newValue);
28
+ }
29
+ init(cookies) {
30
+ if (hasWindow()) {
31
+ return;
32
+ }
33
+ const rawCookie = cookies.find((cookie) => cookie.name === this.storageKey);
34
+ if (!rawCookie) {
35
+ return;
36
+ }
37
+ try {
38
+ const parsed = this.schema.parse(JSON.parse(rawCookie.value));
39
+ if (JSON.stringify(parsed) !== JSON.stringify(this.#value)) {
40
+ this.#value = parsed;
41
+ }
42
+ }
43
+ catch {
44
+ console.warn(`[PersistentState] Init failed for "${this.storageKey}"`);
45
+ }
46
+ }
47
+ set(fn) {
48
+ this.#value = fn(this.#value);
49
+ }
50
+ static hydrate(key, schema) {
51
+ if (!hasWindow()) {
52
+ return schema.parse(undefined);
53
+ }
54
+ const rawCookie = Cookies.get(key);
55
+ if (rawCookie) {
56
+ try {
57
+ return schema.parse(JSON.parse(rawCookie));
58
+ }
59
+ catch {
60
+ console.warn(`[PersistentState] Invalid data for "${key}". Resetting.`);
61
+ }
62
+ }
63
+ return schema.parse(undefined);
64
+ }
65
+ }
66
+ export class State {
67
+ #internalState = $state();
68
+ constructor(initialValue) {
69
+ this.#internalState = initialValue;
70
+ }
71
+ get current() {
72
+ return this.#internalState;
73
+ }
74
+ set current(value) {
75
+ this.#internalState = value;
76
+ }
77
+ set(fn) {
78
+ this.#internalState = fn(this.#internalState);
79
+ }
80
+ }
81
+ function hasWindow() {
82
+ return typeof window !== "undefined";
83
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@sveltebase/state",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "svelte": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "svelte": "./dist/index.js",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "dependencies": {
21
+ "@sveltebase/utils": "0.1.0",
22
+ "zod": "^4.1.11"
23
+ },
24
+ "peerDependencies": {
25
+ "svelte": "^5.0.0"
26
+ },
27
+ "scripts": {
28
+ "build": "svelte-package --input ./src --output ./dist",
29
+ "check": "tsc --project tsconfig.json --noEmit",
30
+ "lint": "bun run build && publint --strict --pack npm",
31
+ "clean": "rm -rf dist"
32
+ }
33
+ }