Skip to content

Instantly share code, notes, and snippets.

@TobiahRex
Last active April 25, 2019 02:02
Show Gist options
  • Save TobiahRex/89d72ff0cfc3215c46d6482171e00c80 to your computer and use it in GitHub Desktop.
Save TobiahRex/89d72ff0cfc3215c46d6482171e00c80 to your computer and use it in GitHub Desktop.
getSingle Control Flow
import { v4 as generateId } from 'uuid';
function SomeModule() {
const [reqIds, setReqIds] = useState([]);
const [state, localDispatch] = useReducer(createReducer(reqIds), initialState);
function getSomeData(reqId) {
const newReqId = generateId();
setReqIds(newReqId)
getSingle(
['ASSET_COMPANY_PROFILE'],
[wsDispatch(
localDispatch(
(msg) => ({
type: types.RECEIVE_COMPANY_INFO,
payload: msg.payload,
meta: { newReqId } // pass in a unique id generated by the module
})
)
)],
[{ type: 'ASSET_COMPANY_PROFILE', payload: { providerId } }],
);
}
function createReducer(reqIds) { // uses closure to save a list of possible reqIds for this instance of the reducer.
const currentReqIds = reqIds;
return function SomeModuleReducer(state, action) {
if (!currentReqIds.includes(action.meta.reqId)) return state;
// do stuff
}
}
}
@TobiahRex
Copy link
Author

TobiahRex commented Apr 25, 2019

v4 is NOT from immutable - should be from uuid. oops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment