diff options
| author | Stephen Blott | 2016-04-01 13:13:25 +0100 |
|---|---|---|
| committer | Stephen Blott | 2016-04-01 13:13:27 +0100 |
| commit | 777af41df142e2a461073aed8c5941d2dfdc24d1 (patch) | |
| tree | 8e19a7dec8486984718d32857c5c14b27cf1c855 /lib | |
| parent | caac7b924af6570c7f3ec3a2f555ba9a45b31813 (diff) | |
| download | vimium-777af41df142e2a461073aed8c5941d2dfdc24d1.tar.bz2 | |
Tweak Settings to add onLoaded() method.
Settings.onLoaded() is similar to Settings.use(), except it does not
require/expect a key argument.
Would could simulate .onLoaded() with .use() by artificially adding an
(unused) key, but that would make the code less clear. So it seems
better to have a separate method.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/settings.coffee | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/settings.coffee b/lib/settings.coffee index 914ef229..70dc1147 100644 --- a/lib/settings.coffee +++ b/lib/settings.coffee @@ -23,7 +23,7 @@ Settings = # For UIComponents (or other content of ours in an iframe within a regular page), we can't access # localStorage, so we check that the top level frame is also an extension page. @cache = if Utils.isBackgroundPage() then localStorage else extend {}, localStorage - @onLoaded() + @runOnLoadedCallbacks() chrome.storage.local.get null, (localItems) => localItems = {} if chrome.runtime.lastError @@ -34,14 +34,17 @@ Settings = chrome.storage.onChanged.addListener (changes, area) => @propagateChangesFromChromeStorage changes if area == "sync" - @onLoaded() + @runOnLoadedCallbacks() # Called after @cache has been initialized. On extension pages, this will be called twice, but that does # not matter because it's idempotent. - onLoaded: -> - @log "onLoaded: #{@onLoadedCallbacks.length} callback(s)" + runOnLoadedCallbacks: -> + @log "runOnLoadedCallbacks: #{@onLoadedCallbacks.length} callback(s)" @isLoaded = true - callback() while callback = @onLoadedCallbacks.pop() + @onLoadedCallbacks.pop()() while 0 < @onLoadedCallbacks.length + + onLoaded: (callback) -> + if @isLoaded then callback() else @onLoadedCallbacks.push callback shouldSyncKey: (key) -> (key of @defaults) and key not in [ "settingsVersion", "previousVersion" ] @@ -84,8 +87,7 @@ Settings = use: (key, callback) -> @log "use: #{key} (isLoaded=#{@isLoaded})" - invokeCallback = => callback @get key - if @isLoaded then invokeCallback() else @onLoadedCallbacks.push invokeCallback + @onLoaded => callback @get key # For settings which require action when their value changes, add hooks to this object. postUpdateHooks: {} |
