autosave fixes

This commit is contained in:
azykov@mail.ru 2026-06-02 20:07:42 +03:00
parent 9145277681
commit 8bc56a433d
No known key found for this signature in database
2 changed files with 33 additions and 27 deletions

View File

@ -59,6 +59,7 @@ export const ObjectView = observer(function ({ object }: ObjectViewProps) {
} }
<group <group
ref={groupRef} ref={groupRef}
name={`${object.id} (${object.typeId} instance)`}
position={object.position} position={object.position}
rotation={object.rotation} rotation={object.rotation}
scale={object.scale} scale={object.scale}
@ -70,7 +71,11 @@ export const ObjectView = observer(function ({ object }: ObjectViewProps) {
const color = (v.color ?? vt?.color) ?? 'white'; const color = (v.color ?? vt?.color) ?? 'white';
const opacity = (v.opacity ?? vt?.opacity) ?? 1; const opacity = (v.opacity ?? vt?.opacity) ?? 1;
return ( return (
<mesh key={idx} position={v.position}> <mesh
key={idx}
name={`voxel ${idx} [${v.position}]`}
position={v.position}
>
<boxGeometry args={[1, 1, 1]} /> <boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial <meshStandardMaterial
color={color} color={color}

View File

@ -1,5 +1,4 @@
import { makeAutoObservable, reaction, toJS } from "mobx"; import { makeAutoObservable, reaction, toJS } from "mobx";
import { deepObserve } from "mobx-utils";
import { WorldFactory } from "../model/worldFactory"; import { WorldFactory } from "../model/worldFactory";
import type { ObjectType, World } from "../types"; import type { ObjectType, World } from "../types";
import type { VoxelType } from "../types/voxel"; import type { VoxelType } from "../types/voxel";
@ -11,29 +10,29 @@ import { clone } from "../utils";
export class WorldState { export class WorldState {
public data: World = WorldFactory.create(); public data: World = WorldFactory.create();
private saveTimer: ReturnType<typeof setTimeout> | 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() { constructor() {
makeAutoObservable(this); makeAutoObservable(this);
this.setupAutoSave(); this._stopAutoSave = this.startAutoSave();
}
private setupAutoSave() {
let disposeDeep: (() => void) | undefined;
reaction(
() => this.data,
(data) => {
disposeDeep?.();
disposeDeep = deepObserve(data, () => this.save());
},
{ fireImmediately: true },
);
} }
public reset() { public reset() {
console.log('Resetting world...'); console.log('Resetting world...');
this.data = WorldFactory.create(); this.withoutAutoSave(() => {
this.data = WorldFactory.create();
});
state.worldEditor.resetSelectedObject(); state.worldEditor.resetSelectedObject();
} }
@ -83,20 +82,22 @@ export class WorldState {
public load() { public load() {
console.log('Loading world...'); console.log('Loading world...');
this.data = WorldFactory.load() ?? WorldFactory.create(); this.withoutAutoSave(() => {
this.data = WorldFactory.load() ?? WorldFactory.create();
});
state.worldEditor.resetSelectedObject(); state.worldEditor.resetSelectedObject();
} }
public save(): void { private saveData(data: World): void {
console.log('Saving world...'); console.log('Saving world...');
console.log((new Error('').stack!)); const stack = new Error('Saving world...').stack!.split('\n').slice(1);
const { objectTypes, voxelTypes, ...debug } = toJS(this.data); const { objectTypes, voxelTypes, ...debug } = toJS(data);
console.log(JSON.stringify(debug, undefined, 4)); console.dir({ stack, debug });
// debounce WorldFactory.save(toJS(this.data));
clearTimeout(this.saveTimer); }
this.saveTimer = setTimeout(() => {
WorldFactory.save(toJS(this.data)); public save(): void {
}, 500); this.saveData(this.data);
} }
public getObjectTypeById(id: string): ObjectType | undefined { public getObjectTypeById(id: string): ObjectType | undefined {