public
Created

Recovery from bug-723012

  • Download Gist
prefs-example.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
// Example how to access preferences that were stored between versions of 1.4 - 1.4.2
 
var prefs = require('addon-kit/simple-prefs').prefs;
var recovery = require('./recovery');
 
// If preferences have not being recovered yet and preference was saved in 1.4 ~ 1.4.2, then
// read it from recovery prefs and override current preference value. If nothing saved in
// 1.4 ~ 1.4.2 then take whatever was saved before (will automatically be current preference).
var myPref = (!prefs.recovered && typeof(recovery.prefs.foo) !== 'undefined') ? prefs.foo = recovery.prefs.foo : prefs.foo;
// Same thing for all prefs....
// once recovery is done save that to prefs to avoid overwrites from old prefs each time.
prefs.recovered = true;
recovery.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
'use strict';
 
const { Loader } = require('@loader');
const options = require('@packaging');
 
let recovery = {};
// Deep copy all the options.
recovery.options = JSON.parse(JSON.stringify(options));
// Make `jetpackID` as it was in 1.4 - 1.4.2
recovery.options.jetpackID = options.jetpackID.split('@').shift();
 
// Create a new loader with a recovery options.
recovery.loader = Loader.new(recovery.options);
recovery.require = Loader.require.bind(recovery.loader, module.path);
 
// Exports storage and pref as they would have been in 1.4 - 1.4.2
exports.storage = recovery.require('addon-kit/simple-storage').storage;
exports.prefs = recovery.require('addon-kit/simple-prefs').prefs;
exports.passwords = recovery.require('addon-kit/passwords');
storage-example.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// Example how to access data stored between versions of 1.4 ~ 1.4.2
 
// storage contains any data saved into it before updating to 1.4.
var storage = require('addon-kit/simple-storage').storage;
var prefs = require('addon-kit/simple-prefs').prefs;
// all the data saved into storage between 1.4 ~ 1.4.2 will not appear in storage,
// but you could access it from `recovery.storage`.
var recovery = require('./recovery');
 
// lets perform recursive merge. We know that data in recovery.storage is newer
// than in actual `storage` so we could give a priority to the newer data.
//
// ## Note this merging algorithm is not perfect and you'll be better off writing
// your own that would suite your particular storage model better ##
 
function recover(current, recent) {
for (var key in recent) {
// if this entry was not present before just copy
if (!(key in current))
current[key] = recent[key];
 
// if data types are different most likely recent version is more valid.
else if (typeof(current[key]) !== typeof(recent[key]))
current[key] = recent[key];
 
// If it's not an object we should be probably override.
else if (typeof(recent[key]) !== 'object')
current[key] = recent[key];
 
// if object then merge those as well
else
recover(current[key], recent[key]);
}
}
 
if (!prefs.storageRecovered) {
recover(storage, recovery.storage);
prefs.storageRecovered = true;
}

I think the preferences example is incorrect:

var myPref = !prefs.recovered && recovery.prefs.foo ? prefs.foo = recovery.prefs.foo : prefs.foo;

If type of a preference is boolean and its value is "false", the "else" part will work that is incorrect.

The code should be something like this:

var myPref = !prefs.recovered && (typeof recovery.prefs.foo != 'undefined') ? prefs.foo = recovery.prefs.foo : prefs.foo;

You're right @CIAvash thanks for pointing that out, I'll update a module to fix this.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.