Skip to content

Instantly share code, notes, and snippets.

View internet-john's full-sized avatar
🧩

john internet-john

🧩
View GitHub Profile
simply, a thunk is a function that returns a function. in our case, thunk middleware holds a ref to the store, namely dispatch and getState. this occurs at the time you create your store and pass applyMiddleware as arg.
now, whenever you dispatch an action, thunk will intercept this function, attach dispatch and getState, providing us the ability to dispatch n times and have access to the store state. powerful stuff.
let's take a look at some code:
function createThunkMiddleware(extraArgument) {
return function (_ref) {
var dispatch = _ref.dispatch,
getState = _ref.getState;
const mainThread = initMainThread();
mainThread.processScript()
while (mainThread.processing) {
if (work) callStack.push(work)
}
const callStack = () => {
while (callStack.executing()) {
@internet-john
internet-john / Parse, filter nested graphQL payload
Last active January 23, 2018 01:18
Extracting specific value from deeply nested payload
As a client side developer, it is commonplace to need to parse and structure payloads in order to consume and display properly
within the UI. Some considerations during this process:
* Is the data we are after always present? Perhaps some objects in payload do not contain field
* What is the average payload size? The larger it is, a more efficient algorithmic procedure is required
In my current project, we are often faced with massive payloads that could, quite literally, have an infinite amount of nested
fields.
For context, I needed the ID of every category a user has created. A category can contain three sublevels
e.g.