-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tab to serialize to storage (#1363)
* Create private properties for site * Add other private getters and clean up normalize method * Store tab state * Move opt-ins from tab to tab-state. create new class * Move to using a new config class for storage * Restore from serialized, add config class and pass to utils. * Fix up tab state restoration * Clean up legacy serialization, add unit test for session restoring. Change the restore methods to take an id. Restore the tab properly on startup. * Ensure storage writes happen in order * Use a promise stack and singleton instead * Backup after restore to revent accidental overwriting * Restore tracker objects
- Loading branch information
1 parent
8be6a8d
commit 0fe1453
Showing
16 changed files
with
814 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* This is used by the dashboard to get the tab data. | ||
*/ | ||
export class LegacyTabTransfer { | ||
/** | ||
* @param {import('./tab.es6')} tab | ||
*/ | ||
constructor (tab) { | ||
const clonedTab = cloneClassObject(tab) | ||
const entries = Object.entries(clonedTab) | ||
for (const [key] of entries) { | ||
this[key] = clonedTab[key] | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {*} value | ||
* @returns {boolean} | ||
*/ | ||
function isPrimitive (value) { | ||
return Object(value) !== value | ||
} | ||
|
||
/** | ||
* @param {*} value | ||
* @returns {boolean} | ||
*/ | ||
function isStructuredCloneable (value) { | ||
return isPrimitive(value) || Array.isArray(value) | ||
} | ||
|
||
function cloneClassObject (object) { | ||
if (isStructuredCloneable(object)) { | ||
return structuredClone(object) | ||
} | ||
const out = {} | ||
for (const key of Object.keys(object)) { | ||
const value = object[key] | ||
// Ignore 'private' keys | ||
if (key.startsWith('_')) { | ||
continue | ||
} | ||
if (isStructuredCloneable(value)) { | ||
out[key] = structuredClone(value) | ||
} else { | ||
out[key] = cloneClassObject(value) | ||
} | ||
} | ||
if (hasModifiedPrototype(object)) { | ||
const objectDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(object)) | ||
// Clone getter values | ||
for (const [key, value] of Object.entries(objectDescriptors)) { | ||
if (typeof value.get === 'function') { | ||
out[key] = cloneClassObject(object[key]) | ||
} | ||
} | ||
} | ||
return out | ||
} | ||
|
||
function hasModifiedPrototype (object) { | ||
return Object.getPrototypeOf(object) !== Object.getPrototypeOf({}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.