Last active
June 2, 2022 08:39
-
-
Save bezhermoso/c687e54f6bec59eecb32 to your computer and use it in GitHub Desktop.
`memoizeUntil` -- memoize function for n milliseconds
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
var R = require("ramda"); | |
var memoizeUntil = function (fn, expiry) { | |
var cache = {}; | |
var onExpiry = []; | |
var onCache = []; | |
var onCachedResult = []; | |
var memoized = function () { | |
var args = Array.prototype.slice.call(arguments, 0); | |
var hash = hashArgs(args); | |
var result; | |
if (!R.has(hash, cache)) { | |
result = fn.apply(this, args); | |
cache[hash] = result; | |
R.forEach(function(f) { f(result, args, expiry); }, onCache); | |
setTimeout(function() { | |
var v = cache[hash]; | |
delete cache[hash]; | |
R.forEach(function(f) { f(v, args, expiry); }, onExpiry); | |
}, expiry); | |
} else { | |
result = cache[hash]; | |
R.forEach(function(f) { f(result, args, expiry); }, onCachedResult); | |
} | |
return result; | |
}; | |
memoized.onExpiry = function (f) { | |
onExpiry = R.concat(onExpiry, [f]); | |
return memoized; | |
}; | |
memoized.onCache = function (f) { | |
onCache = R.concat(onCache, [f]); | |
return memoized; | |
}; | |
memoized.onCachedResult = function (f) { | |
onCachedResult = R.concat(onCachedResult, [f]); | |
}; | |
return memoized; | |
}; | |
var getAuthToken = function () { | |
return new Promise(...); | |
}; | |
getAuthToken = memoizeUntil(getAuthToken, 10 * 60 * 1000); // Remember until 10 minutes has passed. | |
getAuthToken.onCache(function (token, args, timeout) { | |
console.log("Caching auth token for appId `" + args[0] + "` for " + (timeout / 1000) + " seconds"); | |
}); | |
getAuthToken.onCachedResult(function(token, args, expiry) { | |
token.then(function (token) { | |
console.log("Returning cached token: `" + token + "`"); | |
}); | |
}); | |
getAuthToken.onExpiry(function(token, args, expiry) { | |
console.log(String(expiry / 1000) + " seconds has elapsed -- cached token for appId `" + args[0] + "` has expired."); | |
}); | |
getAuthToken(appId, appSecret).then(function() { ... }); // -> "Caching auth token for appId `myaddid` for 600 seconds | |
getAuthToken(appId, appSecret).then(function() { ... }); // -> "Returning cached token: `...` | |
getAuthToken(appId, appSecret).then(function() { ... }); // -> "Returning cached token: `...` | |
getAuthToken(appId, appSecret).then(function() { ... }); // -> "Returning cached token: `...` | |
setTimeout(function() { | |
getAuthToken(appId, appSecret).then(function() { ... }); // -> "Returning cached token: `...` | |
}, 5 * 60 * 1000); // Wait 5 minutes. | |
setTimeout(function() { | |
console.log("Token should have expired by now..."); | |
getAuthToken(appId, appSecret).then(function() { ... }); | |
}, 11 * 60 * 1000); // Wait 11 minutes. | |
//10 minutes after... | |
// console.log -> 600 seconds has elapsed -- cached token for appId `myaddid` has expired. | |
//11 minutes after... | |
// console.log -> Token should have expired by now... | |
// console.log -> "Returning cached token: `...` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment