@dxos/echo-atom 0.8.4-main.d05673bc65 → 0.8.4-main.d9fc60f731

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/src/ref-utils.ts CHANGED
@@ -21,19 +21,34 @@ export const loadRefTarget = <T, R>(
21
21
  get: Atom.Context,
22
22
  onTargetAvailable: (target: T) => R,
23
23
  ): R | undefined => {
24
+ // Accessing `ref.target` registers a resolution callback when the target is
25
+ // not yet loaded, so resolution can be observed via `ref.onResolved` below.
24
26
  const currentTarget = ref.target;
25
27
  if (currentTarget) {
26
28
  return onTargetAvailable(currentTarget);
27
29
  }
28
30
 
29
- // Target not loaded yet - trigger async load.
31
+ // Subscribe to the ref's resolution event in case the target loads later
32
+ // (e.g. when a sibling client creates the linked object). Without this,
33
+ // a one-shot async load that fails because the document hasn't propagated
34
+ // would leave the atom permanently undefined.
35
+ const unsubscribe = ref.onResolved(() => {
36
+ const target = ref.target;
37
+ if (target) {
38
+ get.setSelf(onTargetAvailable(target));
39
+ }
40
+ });
41
+ get.addFinalizer(unsubscribe);
42
+
43
+ // Also try async load (e.g. for objects that need disk loading).
30
44
  void ref
31
45
  .load()
32
46
  .then((loadedTarget) => {
33
47
  get.setSelf(onTargetAvailable(loadedTarget));
34
48
  })
35
49
  .catch(() => {
36
- // Loading failed, keep target as undefined.
50
+ // Loading failed; the resolution subscription above will pick up
51
+ // cross-client updates when they arrive.
37
52
  });
38
53
 
39
54
  return undefined;