snapshots.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { OrderedMap } from 'immutable';
  7. import { UUID } from 'mol-util';
  8. import { RxEventHelper } from 'mol-util/rx-event-helper';
  9. import { PluginState } from '../state';
  10. export { PluginStateSnapshotManager }
  11. class PluginStateSnapshotManager {
  12. private ev = RxEventHelper.create();
  13. private _entries = OrderedMap<string, PluginStateSnapshotManager.Entry>().asMutable();
  14. readonly events = {
  15. changed: this.ev()
  16. };
  17. get entries() { return this._entries; }
  18. getEntry(id: string) {
  19. return this._entries.get(id);
  20. }
  21. remove(id: string) {
  22. if (!this._entries.has(id)) return;
  23. this._entries.delete(id);
  24. this.events.changed.next();
  25. }
  26. add(e: PluginStateSnapshotManager.Entry) {
  27. this._entries.set(e.id, e);
  28. this.events.changed.next();
  29. }
  30. clear() {
  31. if (this._entries.size === 0) return;
  32. this._entries = OrderedMap<string, PluginStateSnapshotManager.Entry>().asMutable();
  33. this.events.changed.next();
  34. }
  35. dispose() {
  36. this.ev.dispose();
  37. }
  38. }
  39. namespace PluginStateSnapshotManager {
  40. export interface Entry {
  41. id: UUID,
  42. timestamp: string,
  43. name?: string,
  44. description?: string,
  45. snapshot: PluginState.Snapshot
  46. }
  47. export function Entry(snapshot: PluginState.Snapshot, name?: string, description?: string): Entry {
  48. return { id: UUID.create22(), timestamp: new Date().toLocaleString(), name, snapshot, description };
  49. }
  50. export interface StateSnapshot {
  51. entries: Entry[]
  52. }
  53. }