Created
June 14, 2017 10:26
-
-
Save notgiorgi/cfdb155f4942b6eff813af02e822379e to your computer and use it in GitHub Desktop.
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
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