UNPKG

@newrelic/gatsby-theme-newrelic

Version:

[![Community Project header](https://github.com/newrelic/opensource-website/raw/master/src/images/categories/Community_Project.png)](https://opensource.newrelic.com/oss-category/#community-project)

30 lines (27 loc) 742 B
const globalState = {}; const createGlobalState = (key, thisCallback, initialValue) => { if (!globalState[key]) { globalState[key] = { callbacks: [], value: initialValue }; } globalState[key].callbacks.push(thisCallback); return { deregister() { const arr = globalState[key].callbacks; const index = arr.indexOf(thisCallback); if (index > -1) { arr.splice(index, 1); } }, emit(value) { if (globalState[key].value !== value) { globalState[key].value = value; globalState[key].callbacks.forEach((callback) => { if (thisCallback !== callback) { callback(value); } }); } }, }; }; export default createGlobalState;