Local offline syncMap fails to resolve after creation #128
Replies: 7 comments 1 reply
-
It is by design. We expect you to load |
Beta Was this translation helpful? Give feedback.
-
Why do you need to load it before you create it? (I didn’t get what check do you need) |
Beta Was this translation helpful? Give feedback.
-
I don't know if it's created or not at the time of loading. In my specific case: I have external IDs and have optional metadata syncMap for that ID (custom avatar, personal notes and stuff). If I find this metadata locally, I'll show something fancy for the person, if there's nothing, I'll just show a boring placeholder avatar. I found this bug when we implemented metadata add form which is located nearby component that loads syncMap. Is there any exists() call or something? |
Beta Was this translation helpful? Give feedback.
-
You can use Or you can just do |
Beta Was this translation helpful? Give feedback.
-
I can't, because I need to update the avatar when meta is changed. I still need
So basically I have to suppress this error and resubscribe when I encounter it? Wouldn't that be an infinite subscribe/throw/resubscribe cycle? |
Beta Was this translation helpful? Give feedback.
-
This is what I came up with so far. It seems to solve my case import { type SyncMapCreatedAction, type SyncMapValues } from "@logux/actions";
import { LoguxUndoError, type SyncMapStore } from "@logux/client";
export function subscribeUntilFound<
value extends SyncMapValues,
store extends SyncMapStore<value>,
>(store: store, listener: Parameters<store["subscribe"]>[0]): () => void {
const id = store.get().id;
let unbind = store.subscribe(listener);
store.loading.catch((reason) => {
if (
!(reason instanceof LoguxUndoError) ||
reason.action.reason !== "notFound"
)
throw reason;
unbind();
unbind = store.client.type<SyncMapCreatedAction<value>>(
store.plural + "/created",
() => {
unbind();
unbind = store.subscribe(listener);
},
{
event: "add",
id,
},
);
});
return unbind;
} |
Beta Was this translation helpful? Give feedback.
-
Yes, this helper will work. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When I declare offline local
syncMapTemplate
I expect it to be resolved when it's created. This is how I can implement unique check of some fields, for example.But right now, if I subscribe to non-existent map and then create it, it breaks with
Server undid logux/subscribe because of notFound
error.This is reproduction:
Beta Was this translation helpful? Give feedback.
All reactions