diff options
| author | Stephen Blott | 2015-04-25 16:48:45 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-04-25 16:48:45 +0100 |
| commit | e620be90e8a3ce705d6d57298cb93af78f6eecb1 (patch) | |
| tree | 7798b5b8d757312acda3baf30415d67ad3dd8772 | |
| parent | d0ca0745dc6b1818d2197ff51b0ed4d90fb6bc64 (diff) | |
| parent | 838e1a8b97c7d1e868a9d4ed6b362a35c9455252 (diff) | |
| download | vimium-e620be90e8a3ce705d6d57298cb93af78f6eecb1.tar.bz2 | |
Merge pull request #1581 from smblott-github/fetch-settings
Simplify front-end settings logic.
| -rw-r--r-- | background_scripts/main.coffee | 16 | ||||
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 50 |
2 files changed, 34 insertions, 32 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 5f0effcb..e782a217 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -204,12 +204,16 @@ selectSpecificTab = (request) -> # # Used by the content scripts to get settings from the local storage. # -handleSettings = (args, port) -> - if (args.operation == "get") - value = Settings.get(args.key) - port.postMessage({ key: args.key, value: value }) - else # operation == "set" - Settings.set(args.key, args.value) +handleSettings = (request, port) -> + switch request.operation + when "get" # Get a single settings value. + port.postMessage key: request.key, value: Settings.get request.key + when "set" # Set a single settings value. + Settings.set request.key, request.value + when "fetch" # Fetch multiple settings values. + values = request.values + values[key] = Settings.get key for own key of values + port.postMessage { values } refreshCompleter = (request) -> completers[request.name].refresh() diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 8a11cd70..e7aea163 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -46,18 +46,26 @@ textInputXPath = (-> # must be called beforehand to ensure get() will return up-to-date values. # settings = - port: null - values: {} - loadedValues: 0 - valuesToLoad: [ "scrollStepSize", "linkHintCharacters", "linkHintNumbers", "filterLinkHints", "hideHud", - "previousPatterns", "nextPatterns", "regexFindMode", "userDefinedLinkHintCss", - "helpDialog_showAdvancedCommands", "smoothScroll", "grabBackFocus" ] isLoaded: false + port: null eventListeners: {} + values: + scrollStepSize: null + linkHintCharacters: null + linkHintNumbers: null + filterLinkHints: null + hideHud: null + previousPatterns: null + nextPatterns: null + regexFindMode: null + userDefinedLinkHintCss: null + helpDialog_showAdvancedCommands: null + smoothScroll: null + grabBackFocus: null init: -> - @port = chrome.runtime.connect({ name: "settings" }) - @port.onMessage.addListener(@receiveMessage) + @port = chrome.runtime.connect name: "settings" + @port.onMessage.addListener (response) => @receiveMessage response # If the port is closed, the background page has gone away (since we never close it ourselves). Stub the # settings object so we don't keep trying to connect to the extension even though it's gone away. @@ -67,36 +75,26 @@ settings = # @get doesn't depend on @port, so we can continue to support it to try and reduce errors. @[property] = (->) if "function" == typeof value and property != "get" - get: (key) -> @values[key] set: (key, value) -> @init() unless @port @values[key] = value - @port.postMessage({ operation: "set", key: key, value: value }) + @port.postMessage operation: "set", key: key, value: value load: -> @init() unless @port + @port.postMessage operation: "fetch", values: @values - for i of @valuesToLoad - @port.postMessage({ operation: "get", key: @valuesToLoad[i] }) - - receiveMessage: (args) -> - # not using 'this' due to issues with binding on callback - settings.values[args.key] = args.value - # since load() can be called more than once, loadedValues can be greater than valuesToLoad, but we test - # for equality so initializeOnReady only runs once - if (++settings.loadedValues == settings.valuesToLoad.length) - settings.isLoaded = true - listener = null - while (listener = settings.eventListeners["load"].pop()) - listener() + receiveMessage: (response) -> + @values = response.values if response.values? + @values[response.key] = response.value if response.key? and response.value? + @isLoaded = true + listener() while listener = @eventListeners.load?.pop() addEventListener: (eventName, callback) -> - if (!(eventName of @eventListeners)) - @eventListeners[eventName] = [] - @eventListeners[eventName].push(callback) + (@eventListeners[eventName] ||= []).push callback # # Give this frame a unique (non-zero) id. |
