UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

141 lines (139 loc) 4.55 kB
import { of } from 'rxjs'; import { map, tap } from 'rxjs/operators'; import { PersistentWorkflowStore } from './persistent-workflow-store'; /** * Implementation of PersistentWorkflowStore by local storage. */ export class PersistentLocalStorage extends PersistentWorkflowStore { nextInstanceId = 1; /** * Save the work item snapshot to the local storage. * * @param version the version number. * @param workItemId the id of work item. * @param workItemState the state of work item. * @param workItemData the persistent data of work item. * @param instanceId the instance ID if it has already. */ save(version, workItemId, workItemState, workItemData, instanceId) { return of(null) .pipe(map(() => { const indexData = this.readIndexData(); if (MsftSme.isNullOrUndefined(instanceId)) { instanceId = this.nextInstanceId++; indexData.instances.push(instanceId); this.writeIndexData(indexData); } const snapshot = { version, instanceId, workItemId, workItemState, workItemData }; this.writeInstance(snapshot); return snapshot; })); } /** * Restore the snapshot data for the workflow. */ restore() { return of(null) .pipe(map(() => { const instances = []; const indexData = this.readIndexData(); if (indexData.instances.length === 0) { return instances; } for (const instanceId of indexData.instances) { const instance = this.readInstance(instanceId); if (instance) { instances.push(instance); if (this.nextInstanceId < instanceId) { this.nextInstanceId = instanceId; } } else { this.clear(instanceId); } } return instances; })); } /** * Clear the snapshot for the instance id. * If not specified, clear all snapshots. * * @param instanceId the instance id. */ clear(instanceId) { return of(null) .pipe(tap(() => { const indexData = this.readIndexData(); // remove one. if (!MsftSme.isNullOrUndefined(instanceId)) { const pointer = indexData.instances.indexOf(instanceId); if (pointer >= 0) { indexData.instances.splice(pointer, 1); this.writeIndexData(indexData); } MsftSme.LocalStorageHandler.removeItem(this.instanceKey(instanceId)); return; } // remove all. MsftSme.LocalStorageHandler.removeItem(this.indexKey()); for (const index2 of indexData.instances) { MsftSme.LocalStorageHandler.removeItem(this.instanceKey(index2)); } return; })); } /** * Gets the key of index data. */ indexKey() { return `${this.moduleName}@${this.name}:index`; } /** * Gets the key of snapshot instance. * * @param instanceId the instance Id. */ instanceKey(instanceId) { return `${this.moduleName}@${this.name}:${instanceId}`; } /** * Reads the index data to enumerate all snapshots. */ readIndexData() { let indexData = null; const item = MsftSme.LocalStorageHandler.getItem(this.indexKey()); if (item) { indexData = JSON.parse(item); } else { indexData = { instances: [] }; } return indexData; } /** * Writes the index data. * * @param indexData the index data. */ writeIndexData(indexData) { MsftSme.LocalStorageHandler.setItem(this.indexKey(), JSON.stringify(indexData)); } /** * Reads the snapshot of the instance. * * @param instanceId the instance ID. */ readInstance(instanceId) { return JSON.parse(MsftSme.LocalStorageHandler.getItem(this.instanceKey(instanceId)) ?? '{}'); } /** * Writes the snapshot. * * @param snapshot the snapshot. */ writeInstance(snapshot) { MsftSme.LocalStorageHandler.setItem(this.instanceKey(snapshot.instanceId), JSON.stringify(snapshot)); } } //# sourceMappingURL=persistent-local-storage.js.map