reactflow-canvas-store
Version:
Global and multi-canvas state store for React Flow projects.
55 lines (54 loc) • 1.75 kB
JavaScript
// A simple internal store object
const globalState = {};
//non-reactive store
export const getGlobalState = (key) => globalState[key];
export const setGlobalState = (key, value) => {
globalState[key] = value;
};
export const updateGlobalState = (key, value) => {
if (!(key in globalState)) {
throw new Error(`Key "${key}" does not exist in global store`);
}
globalState[key] = value;
};
export const resetGlobalState = () => {
for (const key in globalState) {
delete globalState[key];
}
};
export const hasGlobalState = (key) => key in globalState;
export const getAllGlobalState = () => ({ ...globalState });
export const clearGlobalState = () => {
for (const key in globalState) {
delete globalState[key];
}
};
export const getGlobalStateKeys = () => Object.keys(globalState);
export const getGlobalStateValues = () => Object.values(globalState);
export const getGlobalStateEntries = () => Object.entries(globalState);
export const setGlobalStateEntries = (entries) => {
entries.forEach(([key, value]) => {
globalState[key] = value;
});
};
export const updateGlobalStateEntries = (entries) => {
entries.forEach(([key, value]) => {
if (!(key in globalState)) {
throw new Error(`Key "${key}" does not exist in global store`);
}
globalState[key] = value;
});
};
export const resetGlobalStateEntries = (keys) => {
keys.forEach(key => {
if (key in globalState) {
delete globalState[key];
}
else {
throw new Error(`Key "${key}" does not exist in global store`);
}
});
};
export const hasGlobalStateEntries = (keys) => {
return keys.every(key => key in globalState);
};