From fa5b2954a59d32297759361c4de967fdc6dce72c Mon Sep 17 00:00:00 2001
From: Formsmith746 <214276465+Formsmith746@users.noreply.github.com>
Date: Tue, 18 Aug 2026 11:10:19 +0300
Subject: [PATCH] fix: keep imported STL objects after import
Release local/web v1.0.4. Fixes #65
---
README.md | 2 +-
apps/web/src/app/page.tsx | 44 +++++++++++++++------
apps/web/src/lib/projectShapePersistence.ts | 14 +++++++
apps/web/src/lib/skfProject.ts | 2 +-
docs/CHANGELOG.md | 5 +++
package-lock.json | 4 +-
package.json | 2 +-
tests/unit/projectShapePersistence.test.ts | 23 ++++++++++-
8 files changed, 77 insertions(+), 19 deletions(-)
diff --git a/README.md b/README.md
index c31fc0a..9032044 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,7 @@
-
+
diff --git a/apps/web/src/app/page.tsx b/apps/web/src/app/page.tsx
index 8ec5bf8..5d422c0 100644
--- a/apps/web/src/app/page.tsx
+++ b/apps/web/src/app/page.tsx
@@ -16,7 +16,7 @@ import {
type PlacementWorkplane,
} from "@/lib/placementWorkplane";
import { attachProjectAsset, dedupeProjectAssets, projectAssetFromBytes, sourceFormatForFileName } from "@/lib/projectAssets";
-import { hydrateProjectShapeState, type ImportedMeshResource } from "@/lib/projectShapePersistence";
+import { hydrateProjectShapeState, reconcileLoadedProjectShapeCacheEntry, type ImportedMeshResource } from "@/lib/projectShapePersistence";
import { exportSkfProject, importSkfProject, SKF_CREATED_WITH_VERSION } from "@/lib/skfProject";
import { importExtensionSupported } from "@/lib/importExtensions";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
@@ -659,14 +659,27 @@ export default function Home() {
void loadProjectShapes(activeProjectId)
.then((record) => {
if (canceled) return;
- const revision = activeProject.revision ?? record?.revision ?? Date.now();
- const entry = projectShapeCacheEntry(revision, record?.shapes ?? [], record?.history, record?.historyIndex, record?.assets);
- setProjectShapesById((current) => ({
- ...current,
- [activeProjectId]: entry,
- }));
- if (record && !record.skfPackage) {
- void saveProjectShapesWhenIdle(activeProjectId, entry, projectShapeSaveContext(activeProject)).catch(() => {
+ const loadedRevision = record?.revision ?? 0;
+ const revision = Math.max(activeProject.revision ?? 0, loadedRevision, 1);
+ const loadedEntry = projectShapeCacheEntry(Math.max(loadedRevision, 1), record?.shapes ?? [], record?.history, record?.historyIndex, record?.assets);
+ const entry = loadedEntry.revision === revision ? loadedEntry : { ...loadedEntry, revision };
+ setProjectShapesById((current) => {
+ // IndexedDB reads are asynchronous. A local edit/import can update the live
+ // cache while this older read is still in flight; never let that stale read
+ // replace newer in-memory shapes. Bump the preserved entry to the requested
+ // project revision so this effect does not immediately retry the same load.
+ const existing = current[activeProjectId];
+ const reconciled = reconcileLoadedProjectShapeCacheEntry(existing, entry, loadedRevision);
+ if (reconciled === existing) return current;
+ return {
+ ...current,
+ [activeProjectId]: reconciled,
+ };
+ });
+ if (record && !record.skfPackage && loadedRevision > 0) {
+ // Migrate the data at the revision it was actually read from disk. Using the
+ // newer project metadata revision here can let stale shapes outrank a live edit.
+ void saveProjectShapesWhenIdle(activeProjectId, loadedEntry, projectShapeSaveContext(activeProject)).catch(() => {
// The legacy record remains readable and migration can retry on the next load.
});
}
@@ -674,10 +687,15 @@ export default function Home() {
.catch((error) => {
if (!canceled) {
setDashboardNotice(error instanceof Error ? error.message : "Could not load project shapes");
- setProjectShapesById((current) => ({
- ...current,
- [activeProjectId]: projectShapeCacheEntry(activeProject.revision ?? Date.now(), []),
- }));
+ setProjectShapesById((current) => {
+ // A failed background read must not erase a project that has already
+ // received live editor changes while the read was pending.
+ if (current[activeProjectId]) return current;
+ return {
+ ...current,
+ [activeProjectId]: projectShapeCacheEntry(activeProject.revision ?? Date.now(), []),
+ };
+ });
}
});
return () => {
diff --git a/apps/web/src/lib/projectShapePersistence.ts b/apps/web/src/lib/projectShapePersistence.ts
index c1065de..2b53430 100644
--- a/apps/web/src/lib/projectShapePersistence.ts
+++ b/apps/web/src/lib/projectShapePersistence.ts
@@ -3,6 +3,20 @@ import type { WorkplaneShape } from "@/types/sketchforge";
export type ImportedMeshResource = NonNullable;
+export function reconcileLoadedProjectShapeCacheEntry(
+ existing: T | undefined,
+ loaded: T,
+ persistedRevision: number,
+): T {
+ if (!existing || existing.revision <= persistedRevision) {
+ return loaded;
+ }
+ if (existing.revision >= loaded.revision) {
+ return existing;
+ }
+ return { ...existing, revision: loaded.revision };
+}
+
type CompactProjectShapeState = {
shapes: WorkplaneShape[];
history: EditorHistoryEntry[];
diff --git a/apps/web/src/lib/skfProject.ts b/apps/web/src/lib/skfProject.ts
index 9e67a02..4397242 100644
--- a/apps/web/src/lib/skfProject.ts
+++ b/apps/web/src/lib/skfProject.ts
@@ -12,7 +12,7 @@ import type { GridSize, ProjectAsset, ProjectAssetSourceFormat, SketchOperation,
export const SKF_SCHEMA_ID = "com.sketchforge.project";
export const SKF_FORMAT_VERSION = 1;
export const SKF_MINIMUM_READER_VERSION = 1;
-export const SKF_CREATED_WITH_VERSION = "1.0.2";
+export const SKF_CREATED_WITH_VERSION = "1.0.4";
export const SKF_MEDIA_TYPE = "application/vnd.sketchforge.project+zip";
export const SKF_LIMITS = {
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index db35e04..a9ae617 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog
+## 1.0.4
+
+- Fixed imported STL objects briefly appearing and then vanishing when a stale IndexedDB project read completed after the import.
+- Prevented older persisted project data from overwriting newer live editor state during asynchronous project hydration.
+
## 0.1.0
- Initial open-source alpha.
diff --git a/package-lock.json b/package-lock.json
index e24ac9a..5b264ac 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "sketchforge",
- "version": "1.0.3",
+ "version": "1.0.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sketchforge",
- "version": "1.0.3",
+ "version": "1.0.4",
"license": "AGPL-3.0-only",
"dependencies": {
"@tailwindcss/postcss": "^4.1.8",
diff --git a/package.json b/package.json
index e66a10b..70a6725 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "sketchforge",
- "version": "1.0.3",
+ "version": "1.0.4",
"private": true,
"main": "apps/desktop/main.cjs",
"license": "AGPL-3.0-only",
diff --git a/tests/unit/projectShapePersistence.test.ts b/tests/unit/projectShapePersistence.test.ts
index e899d89..0b0b730 100644
--- a/tests/unit/projectShapePersistence.test.ts
+++ b/tests/unit/projectShapePersistence.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { editorHistoryEntry } from "@/lib/editorHistory";
-import { compactProjectShapeState, hydrateProjectShapeState } from "@/lib/projectShapePersistence";
+import { compactProjectShapeState, hydrateProjectShapeState, reconcileLoadedProjectShapeCacheEntry } from "@/lib/projectShapePersistence";
import type { WorkplaneShape } from "@/types/sketchforge";
function importedShape(): WorkplaneShape {
@@ -30,6 +30,27 @@ function importedShape(): WorkplaneShape {
}
describe("project shape persistence resources", () => {
+ it("preserves a newer live cache entry when an older IndexedDB read finishes late", () => {
+ const live = { revision: 20, shapes: [importedShape()] };
+ const staleLoaded = { revision: 30, shapes: [] as WorkplaneShape[] };
+
+ const reconciled = reconcileLoadedProjectShapeCacheEntry(live, staleLoaded, 10);
+
+ expect(reconciled.revision).toBe(30);
+ expect(reconciled.shapes).toHaveLength(1);
+ expect(reconciled.shapes[0]?.id).toBe("mesh-1");
+ });
+
+ it("accepts persisted shapes when the IndexedDB record is newer than the live cache", () => {
+ const live = { revision: 20, shapes: [] as WorkplaneShape[] };
+ const loaded = { revision: 40, shapes: [importedShape()] };
+
+ const reconciled = reconcileLoadedProjectShapeCacheEntry(live, loaded, 40);
+
+ expect(reconciled).toBe(loaded);
+ expect(reconciled.shapes[0]?.id).toBe("mesh-1");
+ });
+
it("stores one immutable mesh resource across current shapes and history", () => {
const mesh = importedShape();
const group: WorkplaneShape = {