Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Created June 14, 2017 10:26
Show Gist options
  • Save notgiorgi/cfdb155f4942b6eff813af02e822379e to your computer and use it in GitHub Desktop.
Save notgiorgi/cfdb155f4942b6eff813af02e822379e to your computer and use it in GitHub Desktop.
class SetBuffer {
constructor(idFn) {
this.items = new Map()
this.idFn = idFn
}
isEmpty() {
return this.items.size === 0
}
take() {
if (this.isEmpty()) return
const [key, value] = this.items.entries().next().value
this.items.delete(key)
return value
}
put(action) {
const { items, idFn } = this
const id = idFn(action)
if (items.has(id)) {
return
}
items.set(id, action)
}
}
function* request(payload) {
/* ... */
}
export default function* () {
const requestChannel = yield actionChannel(
actions.FETCH,
new SetBuffer(action => action.payload.foo),
)
while (true) { // eslint-disable-line
const { payload } = yield take(requestChannel)
const state = yield select(selector, payload)
if (!state) {
yield request(payload)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment