@sveltebase/state 0.3.1 → 0.3.3

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.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { PersistentState, State } from "./state.svelte.js";
2
+ export type { MaybeGetter } from "./state.svelte.js";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC3D,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ export type MaybeGetter<T> = T | (() => T);
2
3
  export declare class PersistentState<TSchema extends z.ZodTypeAny> {
3
4
  #private;
4
5
  private storageKey;
@@ -6,10 +7,10 @@ export declare class PersistentState<TSchema extends z.ZodTypeAny> {
6
7
  constructor(key: string, schema: TSchema);
7
8
  get current(): z.output<TSchema>;
8
9
  set current(newValue: z.output<TSchema>);
9
- init(cookies: {
10
+ init(cookies: MaybeGetter<{
10
11
  name: string;
11
12
  value: string;
12
- }[]): void;
13
+ }[]>): void;
13
14
  set(fn: (value: z.output<TSchema>) => z.output<TSchema>): void;
14
15
  private static hydrate;
15
16
  }
@@ -1 +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,C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,EAEtC;IAEM,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAyB5D,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"}
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  import { Cookies } from "@sveltebase/utils";
3
3
  export class PersistentState {
4
4
  #value = $state();
5
+ #initialized = false;
5
6
  storageKey;
6
7
  schema;
7
8
  constructor(key, schema) {
@@ -27,10 +28,12 @@ export class PersistentState {
27
28
  this.#value = this.schema.parse(newValue);
28
29
  }
29
30
  init(cookies) {
30
- if (hasWindow()) {
31
+ if (this.#initialized || hasWindow()) {
31
32
  return;
32
33
  }
33
- const rawCookie = cookies.find((cookie) => cookie.name === this.storageKey);
34
+ this.#initialized = true;
35
+ const resolvedCookies = unwrap(cookies);
36
+ const rawCookie = resolvedCookies.find((cookie) => cookie.name === this.storageKey);
34
37
  if (!rawCookie) {
35
38
  return;
36
39
  }
@@ -78,6 +81,9 @@ export class State {
78
81
  this.#internalState = fn(this.#internalState);
79
82
  }
80
83
  }
84
+ function unwrap(value) {
85
+ return typeof value === "function" ? value() : value;
86
+ }
81
87
  function hasWindow() {
82
88
  return typeof window !== "undefined";
83
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/state",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -18,7 +18,7 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@sveltebase/utils": "0.3.1",
21
+ "@sveltebase/utils": "0.3.3",
22
22
  "zod": "^4.1.11"
23
23
  },
24
24
  "peerDependencies": {