Skip to content

Commit

Permalink
added setTimeout fix for long durations
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed May 18, 2020
1 parent 126320d commit f34bf70
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 1,774 deletions.
882 changes: 0 additions & 882 deletions dist/main/apicache.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/main/apicache.js.map

This file was deleted.

880 changes: 0 additions & 880 deletions dist/module/apicache.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/module/apicache.js.map

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"lint": "eslint src",
"test:old": "nyc mocha $(find test -name '*_test.js') --recursive",
"pretest": "yarn build",
"test": "nyc jest --verbose",
"dev": "yarn test --watch",
"test:coverage": "yarn test && yarn coverage",
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MemoryCache from './memory-cache'
import pkg from '../package.json'
import { setLongTimeout } from './utils/setLongTimeout'

var t = {
ms: 1,
Expand Down Expand Up @@ -143,10 +144,10 @@ function ApiCache() {
memCache.add(key, value, duration, expireCallback)
}

// add automatic cache clearing from duration, includes max limit on setTimeout
timers[key] = setTimeout(function () {
// add automatic cache clearing from duration, includes max limit on setLongTimeout
timers[key] = setLongTimeout(function () {
instance.clear(key, true)
}, Math.min(duration, 2147483647))
}, duration)
}

function accumulateContent(res, content) {
Expand Down
16 changes: 9 additions & 7 deletions src/memory-cache.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { setLongTimeout } from './utils/setLongTimeout'

function MemoryCache() {
this.cache = {}
this.size = 0
}

MemoryCache.prototype.add = function(key, value, time, timeoutCallback) {
MemoryCache.prototype.add = function (key, value, time, timeoutCallback) {
var old = this.cache[key]
var instance = this

var entry = {
value: value,
expire: time + Date.now(),
timeout: setTimeout(function() {
timeout: setLongTimeout(function () {
instance.delete(key)
return timeoutCallback && typeof timeoutCallback === 'function' && timeoutCallback(value, key)
}, time),
Expand All @@ -22,7 +24,7 @@ MemoryCache.prototype.add = function(key, value, time, timeoutCallback) {
return entry
}

MemoryCache.prototype.delete = function(key) {
MemoryCache.prototype.delete = function (key) {
var entry = this.cache[key]

if (entry) {
Expand All @@ -36,20 +38,20 @@ MemoryCache.prototype.delete = function(key) {
return null
}

MemoryCache.prototype.get = function(key) {
MemoryCache.prototype.get = function (key) {
var entry = this.cache[key]

return entry
}

MemoryCache.prototype.getValue = function(key) {
MemoryCache.prototype.getValue = function (key) {
var entry = this.get(key)

return entry && entry.value
}

MemoryCache.prototype.clear = function() {
Object.keys(this.cache).forEach(function(key) {
MemoryCache.prototype.clear = function () {
Object.keys(this.cache).forEach(function (key) {
this.delete(key)
}, this)

Expand Down
14 changes: 14 additions & 0 deletions src/utils/setLongTimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function setLongTimeout(fn, delay) {
var maxDelay = Math.pow(2, 31) - 1

if (delay > maxDelay) {
var args = arguments
args[1] -= maxDelay

return setTimeout(function () {
setTimeout_.apply(undefined, args)
}, maxDelay)
}

return setTimeout.apply(undefined, arguments)
}

0 comments on commit f34bf70

Please sign in to comment.