Skip to content

Commit

Permalink
try awaiting runSubscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
andykawabata committed Sep 11, 2024
1 parent 633d993 commit 74ec353
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/store/subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
import { capitalize } from "./utils.js";

export default function subscribeActions(store, pageDefinition) {
store.$onAction(({ name, store, args, after }) => {
//before action is called
runSubscriptions(name, args, store, pageDefinition, "before");
after(() => {
runSubscriptions(name, args, store, pageDefinition, "after");
store.$onAction(async ({ name, store, args, after }) => {
// issues with this approach:
// using await here causes the after() callback not to run
// also, the action itself does not wait for runSubscriptions to finish ie beforeSet is not completing before setting the filter
await runSubscriptions(name, args, store, pageDefinition, "before");
after(async () => {
await runSubscriptions(name, args, store, pageDefinition, "after");
});
});
}

function runSubscriptions(name, args, store, pageDefinition, hook) {
async function runSubscriptions(name, args, store, pageDefinition, hook) {
// for loadData actions, both toggle the dataLoading state variable
// and also check for before/after loadData hooks
if (name === "loadData") {
store.toggleDataLoading();
if (pageDefinition[`${hook}LoadData`]) {
pageDefinition[`${hook}LoadData`]({ name, args }, store);
await pageDefinition[`${hook}LoadData`]({ name, args }, store);
}
} else if (name.includes("Filter") && name !== "setFilter") {
// iterate over filters to find if any match the action name
// and run their before/after hooks
Object.keys(store.getFilters).forEach((filterKey) => {
const filterKeys = Object.keys(store.getFilters);
// use for of because forEach does not work with async
for (const filterKey of filterKeys) {
const filterActionString = `set${capitalize(filterKey)}Filter`;
if (
name === filterActionString &&
store.getFilters[filterKey][`${hook}Set`]
) {
store.getFilters[filterKey][`${hook}Set`]({ name, args }, store);
await store.getFilters[filterKey][`${hook}Set`]({ name, args }, store);
if (hook === "after" && !store.getFilters[filterKey].synchronous) {
await store.loadData();
}
}
});
}
} else if (name.includes("ChartData") && name !== "setChartData") {
// iterate over hcarts to find if any match the action name
// and run their before/after hooks
Object.keys(store.getCharts).forEach((chartKey) => {
const chartKeys = Object.keys(store.getCharts);
for (const chartKey of chartKeys) {
const chartActionString = `set${capitalize(chartKey)}ChartData`;
if (
name === chartActionString &&
store.getCharts[chartKey][`${hook}Set`]
) {
store.getCharts[chartKey][`${hook}Set`]({ name, args }, store);
await store.getCharts[chartKey][`${hook}Set`]({ name, args }, store);
}
});
}
}

if (pageDefinition.extendSubscriptions) {
Expand Down

0 comments on commit 74ec353

Please sign in to comment.