effect-atom-svelte 0.4.0 → 0.5.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.
@@ -15,15 +15,18 @@ import { equals, fromPathId, getIn, idSuffix, isLive, pathId, reconcileKeys, set
15
15
  import { FormValidationError } from "./validationError.js";
16
16
  const ROOT = pathId([]);
17
17
  const notChecked = Symbol("notChecked");
18
- const freshState = (initial, keys) => ({
18
+ const freshState = (initial, keys, initialKeys = keys) => ({
19
19
  checked: notChecked,
20
+ checkedKeys: new Map(),
20
21
  decoded: Option.none(),
21
22
  external: new Map(),
22
23
  failure: undefined,
23
24
  initial,
25
+ initialKeys,
24
26
  issues: new Map(),
25
27
  keys,
26
28
  lastSubmitted: Option.none(),
29
+ lastSubmittedKeys: new Map(),
27
30
  revealed: new Set(),
28
31
  shown: new Set(),
29
32
  submitCount: 0,
@@ -65,6 +68,7 @@ export class Engine {
65
68
  #original;
66
69
  // The values the latest validation run decodes.
67
70
  #runValues;
71
+ #runKeys = new Map();
68
72
  #readState;
69
73
  #readValues;
70
74
  #nextKey = 0;
@@ -89,7 +93,6 @@ export class Engine {
89
93
  #fields = new Map();
90
94
  #groups = new Map();
91
95
  #arrays = new Map();
92
- #constraints = new Map();
93
96
  constructor(definition, options) {
94
97
  this.definition = definition;
95
98
  this.options = options;
@@ -103,7 +106,9 @@ export class Engine {
103
106
  Atom.make(options.initialValues).pipe(Atom.withLabel(`${name}.values`));
104
107
  const values = registry.get(this.valuesAtom);
105
108
  const keys = reconcileKeys(values, new Map(), () => this.#key());
106
- this.stateAtom = Atom.make(freshState(options.initialValues, keys)).pipe(Atom.withLabel(`${name}.state`));
109
+ // Paired with the values' items by position: the same keys when they are the initial values.
110
+ const initialKeys = reconcileKeys(options.initialValues, keys, () => this.#key());
111
+ this.stateAtom = Atom.make(freshState(options.initialValues, keys, initialKeys)).pipe(Atom.withLabel(`${name}.state`));
107
112
  this.#seen = { keys, values };
108
113
  const decode = Schema.decodeUnknownEffect(codecOf(definition));
109
114
  const run = (input) => decode(input.values, { errors: "all" }).pipe(Effect.map((decoded) => ({ _tag: "Valid", decoded, input })), Effect.catchTag("SchemaError", (failure) => Effect.succeed({
@@ -120,6 +125,8 @@ export class Engine {
120
125
  this.#readValues = () => registry.get(this.valuesAtom);
121
126
  this.#releases.push(registry.mount(this.stateAtom), registry.subscribe(this.valuesAtom, (next) => {
122
127
  this.#onValues(next);
128
+ }), onDispose(registry, () => {
129
+ this.#registryDisposed();
123
130
  }));
124
131
  if (options.validates) {
125
132
  this.#releases.push(registry.subscribe(this.#validateAtom, (result) => {
@@ -190,7 +197,8 @@ export class Engine {
190
197
  }
191
198
  const live = (ids) => new Set([...ids].filter((id) => id === ROOT || isLive(keys, fromPathId(id))));
192
199
  this.#seen = { keys, values: next };
193
- // Fields of items that are gone can't come back: their keys are never reused.
200
+ // Fields of items that are gone are dropped. Only a revert brings an item's key back, and its
201
+ // fields are made again.
194
202
  for (const cache of [this.#fields, this.#groups, this.#arrays]) {
195
203
  for (const id of cache.keys()) {
196
204
  if (!isLive(keys, fromPathId(id))) {
@@ -272,10 +280,26 @@ export class Engine {
272
280
  }
273
281
  const clean = equals(this.#currentValues(), state.initial);
274
282
  this.#original = initial;
275
- this.#update((latest) => ({ ...latest, initial }));
276
283
  if (clean) {
284
+ // The values become the new initial values, item for item: both take the current keys,
285
+ // set before the values as `updateArray` does, so the write keeps them.
286
+ const keys = reconcileKeys(initial, state.keys, () => this.#key());
287
+ this.#update((latest) => ({
288
+ ...latest,
289
+ initial,
290
+ initialKeys: keys,
291
+ keys,
292
+ }));
277
293
  this.registry.set(this.valuesAtom, initial);
294
+ return;
278
295
  }
296
+ // The edits stay: each new initial item replaces the old one at its position, so an item the
297
+ // user moved or kept compares with its new initial value.
298
+ this.#update((latest) => ({
299
+ ...latest,
300
+ initial,
301
+ initialKeys: reconcileKeys(initial, latest.initialKeys, () => this.#key()),
302
+ }));
279
303
  }
280
304
  reset(values) {
281
305
  if (values !== undefined) {
@@ -299,6 +323,7 @@ export class Engine {
299
323
  ? {
300
324
  ...fresh,
301
325
  lastSubmitted: state.lastSubmitted,
326
+ lastSubmittedKeys: state.lastSubmittedKeys,
302
327
  submitResult: state.submitResult,
303
328
  }
304
329
  : fresh);
@@ -309,10 +334,21 @@ export class Engine {
309
334
  revertToLastSubmit() {
310
335
  this.#editing = undefined;
311
336
  const state = this.#current();
312
- this.registry.set(this.valuesAtom, Option.match(state.lastSubmitted, {
313
- onNone: () => state.initial,
314
- onSome: ({ encoded }) => encoded,
315
- }));
337
+ const [values, keys] = Option.match(state.lastSubmitted, {
338
+ onNone: () => [state.initial, state.initialKeys],
339
+ onSome: ({ encoded }) => [encoded, state.lastSubmittedKeys],
340
+ });
341
+ // The items come back with the keys they had, set before the values as `updateArray` does: an
342
+ // item still there keeps its key and touched state, and each compares with its own initial
343
+ // value again. Paired by position instead, a remove or move before it left every item dirty.
344
+ this.#update((latest) => ({ ...latest, keys }));
345
+ const seen = this.#seen;
346
+ this.registry.set(this.valuesAtom, values);
347
+ // The registry drops a write of the values already there, but the keys changed: the checks
348
+ // and touched state must follow them all the same.
349
+ if (this.#seen === seen) {
350
+ this.#onValues(this.#currentValues());
351
+ }
316
352
  }
317
353
  setErrors(errors, source) {
318
354
  const state = this.#current();
@@ -374,8 +410,9 @@ export class Engine {
374
410
  }
375
411
  this.#token += 1;
376
412
  this.#runValues = this.#currentValues();
413
+ this.#runKeys = this.#current().keys;
377
414
  this.registry.set(this.#validateAtom, {
378
- keys: this.#current().keys,
415
+ keys: this.#runKeys,
379
416
  token: this.#token,
380
417
  values: this.#runValues,
381
418
  });
@@ -389,9 +426,11 @@ export class Engine {
389
426
  // field is unchecked, so the form isn't valid.
390
427
  console.error("effect-atom-svelte: a form's validation failed", Cause.pretty(result.cause));
391
428
  const values = this.#runValues;
429
+ const keys = this.#runKeys;
392
430
  this.#update((state) => ({
393
431
  ...state,
394
432
  checked: values,
433
+ checkedKeys: keys,
395
434
  decoded: Option.none(),
396
435
  failure: result.cause,
397
436
  validating: false,
@@ -421,6 +460,7 @@ export class Engine {
421
460
  this.#update((state) => ({
422
461
  ...state,
423
462
  checked: run.input.values,
463
+ checkedKeys: run.input.keys,
424
464
  decoded: run._tag === "Valid" ? Option.some(run.decoded) : Option.none(),
425
465
  failure: undefined,
426
466
  issues,
@@ -531,6 +571,9 @@ export class Engine {
531
571
  */
532
572
  // oxlint-disable-next-line effecttsgo/async-function -- the form's validate, part of the library's Promise-facing API for Svelte handlers
533
573
  async validate(paths) {
574
+ if (this.#disposed) {
575
+ return false;
576
+ }
534
577
  const state = this.#current();
535
578
  const keyPaths = paths === undefined
536
579
  ? undefined
@@ -550,6 +593,9 @@ export class Engine {
550
593
  }));
551
594
  this.#retryFailed();
552
595
  await this.#settled();
596
+ if (this.#disposed) {
597
+ return false;
598
+ }
553
599
  if (this.#isCurrent()) {
554
600
  this.#reveal(true);
555
601
  }
@@ -611,7 +657,8 @@ export class Engine {
611
657
  }));
612
658
  this.#retryFailed();
613
659
  await this.#settled();
614
- if (session !== this.#session) {
660
+ // A reset let go of this submit, or the registry is gone: nothing is sent.
661
+ if (session !== this.#session || this.#disposed) {
615
662
  return Exit.interrupt();
616
663
  }
617
664
  if (this.options.validates) {
@@ -622,18 +669,14 @@ export class Engine {
622
669
  const encoded = this.options.validates
623
670
  ? state.checked
624
671
  : this.#currentValues();
672
+ const encodedKeys = this.options.validates ? state.checkedKeys : state.keys;
625
673
  let decoded;
626
674
  if (this.options.validates) {
627
675
  // The decode couldn't run at all: there is nothing to send.
628
676
  if (state.failure !== undefined) {
629
677
  // Not the user's input, and nothing claims it: a defect, as the error a runtime failed with
630
678
  // isn't in the submit's error type. The app shows it, as for any defect.
631
- const cause = Cause.die(Cause.squash(state.failure));
632
- this.#update((latest) => ({
633
- ...latest,
634
- submitResult: AsyncResult.failure(cause),
635
- }));
636
- return Exit.failCause(cause);
679
+ return this.#failed(session, Cause.die(Cause.squash(state.failure)));
637
680
  }
638
681
  if (state.issues.size > 0 || Option.isNone(state.decoded)) {
639
682
  return Exit.fail(new FormValidationError({ errors: this.root.errors }));
@@ -652,43 +695,42 @@ export class Engine {
652
695
  }
653
696
  const target = this.options.submit;
654
697
  if (target === undefined) {
655
- this.#succeeded(session, encoded, decoded, decoded);
698
+ this.#succeeded(session, { encoded, encodedKeys }, decoded, decoded);
656
699
  return Exit.succeed(decoded);
657
700
  }
658
- const atom = target.atom();
659
- const value = target.payload ? target.payload(decoded, args) : decoded;
660
- this.#update((latest) => ({
661
- ...latest,
662
- submitResult: AsyncResult.waiting(latest.submitResult),
663
- submitting: true,
664
- }));
665
- // oxlint-disable-next-line effecttsgo/global-date -- compared with an AsyncResult's timestamp, which Effect takes from Date.now
666
- const start = Date.now();
667
- this.registry.set(atom, value);
668
- // The wait holds the atom, so the call finishes after the component is gone, unless the
669
- // registry itself is disposed of, which ends it as interrupted.
670
- const controller = new AbortController();
671
- const cancel = onDispose(this.registry, () => {
672
- controller.abort();
673
- });
674
701
  let exit;
675
702
  try {
676
- exit = await awaitWrite(this.registry, atom, start, controller.signal);
703
+ exit = await this.#send(target, decoded, args);
677
704
  }
678
- finally {
679
- cancel();
705
+ catch (error) {
706
+ // The payload, or the atom's write (as for an Atom.fn that throws before returning its
707
+ // Effect), threw: a defect, which ends the submit as a failed save does.
708
+ return this.#failed(session, Cause.die(error));
680
709
  }
681
- if (session !== this.#session) {
710
+ if (session !== this.#session || this.#disposed) {
682
711
  return exit;
683
712
  }
684
713
  if (Exit.isSuccess(exit)) {
685
- this.#succeeded(session, encoded, decoded, exit.value);
714
+ this.#succeeded(session, { encoded, encodedKeys }, decoded, exit.value);
686
715
  return exit;
687
716
  }
688
- const error = Cause.findErrorOption(exit.cause);
689
- const claimed = Option.isSome(error) && this.options.mapError
690
- ? this.options.mapError(error.value)
691
- : undefined;
717
+ return this.#saveFailed(session, exit);
718
+ }
719
+ // A failed save: mapError may claim its error onto fields or the form; otherwise it shows in
720
+ // submitResult.
721
+ #saveFailed(session, exit) {
722
+ const failure = Cause.findErrorOption(exit.cause);
723
+ let claimed;
724
+ try {
725
+ claimed =
726
+ Option.isSome(failure) && this.options.mapError
727
+ ? this.options.mapError(failure.value)
728
+ : undefined;
729
+ }
730
+ catch (error) {
731
+ // The save's failure stays, with the throw beside it as a defect.
732
+ return this.#failed(session, Cause.combine(exit.cause, Cause.die(error)));
733
+ }
692
734
  if (claimed !== undefined) {
693
735
  this.setErrors(claimed, "submit");
694
736
  }
@@ -708,15 +750,58 @@ export class Engine {
708
750
  }
709
751
  return exit;
710
752
  }
711
- #succeeded(session, encoded, decoded, value) {
712
- if (session !== this.#session) {
753
+ // Writes the payload to the submit's atom and waits for the save's result.
754
+ // oxlint-disable-next-line effecttsgo/async-function -- behind the form's submit, part of the library's Promise-facing API for Svelte handlers
755
+ async #send(target, decoded, args) {
756
+ const atom = target.atom();
757
+ const value = target.payload ? target.payload(decoded, args) : decoded;
758
+ this.#update((latest) => ({
759
+ ...latest,
760
+ submitResult: AsyncResult.waiting(latest.submitResult),
761
+ submitting: true,
762
+ }));
763
+ // oxlint-disable-next-line effecttsgo/global-date -- compared with an AsyncResult's timestamp, which Effect takes from Date.now
764
+ const start = Date.now();
765
+ this.registry.set(atom, value);
766
+ // The wait holds the atom, so the call finishes after the component is gone, unless the
767
+ // registry itself is disposed of, which ends it as interrupted.
768
+ const controller = new AbortController();
769
+ const cancel = onDispose(this.registry, () => {
770
+ controller.abort();
771
+ });
772
+ try {
773
+ return await awaitWrite(this.registry, atom, start, controller.signal);
774
+ }
775
+ finally {
776
+ cancel();
777
+ }
778
+ }
779
+ // Ends a submit with a failure nothing claims: it shows in submitResult.
780
+ #failed(session, cause) {
781
+ if (session === this.#session) {
782
+ this.#update((latest) => ({
783
+ ...latest,
784
+ submitResult: AsyncResult.failure(cause),
785
+ submitting: false,
786
+ }));
787
+ }
788
+ return Exit.failCause(cause);
789
+ }
790
+ #succeeded(session, submitted, decoded, value) {
791
+ if (session !== this.#session || this.#disposed) {
713
792
  return;
714
793
  }
794
+ const { encoded, encodedKeys } = submitted;
715
795
  const lastSubmitted = Option.some({ decoded, encoded });
796
+ const adopt = this.options.resetOnSuccess !== true;
716
797
  this.#update((latest) => ({
717
798
  ...latest,
718
- initial: this.options.resetOnSuccess === true ? latest.initial : encoded,
799
+ // The submitted values become the initial ones, with the keys their items had: the items
800
+ // may have moved since.
801
+ initial: adopt ? encoded : latest.initial,
802
+ initialKeys: adopt ? encodedKeys : latest.initialKeys,
719
803
  lastSubmitted,
804
+ lastSubmittedKeys: encodedKeys,
720
805
  submitResult: AsyncResult.success(value),
721
806
  submitting: false,
722
807
  }));
@@ -784,14 +869,11 @@ export class Engine {
784
869
  : `${this.options.id}-${idSuffix(path)}`;
785
870
  }
786
871
  constraints(path) {
787
- const schemaPath = path.map((segment) => typeof segment === "number" ? 0 : segment);
788
- const id = pathId(schemaPath);
789
- let found = this.#constraints.get(id);
790
- if (found === undefined) {
791
- found = constraintsAt(this.definition.schema.ast, schemaPath);
792
- this.#constraints.set(id, found);
793
- }
794
- return found;
872
+ // Reads the values only for a tuple with elements after its rest, whose fields move as it grows.
873
+ return constraintsAt(this.definition.schema.ast, path, (arrayPath) => {
874
+ const array = getIn(this.values, arrayPath);
875
+ return Array.isArray(array) ? array.length : undefined;
876
+ });
795
877
  }
796
878
  field(path) {
797
879
  const id = pathId(path);
@@ -839,4 +921,13 @@ export class Engine {
839
921
  }
840
922
  this.#releases = [];
841
923
  }
924
+ // The provider that owns the registry disposed of it, which drops the instance's atoms without
925
+ // telling them: the instance is done at once, even mid-submit, and what waited on a validation
926
+ // run stops waiting. There is nothing left to release.
927
+ #registryDisposed() {
928
+ this.#disposed = true;
929
+ clearTimeout(this.#timer);
930
+ this.#releases = [];
931
+ this.#release();
932
+ }
842
933
  }
@@ -1,4 +1,4 @@
1
- import { equals, getIn, nameOf, pathId } from "./paths.js";
1
+ import { equals, getIn, nameOf, pathId, toIndexPath } from "./paths.js";
2
2
  export class FieldImpl {
3
3
  #engine;
4
4
  keyPath;
@@ -32,9 +32,16 @@ export class FieldImpl {
32
32
  this.#engine.setTouched(this.keyPath, touched);
33
33
  }
34
34
  get dirty() {
35
- const index = this.#engine.indexPath(this.keyPath);
36
- return (index !== undefined &&
37
- !equals(getIn(this.#engine.values, index), getIn(this.#engine.state.initial, index)));
35
+ const { state } = this.#engine;
36
+ const index = toIndexPath(state.keys, this.keyPath);
37
+ if (index === undefined) {
38
+ return false;
39
+ }
40
+ // Under an array item, the initial value is that item's, wherever it was: by key, not index.
41
+ const initialIndex = toIndexPath(state.initialKeys, this.keyPath);
42
+ // An item added since (by append or replace) has no initial value.
43
+ return (initialIndex === undefined ||
44
+ !equals(getIn(this.#engine.values, index), getIn(state.initial, initialIndex)));
38
45
  }
39
46
  get validating() {
40
47
  return this.#engine.state.validating;
@@ -66,6 +66,9 @@ const emitDefault = (issue, path, emit) => {
66
66
  emit([...path, ...(formatted.path ?? [])], formatted.message);
67
67
  }
68
68
  };
69
+ const asInvalidType = (issue) => new SchemaIssue.InvalidType(issue.ast, issue.input, {
70
+ reportInput: SchemaIssue.hasInput(issue),
71
+ });
69
72
  const walkFilter = (issue, path, hooks, emit, walk) => {
70
73
  const pointed = pointedPaths(issue.issue, path);
71
74
  const annotated = findMessage(issue.issue) ?? findMessage(issue);
@@ -119,8 +122,13 @@ export const toPathIssues = (issue, hooks) => {
119
122
  }
120
123
  case "Composite":
121
124
  case "AnyOf": {
122
- if (current.issues.length === 0) {
123
- emitDefault(current, path, emit);
125
+ // A value no member of the union applies to (one outside a Literals, say) fails with no
126
+ // inner issues. Effect's formatter reports it without calling a hook. Walked here as the
127
+ // InvalidType a single Literal gives, so `leafHook` names it, and the fallback is
128
+ // Effect's same `Expected "a" | "b"`.
129
+ if (current._tag === "AnyOf" && current.issues.length === 0) {
130
+ walk(asInvalidType(current), path);
131
+ return;
124
132
  }
125
133
  for (const inner of current.issues) {
126
134
  walk(inner, path);
@@ -55,6 +55,11 @@ export interface ExternalError {
55
55
  export interface State {
56
56
  readonly initial: unknown;
57
57
  readonly keys: Keys;
58
+ /**
59
+ * Item keys for `initial`'s arrays, shared with `keys` for the items that are still there: a
60
+ * field compares with its own item's initial value, wherever the item has moved.
61
+ */
62
+ readonly initialKeys: Keys;
58
63
  readonly touched: ReadonlySet<string>;
59
64
  /** Schema errors on show, by path. A path drops out once its check passes. */
60
65
  readonly revealed: ReadonlySet<string>;
@@ -64,6 +69,8 @@ export interface State {
64
69
  readonly issues: ReadonlyMap<string, string>;
65
70
  /** The values that run decoded, to tell whether its result is current. */
66
71
  readonly checked: unknown;
72
+ /** The item keys of `checked`. */
73
+ readonly checkedKeys: Keys;
67
74
  readonly decoded: Option.Option<unknown>;
68
75
  /** Why the latest run couldn't decode at all (a defect, a runtime that failed), if it couldn't. */
69
76
  readonly failure: Cause.Cause<unknown> | undefined;
@@ -77,4 +84,6 @@ export interface State {
77
84
  readonly encoded: unknown;
78
85
  readonly decoded: unknown;
79
86
  }>;
87
+ /** The item keys of `lastSubmitted`'s encoded values. */
88
+ readonly lastSubmittedKeys: Keys;
80
89
  }
@@ -1,3 +1,4 @@
1
+ import { Schema } from "effect";
1
2
  import { AsyncResult } from "effect/reactivity";
2
3
  /** Sets the default for this component's children. Call during component init. */
3
4
  export declare const setRevalidateOnHydrate: (value: boolean) => void;
@@ -27,7 +28,7 @@ export declare const encodeSeed: (key: string, encode: (value: AsyncResult.Async
27
28
  * the browser's schema: the browser then computes the atom itself, as for a result the server
28
29
  * couldn't encode. Development builds warn.
29
30
  */
30
- export declare const decodeSeed: (key: string, value: unknown, decode: (value: unknown) => unknown) => unknown;
31
+ export declare const decodeSeed: (key: string, value: unknown, decode: (value: Schema.Json) => unknown) => unknown;
31
32
  /**
32
33
  * Whether a seed is still waiting, as a stream's result is between its values. The browser runs
33
34
  * the atom again from it, as the server's run ended with the render.
@@ -5,6 +5,7 @@ import { Cause, Schema } from "effect";
5
5
  import { AsyncResult } from "effect/reactivity";
6
6
  import { DEV } from "esm-env";
7
7
  import { createContext } from "svelte";
8
+ import { troubleshooting } from "./troubleshooting.js";
8
9
  const [getDefault, setDefault, hasDefault] = createContext();
9
10
  /** Sets the default for this component's children. Call during component init. */
10
11
  export const setRevalidateOnHydrate = (value) => {
@@ -45,7 +46,7 @@ export const encodeSeed = (key, encode, result) => {
45
46
  throw error;
46
47
  }
47
48
  if (DEV) {
48
- console.warn(`effect-atom-svelte: the result of the serializable atom "${key}" doesn't encode with its schema, so it isn't sent to the browser, which computes the atom again.`, error);
49
+ console.warn(`effect-atom-svelte: the result of the serializable atom "${key}" doesn't encode with its schema, so it isn't sent to the browser, which computes the atom again. ${troubleshooting("doesnt-encode-with-its-schema-so-it-isnt-sent-to-the-browser")}`, error);
49
50
  }
50
51
  return noSeed;
51
52
  }
@@ -60,6 +61,9 @@ export const decodeSeed = (key, value, decode) => {
60
61
  return undefined;
61
62
  }
62
63
  try {
64
+ // SAFETY: what the server's encode returned, sent through `hydratable`; decode checks it against
65
+ // the schema, and throws a schema error, handled below, for anything else.
66
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- hydratable types the value it returns as unknown
63
67
  return decode(value);
64
68
  }
65
69
  catch (error) {
@@ -67,7 +71,7 @@ export const decodeSeed = (key, value, decode) => {
67
71
  throw error;
68
72
  }
69
73
  if (DEV) {
70
- console.warn(`effect-atom-svelte: the server's result for the serializable atom "${key}" doesn't decode with its schema, so the browser computes the atom again.`, error);
74
+ console.warn(`effect-atom-svelte: the server's result for the serializable atom "${key}" doesn't decode with its schema, so the browser computes the atom again. ${troubleshooting("doesnt-decode-with-its-schema-so-the-browser-computes-the-atom-again")}`, error);
71
75
  }
72
76
  return undefined;
73
77
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Throws unless `version` is SvelteKit 3. Remote queries keep the state fields and promise that
3
+ * remoteQueryAtom follows from SvelteKit 3.0.0, and SvelteKit 4 may change them.
4
+ */
5
+ export declare const assertSupportedKit: (version: string) => void;
@@ -0,0 +1,11 @@
1
+ // Which SvelteKit versions remoteQueryAtom supports. Internal: not exported from the package.
2
+ import { troubleshooting } from "./troubleshooting.js";
3
+ /**
4
+ * Throws unless `version` is SvelteKit 3. Remote queries keep the state fields and promise that
5
+ * remoteQueryAtom follows from SvelteKit 3.0.0, and SvelteKit 4 may change them.
6
+ */
7
+ export const assertSupportedKit = (version) => {
8
+ if (!version.startsWith("3.")) {
9
+ throw new Error(`remoteQueryAtom needs SvelteKit 3, with experimental.remoteFunctions turned on, and this app has SvelteKit ${version}. ${troubleshooting("remotequeryatom-needs-sveltekit-3")}`);
10
+ }
11
+ };
@@ -1,6 +1,6 @@
1
1
  // The one place the inspector (../Inspector.ts) relies on how Effect implements AtomRegistry rather
2
- // than on its public API. Written against effect 4.0.1 (effect/src/reactivity/AtomRegistry.ts); the
3
- // peer range is ~4.0.0 because of code like this.
2
+ // than on its public API. Written against effect 4.0.1 and checked against 4.1.7 (effect/src/reactivity/AtomRegistry.ts);
3
+ // the peer range is ~4.1.0 because of code like this.
4
4
  //
5
5
  // The public `AtomRegistry` announces nodes added and removed (`onNodeAdded`, `onNodeRemoved`), and
6
6
  // a `Node` shows its parents, children and listeners. It says nothing when a node's value changes,
@@ -0,0 +1,5 @@
1
+ import type { Atom } from "effect/reactivity";
2
+ /** Records an atom made by remoteQueryAtom. */
3
+ export declare const markRemoteQueryAtom: (atom: Atom.Atom<unknown>) => void;
4
+ /** Whether remoteQueryAtom made this atom, or it is a copy of one. */
5
+ export declare const isRemoteQueryAtom: (atom: Atom.Atom<unknown>) => boolean;
@@ -0,0 +1,11 @@
1
+ // Which atoms remoteQueryAtom made, so the hooks can warn about reading one in a way that renders
2
+ // Initial on the server. Internal: not exported from the package. Kept apart from SvelteKit.ts so
3
+ // the hooks don't import SvelteKit.
4
+ // An own enumerable property, so the copies Atom.keepAlive, Atom.withLabel and the like make keep it.
5
+ const marker = Symbol.for("effect-atom-svelte/remoteQueryAtom");
6
+ /** Records an atom made by remoteQueryAtom. */
7
+ export const markRemoteQueryAtom = (atom) => {
8
+ Reflect.set(atom, marker, true);
9
+ };
10
+ /** Whether remoteQueryAtom made this atom, or it is a copy of one. */
11
+ export const isRemoteQueryAtom = (atom) => Object.hasOwn(atom, marker);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-atom-svelte",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Community-built Svelte 5 bindings for Effect Atom (not affiliated with the Effect team)",
5
5
  "keywords": [
6
6
  "effect",
@@ -53,15 +53,16 @@
53
53
  },
54
54
  "devDependencies": {
55
55
  "@demo/domain": "workspace:*",
56
- "@sveltejs/package": "^3.0.1",
57
- "@sveltejs/vite-plugin-svelte": "^7.3.1",
56
+ "@sveltejs/kit": "^3.4.1",
57
+ "@sveltejs/package": "^3.0.3",
58
+ "@sveltejs/vite-plugin-svelte": "^7.3.2",
58
59
  "@types/bun": "^1.4.2",
59
60
  "@vitest/browser": "^5.0.5",
60
61
  "@vitest/browser-playwright": "^5.0.5",
61
- "effect": "4.0.5",
62
+ "effect": "4.1.7",
62
63
  "playwright": "^1.57.0",
63
- "svelte": "^5.57.2",
64
- "svelte-check": "^4.7.6",
64
+ "svelte": "^5.59.2",
65
+ "svelte-check": "^4.7.8",
65
66
  "typescript": "^6.0.3",
66
67
  "vite": "^8.3.4",
67
68
  "vitest": "^5.0.5",
@@ -69,7 +70,7 @@
69
70
  },
70
71
  "peerDependencies": {
71
72
  "@sveltejs/kit": "^2.0.0 || ^3.0.0",
72
- "effect": "~4.0.0",
73
+ "effect": "~4.1.0",
73
74
  "svelte": "^5.57.2"
74
75
  },
75
76
  "peerDependenciesMeta": {