Last active
April 25, 2019 02:02
-
-
Save TobiahRex/89d72ff0cfc3215c46d6482171e00c80 to your computer and use it in GitHub Desktop.
getSingle Control Flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
v4
is NOT fromimmutable
- should be fromuuid
. oops.