nodebb-plugin-ns-embed
Version:
Embed media and rich content in posts: youtube, vimeo, twitch etc. All embeds are based on the rules. You are encouraged to build your own rules to embed everything what is embeddable.
49 lines (39 loc) • 1.39 kB
JavaScript
import React, {createContext, useReducer} from 'react';
import {newRule, rules, selectedRule} from './reducers';
export const StoreContext = createContext(null);
export function createInitialState() {
return {
newRule : {},
rules : [],
selectedRule: null
};
}
/**
* Experimental Store Implementation to represent the possibility to have lightweight Store solution with centralized reducer like Redux
*/
export function createStore(initialState) {
let state, dispatch;
function invalidate() {
let [currentState, dispatchRef] = useReducer((state, action) => {
return {
newRule : newRule(state.newRule, action),
rules : rules(state.rules, action),
selectedRule: selectedRule(state.selectedRule, action)
};
}, initialState);
state = currentState;
dispatch = dispatchRef;
}
return {
dispatch : action => dispatch(action),
getState : () => state,
invalidate: () => invalidate()
};
}
export function createStoreProvider(store) {
return ({children}) => {
store.invalidate();
// A component calling useContext will always re-render when the context value changes.
return <StoreContext.Provider value={{store}}>{children}</StoreContext.Provider>;
};
}