| 17 | } |
| 18 | |
| 19 | export class AutosaveWatcher implements NodeObserver, ChildSetObserver, ProjectObserver { |
| 20 | @observable |
| 21 | needsSave: boolean |
| 22 | |
| 23 | @observable |
| 24 | lastVersion: string |
| 25 | |
| 26 | @observable |
| 27 | failedSave: boolean |
| 28 | failedSaveInfo: AutosaveWatcherFailedSaveInfo |
| 29 | |
| 30 | @observable |
| 31 | needsRefresh: boolean |
| 32 | |
| 33 | failedMessage: string |
| 34 | |
| 35 | project: Project |
| 36 | projectLoader: ProjectLoader |
| 37 | |
| 38 | timeoutID?: number |
| 39 | |
| 40 | constructor(project: Project, projectLoader: ProjectLoader) { |
| 41 | this.project = project |
| 42 | this.lastVersion = this.project.version |
| 43 | this.projectLoader = projectLoader |
| 44 | this.needsSave = false |
| 45 | this.failedSave = false |
| 46 | } |
| 47 | |
| 48 | handleChildSetMutation(mutations: ChildSetMutation): void { |
| 49 | this.trigger() |
| 50 | } |
| 51 | |
| 52 | handleProjectMutation(mutation: ProjectMutation): void { |
| 53 | this.trigger() |
| 54 | } |
| 55 | |
| 56 | handleNodeMutation(nodeMutation: NodeMutation): void { |
| 57 | this.trigger() |
| 58 | } |
| 59 | |
| 60 | @action |
| 61 | trigger() { |
| 62 | this.needsSave = true |
| 63 | |
| 64 | if (this.timeoutID == null && !this.project?.isReadOnly) { |
| 65 | this.timeoutID = window.setTimeout( |
| 66 | () => |
| 67 | runInAction(() => { |
| 68 | this.needsSave = false |
| 69 | this.timeoutID = null |
| 70 | |
| 71 | this.projectLoader |
| 72 | .saveProject(this.project) |
| 73 | .then((success) => { |
| 74 | if (success) { |
| 75 | this.needsSave = false |
| 76 | this.failedSave = false |
nothing calls this directly
no outgoing calls
no test coverage detected