mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 16:13:24 +02:00
Trying to implement memory based settings storage.
This commit is contained in:
@@ -1,3 +1,57 @@
|
||||
module.exports = function SettingsServiceFactory($localForage) {
|
||||
var loadedInMemory = false
|
||||
|
||||
$localForage.memoryData = {}
|
||||
|
||||
function setItemMemory(key, value) {
|
||||
$localForage.memoryData[key] = value
|
||||
}
|
||||
|
||||
function getItemMemory(key) {
|
||||
var ret
|
||||
if (typeof $localForage.memoryData[key] !== 'undefined') {
|
||||
ret = $localForage.memoryData[key]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
$localForage.setItemSync = function (key, value) {
|
||||
setItemMemory(key, value)
|
||||
|
||||
$localForage.setItem(key, value, function () {
|
||||
console.log('Finished saving to local forage', key, value)
|
||||
})
|
||||
}
|
||||
|
||||
$localForage.getAllItemsSync = function () {
|
||||
$localForage.getKeys().then(function (keys) {
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
$localForage.getItem(keys[i]).then(setItemMemory.bind(null, keys[i]))
|
||||
}
|
||||
})
|
||||
// FIX: This is not sync
|
||||
loadedInMemory = true
|
||||
}
|
||||
|
||||
$localForage.getItemSync = function (key) {
|
||||
if (!loadedInMemory) {
|
||||
console.log('Loading for the first time')
|
||||
$localForage.getAllItemsSync(function () {
|
||||
console.log(getItemMemory(key))
|
||||
})
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
console.log(getItemMemory(key))
|
||||
}, 1000)
|
||||
|
||||
return getItemMemory(key)
|
||||
}
|
||||
|
||||
$localForage.bindSync = function (scope, opts) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
return $localForage
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user