Add fetch* utils

This commit is contained in:
Michael Jackson
2018-05-21 15:44:00 -07:00
parent ba14f9197f
commit 26ba4698e3
7 changed files with 200 additions and 153 deletions

View File

@ -1,7 +1,20 @@
function createMutex(doWork) {
const invariant = require("invariant");
function defaultCreateKey(payload) {
return payload;
}
function createMutex(doWork, createKey = defaultCreateKey) {
const mutex = Object.create(null);
return (key, payload, callback) => {
return (payload, callback) => {
const key = createKey(payload);
invariant(
typeof key === "string",
"Mutex needs a string key; please provide a createKey function that returns a string"
);
if (mutex[key]) {
mutex[key].push(callback);
} else {