Skip to content

Instantly share code, notes, and snippets.

@jhillacre
Created May 31, 2021 16:54
Show Gist options
  • Save jhillacre/569ab84b0d375f70a3c4d4a791b096fa to your computer and use it in GitHub Desktop.
Save jhillacre/569ab84b0d375f70a3c4d4a791b096fa to your computer and use it in GitHub Desktop.
// this ia snippit from a factory that makes vuex actions with. we make a module per stream.
// these are vueX actions, mutations and state are implied.
async subscribeActivity(context) {
context.commit("setActivitySubscriptionCount");
if (context.state.activitySubscriptions.count === 1) {
const listPromise = context.dispatch("list", { args: {} });
const subscribePromise = dcrfClient.subscribe(
stream,
{},
async (data, action) => {
if (!context.state.activitySubscriptions.listPromise) {
return;
}
const listId = await context.state.activitySubscriptions.listPromise;
if (action === "delete") {
context.commit("removeFromList", { listId, objectId: data.pk });
context.commit("deleteObject", data.pk);
} else {
context.commit("setObject", data);
context.commit("addToList", { listId, objectId: data.id });
}
},
{
create: true,
subscribeAction: "subscribe_activity",
unsubscribeAction: "unsubscribe_activity",
}
);
context.commit("setActivitySubscription", {
subscription: subscribePromise,
listPromise,
});
return listPromise;
}
return context.state.activitySubscriptions.listPromise;
}
async unsubscribeActivity(context) {
if (context.state.activitySubscriptions.count >= 1) {
const subscription = context.state.activitySubscriptions.subscription;
const listPromise = context.state.activitySubscriptions.listPromise;
context.commit("deleteActivitySubscriptionCount");
if (context.state.activitySubscriptions.count <= 0) {
await subscription.cancel();
context.commit("deleteList", await listPromise);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment