magicseth
magicseth2y ago

Is it possible that adding a second

Is it possible that adding a second subscription to a query with the same parameters won't get an immediate callback with the last value?
14 Replies
magicseth
magicsethOP2y ago
If I make multiple subscriptions at once, they all get updated. Subsequent subscriptions don't get updated unless the document changes. If I add a random unused cache busting string as a parameter, it seems to work as intended
jamwt
jamwt2y ago
yep well, do you mean, they don't even get the current value? the subsequent subs with the same parameters?
ballingt
ballingt2y ago
This depends on the API you're using, e.g.
const watch = client.watchQuery(query, ...args);
watch.onUpdate(() => console.log("callback called"));
const watch = client.watchQuery(query, ...args);
watch.onUpdate(() => console.log("callback called"));
will not run immediately, it will run once a query update is received. That might be pretty quick (if this is a new query, the client will tell the server to start a subscription on it and will get a value in ~the time it takes for a network roundtrip) but it might be a long time if the client already had a subscription on this query. Is this the kind of thing you're talking about? if so you can synchronously check for the current result by checking watch.localQueryResult() to see if it returns something other than undefined If you're using a different API, the same concepts may apply — watchQuery is a ConvexReactClient API, the InternalConvexClient equivalent is client.localQueryResult(query, ...args) for synchronously checking for the current value, only present if there a subscription on this (query, args) combo already.
jamwt
jamwt2y ago
ah forgot, @magicseth is not using react, eh? b/c useQuery should handle this for you from convex/react
magicseth
magicsethOP2y ago
my vue wrapper is calling this:
let queryToken: string = convex!.addSub(randomString, setter, name, fullArgs);
let queryToken: string = convex!.addSub(randomString, setter, name, fullArgs);
If I have already subscribed, I see the ref counter increase but I don't have a cache of values at my layer
ballingt
ballingt2y ago
addSub isn't a Convex function, I think that's yours too? Is it wrapping the InternalConvexClient? I think in there I'd add the setter to your callback list, then call it manually if client.localQueryResult(name, fullArgs) returns something other than undefined
magicseth
magicsethOP2y ago
const { queryToken, unsubscribe } = this.client.subscribe(queryPath, args); That is what my addSub function is so I should add the localQueryResult immediately okay
ballingt
ballingt2y ago
yeah something like
class VueClient {
...
subscribe(queryPath, args, callback) {
if (this.client.localQueryResult(queryPath, args)) {
callback(this.client.localQueryResult(queryPath, args));
}
this.client.subscribe(queryPath, args);
}
}
class VueClient {
...
subscribe(queryPath, args, callback) {
if (this.client.localQueryResult(queryPath, args)) {
callback(this.client.localQueryResult(queryPath, args));
}
this.client.subscribe(queryPath, args);
}
}
magicseth
magicsethOP2y ago
that seems to have done the trick, still some flickering but it is getting updated!
ballingt
ballingt2y ago
Just a shot in the dark, it's possible that batching your updates could fix the flicker https://www.how-to-vue.com/vue/reactivity/batch-update.html
ballingt
ballingt2y ago
but that's really a guess, if you want to donate your Vue client I will take a look and try to fix this and post a https://stack.convex.dev article on it
magicseth
magicsethOP2y ago
Gist
convexvue.ts
convexvue.ts. GitHub Gist: instantly share code, notes, and snippets.
magicseth
magicsethOP2y ago
It's mostly based on Jamie's Solid convex with just a little ref() magic
ballingt
ballingt2y ago
awesome, I'll give this a shot when my boss isn't looking 😛 thanks!

Did you find this page helpful?