diff options
| author | Stephen Blott | 2012-11-02 13:10:33 +0000 |
|---|---|---|
| committer | Stephen Blott | 2014-04-18 21:39:58 +0100 |
| commit | 3aa47a198f2424ba91bb7a8ac07d6b2c53da9698 (patch) | |
| tree | 2912ad714439c1416b39b934b2073b51bb3af8f9 /background_scripts/settings.coffee | |
| parent | 366c0a56a4eed38fa8425fd227cd389181fbed92 (diff) | |
| download | vimium-3aa47a198f2424ba91bb7a8ac07d6b2c53da9698.tar.bz2 | |
Initial synchronization commit.
Synchronization is via `chrome.storage.sync.*`; data is cached in `localStorage`.
Diffstat (limited to 'background_scripts/settings.coffee')
| -rw-r--r-- | background_scripts/settings.coffee | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index 0fe1e1bb..64fce308 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -7,17 +7,49 @@ root.Settings = Settings = get: (key) -> if (key of localStorage) then JSON.parse(localStorage[key]) else @defaults[key] - set: (key, value) -> + # The doNotSync argument suppresses calls to chrome.storage.sync.* while running unit tests + set: (key, value, doNotSync) -> # don't store the value if it is equal to the default, so we can change the defaults in the future - if (value == @defaults[key]) - @clear(key) - else - localStorage[key] = JSON.stringify(value) + # warning: this test is always false for settings with numeric default values (such as scrollStepSize) + if ( value == @defaults[key] ) + return @clear(key,doNotSync) + # don't update the key/value if it's unchanged; thereby suppressing unnecessary calls to chrome.storage + valueJSON = JSON.stringify value + if localStorage[key] == valueJSON + return localStorage[key] + # we have a new value: so update chrome.storage and localStorage + root.Sync.set key, valueJSON if root?.Sync?.set + localStorage[key] = valueJSON - clear: (key) -> delete localStorage[key] + # The doNotSync argument suppresses calls to chrome.storage.sync.* while running unit tests + clear: (key, doNotSync) -> + if @has key + root.Sync.clear key if root?.Sync?.clear + delete localStorage[key] has: (key) -> key of localStorage + # the postUpdateHooks handler below is called each time an option changes: + # either from options/options.coffee (when the options page is saved) + # or from background_scripts/sync.coffee (when an update propagates from chrome.storage) + # + # NOTE: + # this has been refactored and renamed from options.coffee(postSaveHooks): + # - refactored because it is now also called from sync.coffee + # - renamed because it is no longer associated only with "Save" operations + # + postUpdateHooks: + keyMappings: (value) -> + root.Commands.clearKeyMappingsAndSetDefaults() + root.Commands.parseCustomKeyMappings value + root.refreshCompletionKeysAfterMappingSave() + + # postUpdateHooks convenience wrapper + doPostUpdateHook: (key, value) -> + if @postUpdateHooks[key] + @postUpdateHooks[key] value + + # options/options.(coffee|html) only handle booleans and strings; therefore # all defaults must be booleans or strings defaults: |
