From 257a219fdfd33c49b565a93dff9d785824533d2a Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sun, 31 May 2015 14:25:42 +0100 Subject: Add event listeners to settings, support load events --- lib/settings.coffee | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib') 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). -- cgit v1.2.3