aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authormrmr19932015-05-31 14:25:42 +0100
committermrmr19932015-05-31 14:25:42 +0100
commit257a219fdfd33c49b565a93dff9d785824533d2a (patch)
tree36580696db337ea4f1e9fbe0f61828248b21c250 /lib
parentc753194941b3c7a6df8ff328fa36b71c854bc26a (diff)
downloadvimium-257a219fdfd33c49b565a93dff9d785824533d2a.tar.bz2
Add event listeners to settings, support load events
Diffstat (limited to 'lib')
-rw-r--r--lib/settings.coffee13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/settings.coffee b/lib/settings.coffee
index 2dd6722b..b5021225 100644
--- a/lib/settings.coffee
+++ b/lib/settings.coffee
@@ -31,6 +31,8 @@ Sync =
for own key, value of items
Settings.storeAndPropagate key, value if @shouldSyncKey key
Settings.isLoaded = true
+ unless isPreloaded
+ listener() while listener = Settings.eventListeners.load?.pop()
# Asynchronous message from synced storage.
handleStorageUpdate: (changes, area) ->
@@ -70,7 +72,13 @@ else
root.Settings = Settings =
isLoaded: isPreloaded
cache: settingsCache
- init: -> Sync.init()
+ eventListeners: {}
+
+ init: ->
+ Sync.init()
+ if isPreloaded
+ listener() while listener = Settings.eventListeners.load?.pop()
+
get: (key) ->
console.log "WARNING: Settings have not loaded yet; using the default value for #{key}." unless @isLoaded
if (key of @cache) then JSON.parse(@cache[key]) else @defaults[key]
@@ -91,6 +99,9 @@ root.Settings = Settings =
has: (key) -> key of @cache
+ addEventListener: (eventName, callback) ->
+ (@eventListeners[eventName] ||= []).push callback
+
# For settings which require action when their value changes, add hooks to this object, to be called from
# options/options.coffee (when the options page is saved), and by Settings.storeAndPropagate (when an
# update propagates from chrome.storage.sync).