Merge pull request #261 from OfficialNoob/patch-1

Made decodeStoredItem detect item type
This commit is contained in:
Ajay Ramachandran 2020-03-10 01:21:10 -04:00 committed by GitHub
commit 77eff12d9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 17 deletions

View file

@ -1,5 +1,8 @@
import * as Types from "./types"; import * as Types from "./types";
import Config from "./config"; import Config from "./config";
// Make the config public for debugging purposes
(<any> window).SB = Config;
import Utils from "./utils"; import Utils from "./utils";
var utils = new Utils({ var utils = new Utils({

View file

@ -84,13 +84,8 @@ class SBMap<T, U> extends Map {
return result; return result;
} }
toJSON() {
return Array.from(this.entries());
}
} }
var Config: SBObject = { var Config: SBObject = {
/** /**
* Callback function when an option is updated * Callback function when an option is updated
@ -131,14 +126,14 @@ var Config: SBObject = {
/** /**
* A SBMap cannot be stored in the chrome storage. * A SBMap cannot be stored in the chrome storage.
* This data will be encoded into an array instead as specified by the toJSON function. * This data will be encoded into an array instead
* *
* @param data * @param data
*/ */
function encodeStoredItem(data) { function encodeStoredItem(data) {
// if data is SBMap convert to json for storing // if data is SBMap convert to json for storing
if(!(data instanceof SBMap)) return data; if(!(data instanceof SBMap)) return data;
return JSON.stringify(data); return Array.from(data.entries());
} }
/** /**
@ -148,18 +143,30 @@ function encodeStoredItem(data) {
* @param {*} data * @param {*} data
*/ */
function decodeStoredItem(id: string, data) { function decodeStoredItem(id: string, data) {
if(typeof data !== "string") return data; if (!Config.defaults[id]) return data;
if (Config.defaults[id] instanceof SBMap) {
try { try {
let str = JSON.parse(data); let jsonData: any = data;
if(!Array.isArray(str)) return data; // Check if data is stored in the old format for SBMap (a JSON string)
return new SBMap(id, str); if (typeof data === "string") {
try {
jsonData = JSON.parse(data);
} catch(e) { } catch(e) {
// Continue normally (out of this if statement)
}
}
if (!Array.isArray(jsonData)) return data;
return new SBMap(id, jsonData);
} catch(e) {
console.error("Failed to parse SBMap: " + id);
}
}
// If all else fails, return the data // If all else fails, return the data
return data; return data;
}
} }
function configProxy(): any { function configProxy(): any {

View file

@ -1,4 +1,6 @@
import Config from "./config"; import Config from "./config";
// Make the config public for debugging purposes
(<any> window).SB = Config;
import Utils from "./utils"; import Utils from "./utils";
var utils = new Utils(); var utils = new Utils();