diff options
| author | Stephen Blott | 2015-06-20 09:54:17 +0100 | 
|---|---|---|
| committer | Stephen Blott | 2015-06-20 09:54:17 +0100 | 
| commit | b09822eb349ec88a573d4f450e9b57e8fa3c6473 (patch) | |
| tree | 0cabb553db04d2d43b2c5472ec7fed9adc576121 | |
| parent | 1e671e28fd1f348b17ee16351c1a0b03f9d15e4f (diff) | |
| download | vimium-b09822eb349ec88a573d4f450e9b57e8fa3c6473.tar.bz2 | |
Fix SimpleCache bugs.
This fixes two bugs in SimpleCache.
1. Rotate the cache on the next tick.  There is a marginally small
   chance that the cache will rotate between calls to .has() and .get().
   So, we do the rotation ansynchronously.  This guarantees that these
   two functions will always see the same cache state.
2. The implementation of .clear() (which is unused, I think) has at some
   point become out of date (and incorrect) w.r.t. the implementation of
   .rotate().
| -rw-r--r-- | lib/utils.coffee | 13 | 
1 files changed, 7 insertions, 6 deletions
| diff --git a/lib/utils.coffee b/lib/utils.coffee index 93045f32..de335452 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -323,14 +323,15 @@ class SimpleCache        null    rotate: (force = false) -> -    if force or @entries < Object.keys(@cache).length or @expiry < new Date() - @lastRotation -      @lastRotation = new Date() -      @previous = @cache -      @cache = {} +    Utils.nextTick => +      if force or @entries < Object.keys(@cache).length or @expiry < new Date() - @lastRotation +        @lastRotation = new Date() +        @previous = @cache +        @cache = {}    clear: -> -    @rotate true -    @rotate true +    @cache = {} +    @previous = {}  # This is a simple class for the common case where we want to use some data value which may be immediately  # available, or for which we may have to wait.  It implements a use-immediately-or-wait queue, and calls the | 
