|
@@ -189,7 +189,7 @@ class StateDB implements IStateDB {
|
|
|
*
|
|
|
* @param namespace - The namespace to retrieve.
|
|
|
*
|
|
|
- * @returns A promise that bears a collection data payloads for a namespace.
|
|
|
+ * @returns A promise that bears a collection of payloads for a namespace.
|
|
|
*
|
|
|
* #### Notes
|
|
|
* Namespaces are entirely conventional entities. The `id` values of stored
|
|
@@ -202,32 +202,10 @@ class StateDB implements IStateDB {
|
|
|
*/
|
|
|
fetchNamespace(namespace: string): Promise<IStateItem[]> {
|
|
|
return this._ready.then(() => {
|
|
|
- const { localStorage } = window;
|
|
|
const prefix = `${this._window}:${this.namespace}:${namespace}:`;
|
|
|
- let items: IStateItem[] = [];
|
|
|
- let i = localStorage.length;
|
|
|
-
|
|
|
- while (i) {
|
|
|
- let key = localStorage.key(--i);
|
|
|
-
|
|
|
- if (key && key.indexOf(prefix) === 0) {
|
|
|
- let value = localStorage.getItem(key);
|
|
|
-
|
|
|
- try {
|
|
|
- let envelope = JSON.parse(value) as Private.Envelope;
|
|
|
-
|
|
|
- items.push({
|
|
|
- id: key.replace(`${this._window}:${this.namespace}:`, ''),
|
|
|
- value: envelope ? envelope.v : undefined
|
|
|
- });
|
|
|
- } catch (error) {
|
|
|
- console.warn(error);
|
|
|
- localStorage.removeItem(key);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ const replace = `${this._window}:${this.namespace}:`;
|
|
|
|
|
|
- return items;
|
|
|
+ return StateDB.fetchNamespace(prefix, key => key.replace(replace, ''));
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -275,31 +253,13 @@ class StateDB implements IStateDB {
|
|
|
*/
|
|
|
toJSON(): Promise<ReadonlyJSONObject> {
|
|
|
return this._ready.then(() => {
|
|
|
- const { localStorage } = window;
|
|
|
const prefix = `${this._window}:${this.namespace}:`;
|
|
|
- const contents: Partial<ReadonlyJSONObject> = { };
|
|
|
- let i = localStorage.length;
|
|
|
-
|
|
|
- while (i) {
|
|
|
- let key = localStorage.key(--i);
|
|
|
-
|
|
|
- if (key && key.indexOf(prefix) === 0) {
|
|
|
- let value = localStorage.getItem(key);
|
|
|
-
|
|
|
- try {
|
|
|
- let envelope = JSON.parse(value) as Private.Envelope;
|
|
|
-
|
|
|
- if (envelope) {
|
|
|
- contents[key.replace(prefix, '')] = envelope.v;
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- console.warn(error);
|
|
|
- localStorage.removeItem(key);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- return contents;
|
|
|
+ return StateDB.fetchNamespace(prefix, key => key.replace(prefix, ''))
|
|
|
+ .reduce((acc, val) => {
|
|
|
+ acc[val.id] = val.value;
|
|
|
+ return acc;
|
|
|
+ }, { } as Partial<ReadonlyJSONObject>);
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -462,6 +422,49 @@ namespace StateDB {
|
|
|
*/
|
|
|
windowName?: string;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve all the saved bundles for a given namespace in local storage.
|
|
|
+ *
|
|
|
+ * @param prefix - The namespace to retrieve.
|
|
|
+ *
|
|
|
+ * @param mask - Optional mask function to transform each key retrieved.
|
|
|
+ *
|
|
|
+ * @returns A collection of data payloads for a given prefix.
|
|
|
+ *
|
|
|
+ * #### Notes
|
|
|
+ * If there are any errors in retrieving the data, they will be logged to the
|
|
|
+ * console in order to optimistically return any extant data without failing.
|
|
|
+ */
|
|
|
+ export
|
|
|
+ function fetchNamespace(namespace: string, mask: (key: string) => string = key => key): IStateItem[] {
|
|
|
+ const { localStorage } = window;
|
|
|
+
|
|
|
+ let items: IStateItem[] = [];
|
|
|
+ let i = localStorage.length;
|
|
|
+
|
|
|
+ while (i) {
|
|
|
+ let key = localStorage.key(--i);
|
|
|
+
|
|
|
+ if (key && key.indexOf(namespace) === 0) {
|
|
|
+ let value = localStorage.getItem(key);
|
|
|
+
|
|
|
+ try {
|
|
|
+ let envelope = JSON.parse(value) as Private.Envelope;
|
|
|
+
|
|
|
+ items.push({
|
|
|
+ id: mask(key),
|
|
|
+ value: envelope ? envelope.v : undefined
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ console.warn(error);
|
|
|
+ localStorage.removeItem(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return items;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|