The title may sound confusing, because the question is too.
I have a function - query(params: FooBar). This function returns a Svelte store, which I would like to base a derived store from.
let result = query("all users in group 123");
let userAvatars = derived(result, ($result) => { /* ... */ });
So far, so good - but the problem is that the query() call itself is actually dependent on a store value.
let groupId = getGroupId();
let result = query("all users in group " + $groupId);
let userAvatars = derived(result, ($result) => { /* ... */ });
The problem is that the parameter to query() only gets evaluated once and does not update when groupId does. I thought of two approaches:
1 - Making result itself a derived store:
let result = derived(groupId, ($groupId) => query("all users in group " + $groupId));
This doesn't work, because the function passed as the second param to derived() is expected to return the value directly, but I can only return the store.
2 - Making it a reactive statement:
$: result = query("all users in group " + $groupId);
Which doesn't work either, because the the result variable gets reassigned, and hence the userAvatars derived store doesn't react to the changes of the new store.
I am fairly certain it's possible by using the store API directly (e.g. .subscribe()) but I don't see a clear way forward, and it also might require a lot of boilerplate. Is there something I missed?