Skip to content

Commit

Permalink
refactor: subscribe to vis color store on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
mgadewoll committed Nov 7, 2024
1 parent c4b2b7d commit 9aeec86
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ export default () => {
};

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
handleVisColorThemeChange
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export default () => {
};

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
handleVisColorThemeChange
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,16 @@ export default () => {
const [palettes, setPalettes] = useState(getPalettes());

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(VIS_COLOR_STORE_EVENTS.UPDATE, () => {
setPalettes(getPalettes());
});
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
() => {
setPalettes(getPalettes());
}
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, [getPalettes]);

const onChangeSize = (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,16 @@ export default () => {
const [selectedPalette, setSelectedPalette] = useState('palette_1');

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(VIS_COLOR_STORE_EVENTS.UPDATE, () => {
setPalettes(getPalettes());
});
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
() => {
setPalettes(getPalettes());
}
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

return (
Expand Down
13 changes: 10 additions & 3 deletions packages/eui/src-docs/src/views/elastic_charts/theming.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ export default () => {
);

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(VIS_COLOR_STORE_EVENTS.UPDATE, () => {
setPalettes(getPalettes());
});
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
() => {
setPalettes(getPalettes());
}
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ export const Playground: Story = {

// subscribe to theme-related vis_color changes
useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(VIS_COLOR_STORE_EVENTS.UPDATE, () => {
setPalette(euiPaletteColorBlind());
});
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
() => {
setPalette(euiPaletteColorBlind());
}
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

useUpdateEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ export const Playground: Story = {

// subscribe to theme-related vis_color changes
useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(VIS_COLOR_STORE_EVENTS.UPDATE, () => {
setPalettes(getInitialPalettes());
});
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
() => {
setPalettes(getInitialPalettes());
}
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

useUpdateEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ export default () => {
};

useEffect(() => {
EUI_VIS_COLOR_STORE.subscribe(
const storeId = EUI_VIS_COLOR_STORE.subscribe(
VIS_COLOR_STORE_EVENTS.UPDATE,
handleVisColorThemeChange
);

return () => {
EUI_VIS_COLOR_STORE.unsubscribe(VIS_COLOR_STORE_EVENTS.UPDATE, storeId);
};
}, []);

return (
Expand Down

0 comments on commit 9aeec86

Please sign in to comment.