Skip to content

Free open-source image editor for the browser

Release unused history resources

Rodrigo5f5f2b513e48
diff --git a/src/app/document/index.ts b/src/app/document/index.ts
index e8fd5f4..a624799 100644
--- a/src/app/document/index.ts
+++ b/src/app/document/index.ts
@@ -17,7 +17,14 @@ function equal(a: Scene, b: Scene) {
/** One independent editing session. No React, decoders, or file workflows. */
export function createDocument(initial: Scene, resources = createResources()) {
const scene = createStore(() => initial);
- const { update, ...history } = createHistory(scene, equal);
+ const { update, ...history } = createHistory(
+ scene,
+ equal,
+ 100,
+ (retained) => {
+ resources.retain(new Set(retained.map((state) => state.source)));
+ },
+ );
let closed = false;
return {
id: crypto.randomUUID(),
diff --git a/src/app/document/resources.ts b/src/app/document/resources.ts
index 46083a9..7a8c971 100644
--- a/src/app/document/resources.ts
+++ b/src/app/document/resources.ts
@@ -20,6 +20,14 @@ export function createResources() {
}
return resource;
},
+ retain(ids: ReadonlySet<string>) {
+ for (const [id, { image }] of images) {
+ if (!ids.has(id)) {
+ image.color.dispose();
+ images.delete(id);
+ }
+ }
+ },
dispose() {
disposed = true;
for (const { image } of images.values()) {
diff --git a/src/lib/history.ts b/src/lib/history.ts
index d67a3f5..dbcdecf 100644
--- a/src/lib/history.ts
+++ b/src/lib/history.ts
@@ -5,13 +5,21 @@ export function createHistory<T extends object>(
state: StoreApi<T>,
equal: (a: T, b: T) => boolean,
limit = 100,
+ onChange?: (retained: readonly T[]) => void,
) {
const status = createStore(() => ({ undoCount: 0, redoCount: 0 }));
const past: T[] = [];
const future: T[] = [];
let group: T | undefined;
- const publish = () =>
+ function publish() {
status.setState({ undoCount: past.length, redoCount: future.length });
+ onChange?.([
+ ...past,
+ ...future,
+ ...(group ? [group] : []),
+ state.getState(),
+ ]);
+ }
function record(before: T) {
if (equal(before, state.getState())) {
return;
@@ -21,13 +29,13 @@ export function createHistory<T extends object>(
past.shift();
}
future.length = 0;
- publish();
}
function commit() {
const before = group;
group = undefined;
if (before !== undefined) {
record(before);
+ publish();
}
}
function cancel() {
@@ -35,6 +43,7 @@ export function createHistory<T extends object>(
group = undefined;
if (before !== undefined) {
state.setState(before, true);
+ publish();
}
}
function travel(from: T[], to: T[]) {
@@ -62,6 +71,7 @@ export function createHistory<T extends object>(
if (!group) {
record(before);
}
+ publish();
},
begin() {
group ??= state.getState();
diff --git a/tests/documents.test.ts b/tests/documents.test.ts
index 33df7e3..657360d 100644
--- a/tests/documents.test.ts
+++ b/tests/documents.test.ts
@@ -1,6 +1,8 @@
import { expect, test } from "@playwright/test";
+import { init, target } from "vgpu/mock";
import { createDocument } from "@/app/document";
import { setAdjustments, setToneCurve } from "@/app/document/edits";
+import { createResources } from "@/app/document/resources";
import { defaultAdjustments } from "@/app/scene";
import { createWorkspace } from "@/app/workspace";
import { defaultCurve } from "@/features/tone-curves/curve";
@@ -95,4 +97,42 @@ test("documents edit independently without React, retain bounded history, and re
expect(() => setAdjustments(first, { exposure: 2 })).toThrow("closed");
workspace.dispose();
expect(() => setAdjustments(second, { exposure: 2 })).toThrow("closed");
+ await test.step("resources survive undo and are released after cancellation, branching, and history eviction", async () => {
+ const gpu = await init();
+ const resources = createResources();
+ const file = new File(["fixture"], "image.png");
+ const add = () => resources.add(file, target(gpu, { size: [2, 2] }));
+ const source = add();
+ const doc = createDocument(
+ { ...document().scene.getState(), source },
+ resources,
+ );
+ const replace = (source: string) =>
+ doc.edit({ ...doc.scene.getState(), source });
+ const canceled = add();
+ doc.history.begin();
+ replace(canceled);
+ expect(resources.get(source)).toBeDefined();
+ doc.history.cancel();
+ expect(() => resources.get(canceled)).toThrow("unavailable");
+ const branch = add();
+ replace(branch);
+ doc.history.undo();
+ expect(resources.get(branch)).toBeDefined();
+ doc.history.redo();
+ expect(doc.scene.getState().source).toBe(branch);
+ doc.history.undo();
+ setAdjustments(doc, { exposure: 1 });
+ expect(() => resources.get(branch)).toThrow("unavailable");
+ const replacement = add();
+ replace(replacement);
+ for (let i = 0; i < 100; i++) {
+ setAdjustments(doc, { exposure: i % 2 });
+ }
+ expect(() => resources.get(source)).toThrow("unavailable");
+ expect(resources.get(replacement)).toBeDefined();
+ doc.dispose();
+ expect(() => resources.get(replacement)).toThrow("unavailable");
+ gpu.dispose();
+ });
});