jazz-svelte 0.14.24 → 0.14.26

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.
@@ -4,6 +4,7 @@ export declare class CoState<V extends CoValueOrZodSchema, R extends ResolveQuer
4
4
  constructor(Schema: V, id: string | undefined | null | (() => string | undefined | null), options?: {
5
5
  resolve?: ResolveQueryStrict<V, R>;
6
6
  });
7
+ update(value: Loaded<V, R> | undefined | null): void;
7
8
  get current(): Loaded<V, R> | null | undefined;
8
9
  }
9
10
  export declare class AccountCoState<A extends (AccountClass<Account> & CoValueFromRaw<Account>) | AnyAccountSchema, R extends ResolveQuery<A> = true> {
@@ -11,6 +12,7 @@ export declare class AccountCoState<A extends (AccountClass<Account> & CoValueFr
11
12
  constructor(Schema: A, options?: {
12
13
  resolve?: ResolveQueryStrict<A, R>;
13
14
  });
15
+ update(value: Loaded<A, R> | undefined | null): void;
14
16
  logOut: () => void;
15
17
  get current(): Loaded<A, R> | null | undefined;
16
18
  get agent(): import("jazz-tools").AnonymousJazzAgent | InstanceOfSchema<A>;
@@ -2,51 +2,52 @@ import { createSubscriber } from 'svelte/reactivity';
2
2
  import { getJazzContext } from './jazz.svelte';
3
3
  import { anySchemaToCoSchema, subscribeToCoValue } from 'jazz-tools';
4
4
  import { useIsAuthenticated } from './auth/useIsAuthenticated.svelte.js';
5
+ import { untrack } from 'svelte';
5
6
  export class CoState {
6
7
  #value = undefined;
7
8
  #ctx = getJazzContext();
8
9
  #id;
9
10
  #subscribe;
11
+ #update = () => { };
10
12
  constructor(Schema, id, options) {
11
13
  this.#id = $derived.by(typeof id === 'function' ? id : () => id);
12
14
  this.#subscribe = createSubscriber(update => {
13
- return $effect.root(() => {
14
- $effect.pre(() => {
15
- const ctx = this.#ctx.current;
16
- const id = this.#id;
17
- if (!ctx || !id)
18
- return update();
19
- const agent = 'me' in ctx ? ctx.me : ctx.guest;
20
- const unsubscribe = subscribeToCoValue(anySchemaToCoSchema(Schema), id, {
21
- // @ts-expect-error The resolve query type isn't compatible with the anySchemaToCoSchema conversion
22
- resolve: options?.resolve,
23
- loadAs: agent,
24
- onUnavailable: () => {
25
- this.#value = null;
26
- update();
27
- },
28
- onUnauthorized: () => {
29
- this.#value = null;
30
- update();
31
- },
32
- syncResolution: true
33
- }, value => {
34
- if (value === this.#value)
35
- return;
36
- this.#value = value;
37
- update();
38
- });
39
- return () => {
40
- unsubscribe();
41
- this.#value = undefined;
42
- };
43
- });
44
- });
15
+ this.#update = update;
45
16
  });
46
17
  $effect.pre(() => {
47
- this.#subscribe();
18
+ const ctx = this.#ctx.current;
19
+ const id = this.#id;
20
+ return untrack(() => {
21
+ if (!ctx || !id) {
22
+ return this.update(undefined);
23
+ }
24
+ const agent = 'me' in ctx ? ctx.me : ctx.guest;
25
+ const unsubscribe = subscribeToCoValue(anySchemaToCoSchema(Schema), id, {
26
+ // @ts-expect-error The resolve query type isn't compatible with the anySchemaToCoSchema conversion
27
+ resolve: options?.resolve,
28
+ loadAs: agent,
29
+ onUnavailable: () => {
30
+ this.update(null);
31
+ },
32
+ onUnauthorized: () => {
33
+ this.update(null);
34
+ },
35
+ syncResolution: true
36
+ }, value => {
37
+ this.update(value);
38
+ });
39
+ return () => {
40
+ unsubscribe();
41
+ };
42
+ });
48
43
  });
49
44
  }
45
+ update(value) {
46
+ if (this.#value === value)
47
+ return;
48
+ this.#value = value;
49
+ this.#update();
50
+ }
50
51
  get current() {
51
52
  this.#subscribe();
52
53
  return this.#value;
@@ -56,44 +57,44 @@ export class AccountCoState {
56
57
  #value = undefined;
57
58
  #ctx = getJazzContext();
58
59
  #subscribe;
60
+ #update = () => { };
59
61
  constructor(Schema, options) {
60
62
  this.#subscribe = createSubscriber(update => {
61
- return $effect.root(() => {
62
- $effect.pre(() => {
63
- const ctx = this.#ctx.current;
64
- if (!ctx || !('me' in ctx))
65
- return update();
66
- const me = ctx.me;
67
- const unsubscribe = subscribeToCoValue(anySchemaToCoSchema(Schema), me.id, {
68
- // @ts-expect-error The resolve query type isn't compatible with the anySchemaToCoSchema conversion
69
- resolve: options?.resolve,
70
- loadAs: me,
71
- onUnavailable: () => {
72
- this.#value = null;
73
- update();
74
- },
75
- onUnauthorized: () => {
76
- this.#value = null;
77
- update();
78
- },
79
- syncResolution: true
80
- }, value => {
81
- if (value === this.#value)
82
- return;
83
- this.#value = value;
84
- update();
85
- });
86
- return () => {
87
- unsubscribe();
88
- this.#value = undefined;
89
- };
90
- });
91
- });
63
+ this.#update = update;
92
64
  });
93
65
  $effect.pre(() => {
94
- this.#subscribe();
66
+ const ctx = this.#ctx.current;
67
+ return untrack(() => {
68
+ if (!ctx || !('me' in ctx)) {
69
+ return this.update(undefined);
70
+ }
71
+ const me = ctx.me;
72
+ const unsubscribe = subscribeToCoValue(anySchemaToCoSchema(Schema), me.id, {
73
+ // @ts-expect-error The resolve query type isn't compatible with the anySchemaToCoSchema conversion
74
+ resolve: options?.resolve,
75
+ loadAs: me,
76
+ onUnavailable: () => {
77
+ this.update(null);
78
+ },
79
+ onUnauthorized: () => {
80
+ this.update(null);
81
+ },
82
+ syncResolution: true
83
+ }, value => {
84
+ this.update(value);
85
+ });
86
+ return () => {
87
+ unsubscribe();
88
+ };
89
+ });
95
90
  });
96
91
  }
92
+ update(value) {
93
+ if (this.#value === value)
94
+ return;
95
+ this.#value = value;
96
+ this.#update();
97
+ }
97
98
  logOut = () => {
98
99
  this.#ctx.current?.logOut();
99
100
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jazz-svelte",
3
- "version": "0.14.24",
3
+ "version": "0.14.26",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",
@@ -46,13 +46,14 @@
46
46
  "svelte-check": "^4.0.0",
47
47
  "typescript": "5.6.2",
48
48
  "typescript-eslint": "^8.0.0",
49
+ "virtua": "^0.41.5",
49
50
  "vite": "6.3.5",
50
- "jazz-inspector-element": "0.14.24"
51
+ "jazz-inspector-element": "0.14.26"
51
52
  },
52
53
  "dependencies": {
53
- "cojson": "0.14.24",
54
- "jazz-tools": "0.14.24",
55
- "jazz-browser": "0.14.24"
54
+ "cojson": "0.14.26",
55
+ "jazz-tools": "0.14.26",
56
+ "jazz-browser": "0.14.26"
56
57
  },
57
58
  "scripts": {
58
59
  "dev": "vite dev",