From 4d886fe85ccc8e58dae0cd4c8e92ea1b0660a858 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 29 Mar 2013 14:18:44 +0100 Subject: [PATCH] Update $.sync to use the Chrome Storage API and only one event listener. --- lib/$.coffee | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/$.coffee b/lib/$.coffee index 70af75f99..e8922e6a5 100644 --- a/lib/$.coffee +++ b/lib/$.coffee @@ -27,11 +27,6 @@ $.extend $, $.off d, 'DOMContentLoaded', cb fc() $.on d, 'DOMContentLoaded', cb - sync: (key, cb) -> - key = "#{g.NAMESPACE}#{key}" - $.on window, 'storage', (e) -> - if e.key is key - cb JSON.parse e.newValue formData: (form) -> if form instanceof HTMLFormElement return new FormData form @@ -200,6 +195,21 @@ $.extend $, # Round to an integer otherwise. Math.round size "#{size} #{['B', 'KB', 'MB', 'GB'][unit]}" + sync: do -> + cbs = {} +<% if (type === 'crx') { %> + chrome.storage.onChanged.addListener (changes) -> + for key of changes + if cb = cbs[key] + cb changes[key].newValue + return + (key, cb) -> cbs[key] = cb +<% } else { %> + $.on window, 'storage', (e) -> + if cb = cbs[e.key] + cb JSON.parse e.newValue + (key, cb) -> cbs[g.NAMESPACE + key] = cb +<% } %> item: (key, val) -> item = {} item[key] = val