From 8bc56a433db6915727247c3b7a44c4968eee1e04 Mon Sep 17 00:00:00 2001 From: "azykov@mail.ru" Date: Tue, 2 Jun 2026 20:07:42 +0300 Subject: [PATCH] autosave fixes --- src/components/ObjectView.tsx | 7 ++++- src/state/worldState.ts | 53 ++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/components/ObjectView.tsx b/src/components/ObjectView.tsx index 0fdd318..14d2ce3 100644 --- a/src/components/ObjectView.tsx +++ b/src/components/ObjectView.tsx @@ -59,6 +59,7 @@ export const ObjectView = observer(function ({ object }: ObjectViewProps) { } + | undefined; + private startAutoSave() { + return reaction(() => toJS(this.data), (data) => this.saveData(data), { delay: 500 }); + } + + private withoutAutoSave(fn: () => void) { + this._stopAutoSave(); + fn(); + this._stopAutoSave = this.startAutoSave(); + } + + private _stopAutoSave: () => void = () => { }; constructor() { makeAutoObservable(this); - this.setupAutoSave(); - } - - private setupAutoSave() { - let disposeDeep: (() => void) | undefined; - reaction( - () => this.data, - (data) => { - disposeDeep?.(); - disposeDeep = deepObserve(data, () => this.save()); - }, - { fireImmediately: true }, - ); + this._stopAutoSave = this.startAutoSave(); } public reset() { console.log('Resetting world...'); - this.data = WorldFactory.create(); + this.withoutAutoSave(() => { + this.data = WorldFactory.create(); + }); state.worldEditor.resetSelectedObject(); } @@ -83,20 +82,22 @@ export class WorldState { public load() { console.log('Loading world...'); - this.data = WorldFactory.load() ?? WorldFactory.create(); + this.withoutAutoSave(() => { + this.data = WorldFactory.load() ?? WorldFactory.create(); + }); state.worldEditor.resetSelectedObject(); } - public save(): void { + private saveData(data: World): void { console.log('Saving world...'); - console.log((new Error('').stack!)); - const { objectTypes, voxelTypes, ...debug } = toJS(this.data); - console.log(JSON.stringify(debug, undefined, 4)); - // debounce - clearTimeout(this.saveTimer); - this.saveTimer = setTimeout(() => { - WorldFactory.save(toJS(this.data)); - }, 500); + const stack = new Error('Saving world...').stack!.split('\n').slice(1); + const { objectTypes, voxelTypes, ...debug } = toJS(data); + console.dir({ stack, debug }); + WorldFactory.save(toJS(this.data)); + } + + public save(): void { + this.saveData(this.data); } public getObjectTypeById(id: string): ObjectType | undefined {