Merge pull request #6279 from electron/chrome-storage-return

Always call chrome.storage.*.get callback
This commit is contained in:
Kevin Sawicki 2016-06-28 14:47:32 -07:00 committed by GitHub
commit 8155e71925
3 changed files with 25 additions and 10 deletions

View file

@ -12,11 +12,17 @@ const setStorage = (storageType, storage) => {
window.localStorage.setItem(`__chrome.storage.${storageType}__`, json)
}
const scheduleCallback = (items, callback) => {
setTimeout(function () {
callback(items)
})
}
const getStorageManager = (storageType) => {
return {
get (keys, callback) {
const storage = getStorage(storageType)
if (keys == null) return storage
if (keys == null) return scheduleCallback(storage, callback)
let defaults = {}
switch (typeof keys) {
@ -30,7 +36,7 @@ const getStorageManager = (storageType) => {
}
break
}
if (keys.length === 0) return {}
if (keys.length === 0) return scheduleCallback({}, callback)
let items = {}
keys.forEach(function (key) {
@ -38,10 +44,7 @@ const getStorageManager = (storageType) => {
if (value == null) value = defaults[key]
items[key] = value
})
setTimeout(function () {
callback(items)
})
scheduleCallback(items, callback)
},
set (items, callback) {

View file

@ -1033,7 +1033,10 @@ describe('browser-window module', function () {
assert.equal(message.runtimeId, 'foo')
assert.equal(message.tabId, w.webContents.id)
assert.equal(message.i18nString, 'foo - bar (baz)')
assert.deepEqual(message.storageItems, {foo: 'bar'})
assert.deepEqual(message.storageItems, {
local: {hello: 'world'},
sync: {foo: 'bar'}
})
done()
})
})

View file

@ -6,16 +6,25 @@
<script>
function testStorage (callback) {
chrome.storage.sync.set({foo: 'bar'}, function () {
chrome.storage.sync.get({foo: 'baz'}, callback)
chrome.storage.sync.get({foo: 'baz'}, function (syncItems) {
chrome.storage.local.set({hello: 'world'}, function () {
chrome.storage.local.get(null, function(localItems) {
callback(syncItems, localItems)
})
})
})
})
}
testStorage(function (items) {
testStorage(function (syncItems, localItems) {
var message = JSON.stringify({
runtimeId: chrome.runtime.id,
tabId: chrome.devtools.inspectedWindow.tabId,
i18nString: chrome.i18n.getMessage('foo', ['bar', 'baz']),
storageItems: items
storageItems: {
local: localItems,
sync: syncItems
}
})
var sendMessage = `require('electron').ipcRenderer.send('answer', ${message})`
window.chrome.devtools.inspectedWindow.eval(sendMessage, function () {})