fix: apply editor chunk save acks so dirty drafts clear
Reconnect draft-keep treated the EDITOR_CHUNK save reply as a reload and skipped applying it while the local draft was still dirty. The server had already persisted (including Void), but the Save button stayed dirty. Track in-flight save keys (dimension-scoped) and apply those acks so the editor baseline refreshes after a successful write. Co-authored-by: tim <tim@timfalken.com>
@@ -48,6 +48,7 @@ import
48
48
isOverworld
49
49
} from "/shared/world/dimensions.js";
50
50
import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";
51
import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "./saveAck.js";
51
52
52
53
const game = new Game();
53
54
let socket;
@@ -70,6 +71,8 @@ let neighborSpawnersByChunk = new Map();
70
71
let pendingNavigate = null;
71
72
/** When true, next matching editorChunk after save should navigate to pendingNavigate. */
72
73
let navigateAfterSave = false;
74
/** Keys (`dimensionId:chunkX,chunkY`) for in-flight `EDITOR_SAVE_CHUNK` acks. */
75
const pendingSaveAcks = new Set();
73
76
let chunkX = 0;
74
77
let chunkY = 0;
75
78
let selectedDimensionId = OVERWORLD_ID;
@@ -819,6 +822,7 @@ function saveSelectedChunk ()
819
822
const neighborSaves = [...dirtyNeighborChunks.entries()];
820
823
clearNeighborEditState();
821
824
const savedChunks = [{ chunkX, chunkY }];
825
pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, chunkX, chunkY));
822
826
socket.send({
823
827
type: MessageType.editorSaveChunk,
824
828
chunkX,
@@ -835,6 +839,7 @@ function saveSelectedChunk ()
835
839
{
836
840
const [cx, cy] = key.split(",").map(Number);
837
841
savedChunks.push({ chunkX: cx, chunkY: cy });
842
pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, cx, cy));
838
843
socket.send({
839
844
type: MessageType.editorSaveChunk,
840
845
chunkX: cx,
@@ -1107,12 +1112,15 @@ function setRevisionState ({ activeRevisionId: active, pendingRevisionId: pendin
1107
1112
updateRevisionUI();
1108
1113
}
1109
1114
1110
function showSelectedChunk (payload)
1115
function showSelectedChunk (payload, { isSaveAck = false } = {})
1111
1116
{
1112
const keepDirtyDraft = !!draft
1113
&& payload.chunkX === chunkX
1114
&& payload.chunkY === chunkY
1115
&& isSelectedChunkDirty();
1117
const keepDirtyDraft = !shouldApplySelectedEditorChunk({
1118
isSaveAck,
1119
isDirty: !!draft
1120
&& payload.chunkX === chunkX
1121
&& payload.chunkY === chunkY
1122
&& isSelectedChunkDirty()
1123
});
1116
1124
if (!keepDirtyDraft)
1117
1125
{ clearNeighborEditState(); }
1118
1126
if (keepDirtyDraft)
@@ -1164,7 +1172,7 @@ function showSelectedChunk (payload)
1164
1172
}
1165
1173
view.applySnapshot(snapshotFromPayload(payload));
1166
1174
syncSpawnersOverlay(payload.chunkX, payload.chunkY, draft.spawners);
1167
const restored = tryRestoreDraftBackupAfterServerLoad(payload);
1175
const restored = isSaveAck ? false : tryRestoreDraftBackupAfterServerLoad(payload);
1168
1176
if (!restored)
1169
1177
{ captureDraftBaseline(); }
1170
1178
else
@@ -1763,15 +1771,18 @@ async function main ()
1763
1771
{ renderGlobalChangelog(message.changelog ?? []); }
1764
1772
else if (message.type === MessageType.editorChunk)
1765
1773
{
1766
const msgDim = normalizeDimensionId(message.dimensionId ?? OVERWORLD_ID);
1774
const msgDim = normalizeDimensionId(message.dimensionId ?? selectedDimensionId);
1767
1775
if (msgDim !== selectedDimensionId)
1768
1776
{ return; }
1777
const isSaveAck = pendingSaveAcks.delete(
1778
editorChunkSaveKey(msgDim, message.chunkX, message.chunkY)
1779
);
1769
1780
if (message.settings)
1770
1781
{ view.setSettings(message.settings); }
1771
1782
if (!inEditorView(message.chunkX, message.chunkY))
1772
1783
{ return; }
1773
1784
if (message.chunkX === chunkX && message.chunkY === chunkY)
1774
{ showSelectedChunk(message); }
1785
{ showSelectedChunk(message, { isSaveAck }); }
1775
1786
else
1776
1787
{
1777
1788
const key = chunkKey(message.chunkX, message.chunkY);
@@ -1808,6 +1819,7 @@ async function main ()
1808
1819
if (giftsModal?.isOpen())
1809
1820
{ giftsModal.handleError?.(message.message); }
1810
1821
pendingCreatedDimensionId = null;
1822
pendingSaveAcks.clear();
1811
1823
navigateAfterSave = false;
1812
1824
dimensionPicker?.setValue(selectedDimensionId);
1813
1825
if (applyInFlight)
@@ -48,6 +48,7 @@ import48
isOverworld48
isOverworld49
} from "/shared/world/dimensions.js";49
} from "/shared/world/dimensions.js";50
import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";50
import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";51
import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "./saveAck.js";51
52
52
const game = new Game();53
const game = new Game();53
let socket;54
let socket;@@ -70,6 +71,8 @@ let neighborSpawnersByChunk = new Map();70
let pendingNavigate = null;71
let pendingNavigate = null;71
/** When true, next matching editorChunk after save should navigate to pendingNavigate. */72
/** When true, next matching editorChunk after save should navigate to pendingNavigate. */72
let navigateAfterSave = false;73
let navigateAfterSave = false;74
/** Keys (`dimensionId:chunkX,chunkY`) for in-flight `EDITOR_SAVE_CHUNK` acks. */75
const pendingSaveAcks = new Set();73
let chunkX = 0;76
let chunkX = 0;74
let chunkY = 0;77
let chunkY = 0;75
let selectedDimensionId = OVERWORLD_ID;78
let selectedDimensionId = OVERWORLD_ID;@@ -819,6 +822,7 @@ function saveSelectedChunk ()819
const neighborSaves = [...dirtyNeighborChunks.entries()];822
const neighborSaves = [...dirtyNeighborChunks.entries()];820
clearNeighborEditState();823
clearNeighborEditState();821
const savedChunks = [{ chunkX, chunkY }];824
const savedChunks = [{ chunkX, chunkY }];825
pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, chunkX, chunkY));822
socket.send({826
socket.send({823
type: MessageType.editorSaveChunk,827
type: MessageType.editorSaveChunk,824
chunkX,828
chunkX,@@ -835,6 +839,7 @@ function saveSelectedChunk ()835
{839
{836
const [cx, cy] = key.split(",").map(Number);840
const [cx, cy] = key.split(",").map(Number);837
savedChunks.push({ chunkX: cx, chunkY: cy });841
savedChunks.push({ chunkX: cx, chunkY: cy });842
pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, cx, cy));838
socket.send({843
socket.send({839
type: MessageType.editorSaveChunk,844
type: MessageType.editorSaveChunk,840
chunkX: cx,845
chunkX: cx,@@ -1107,12 +1112,15 @@ function setRevisionState ({ activeRevisionId: active, pendingRevisionId: pendin1107
updateRevisionUI();1112
updateRevisionUI();1108
}1113
}1109
1114
1110
function showSelectedChunk (payload)1115
function showSelectedChunk (payload, { isSaveAck = false } = {})1111
{1116
{1112
const keepDirtyDraft = !!draft1117
const keepDirtyDraft = !shouldApplySelectedEditorChunk({1113
&& payload.chunkX === chunkX1118
isSaveAck,1114
&& payload.chunkY === chunkY1119
isDirty: !!draft1115
&& isSelectedChunkDirty();1120
&& payload.chunkX === chunkX1121
&& payload.chunkY === chunkY1122
&& isSelectedChunkDirty()1123
});1116
if (!keepDirtyDraft)1124
if (!keepDirtyDraft)1117
{ clearNeighborEditState(); }1125
{ clearNeighborEditState(); }1118
if (keepDirtyDraft)1126
if (keepDirtyDraft)@@ -1164,7 +1172,7 @@ function showSelectedChunk (payload)1164
}1172
}1165
view.applySnapshot(snapshotFromPayload(payload));1173
view.applySnapshot(snapshotFromPayload(payload));1166
syncSpawnersOverlay(payload.chunkX, payload.chunkY, draft.spawners);1174
syncSpawnersOverlay(payload.chunkX, payload.chunkY, draft.spawners);1167
const restored = tryRestoreDraftBackupAfterServerLoad(payload);1175
const restored = isSaveAck ? false : tryRestoreDraftBackupAfterServerLoad(payload);1168
if (!restored)1176
if (!restored)1169
{ captureDraftBaseline(); }1177
{ captureDraftBaseline(); }1170
else1178
else@@ -1763,15 +1771,18 @@ async function main ()1763
{ renderGlobalChangelog(message.changelog ?? []); }1771
{ renderGlobalChangelog(message.changelog ?? []); }1764
else if (message.type === MessageType.editorChunk)1772
else if (message.type === MessageType.editorChunk)1765
{1773
{1766
const msgDim = normalizeDimensionId(message.dimensionId ?? OVERWORLD_ID);1774
const msgDim = normalizeDimensionId(message.dimensionId ?? selectedDimensionId);1767
if (msgDim !== selectedDimensionId)1775
if (msgDim !== selectedDimensionId)1768
{ return; }1776
{ return; }1777
const isSaveAck = pendingSaveAcks.delete(1778
editorChunkSaveKey(msgDim, message.chunkX, message.chunkY)1779
);1769
if (message.settings)1780
if (message.settings)1770
{ view.setSettings(message.settings); }1781
{ view.setSettings(message.settings); }1771
if (!inEditorView(message.chunkX, message.chunkY))1782
if (!inEditorView(message.chunkX, message.chunkY))1772
{ return; }1783
{ return; }1773
if (message.chunkX === chunkX && message.chunkY === chunkY)1784
if (message.chunkX === chunkX && message.chunkY === chunkY)1774
{ showSelectedChunk(message); }1785
{ showSelectedChunk(message, { isSaveAck }); }1775
else1786
else1776
{1787
{1777
const key = chunkKey(message.chunkX, message.chunkY);1788
const key = chunkKey(message.chunkX, message.chunkY);@@ -1808,6 +1819,7 @@ async function main ()1808
if (giftsModal?.isOpen())1819
if (giftsModal?.isOpen())1809
{ giftsModal.handleError?.(message.message); }1820
{ giftsModal.handleError?.(message.message); }1810
pendingCreatedDimensionId = null;1821
pendingCreatedDimensionId = null;1822
pendingSaveAcks.clear();1811
navigateAfterSave = false;1823
navigateAfterSave = false;1812
dimensionPicker?.setValue(selectedDimensionId);1824
dimensionPicker?.setValue(selectedDimensionId);1813
if (applyInFlight)1825
if (applyInFlight)@@ -0,0 +1,24 @@
1
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
2
3
/**
4
* Editor chunk saves and reloads share the same `EDITOR_CHUNK` reply.
5
* After `EDITOR_SAVE_CHUNK` the local draft is still dirty (baseline is not
6
* updated until the ack is applied). Treating that ack like a reconnect reload
7
* leaves the Save button dirty even though the server already persisted.
8
*/
9
10
export function editorChunkSaveKey (dimensionId, chunkX, chunkY)
11
{
12
return `${normalizeDimensionId(dimensionId ?? OVERWORLD_ID)}:${chunkX | 0},${chunkY | 0}`;
13
}
14
15
/**
16
* Whether an incoming selected-chunk `EDITOR_CHUNK` should replace the local draft.
17
* Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint.
18
*
19
* @param {{ isSaveAck?: boolean, isDirty?: boolean }} options
20
*/
21
export function shouldApplySelectedEditorChunk ({ isSaveAck = false, isDirty = false } = {})
22
{
23
return !!isSaveAck || !isDirty;
24
}
@@ -0,0 +1,24 @@1
import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";2
3
/**4
* Editor chunk saves and reloads share the same `EDITOR_CHUNK` reply.5
* After `EDITOR_SAVE_CHUNK` the local draft is still dirty (baseline is not6
* updated until the ack is applied). Treating that ack like a reconnect reload7
* leaves the Save button dirty even though the server already persisted.8
*/9
10
export function editorChunkSaveKey (dimensionId, chunkX, chunkY)11
{12
return `${normalizeDimensionId(dimensionId ?? OVERWORLD_ID)}:${chunkX | 0},${chunkY | 0}`;13
}14
15
/**16
* Whether an incoming selected-chunk `EDITOR_CHUNK` should replace the local draft.17
* Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint.18
*19
* @param {{ isSaveAck?: boolean, isDirty?: boolean }} options20
*/21
export function shouldApplySelectedEditorChunk ({ isSaveAck = false, isDirty = false } = {})22
{23
return !!isSaveAck || !isDirty;24
}@@ -0,0 +1,27 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
4
import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "../src/editor/saveAck.js";
5
6
test("editorChunkSaveKey is dimension-scoped", () =>
7
{
8
assert.equal(editorChunkSaveKey(OVERWORLD_ID, 1, 2), "overworld:1,2");
9
assert.equal(editorChunkSaveKey("Cave A", 1, 2), "cave-a:1,2");
10
assert.notEqual(
11
editorChunkSaveKey(OVERWORLD_ID, 0, 0),
12
editorChunkSaveKey("cave_a", 0, 0)
13
);
14
assert.equal(editorChunkSaveKey(undefined, 0, 0), editorChunkSaveKey(OVERWORLD_ID, 0, 0));
15
});
16
17
test("save ack applies even when the local draft is still dirty", () =>
18
{
19
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: true }), true);
20
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: false }), true);
21
});
22
23
test("non-ack reload keeps unsaved paint on the selected chunk", () =>
24
{
25
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);
26
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);
27
});
@@ -0,0 +1,27 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { OVERWORLD_ID } from "../../shared/world/dimensions.js";4
import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "../src/editor/saveAck.js";5
6
test("editorChunkSaveKey is dimension-scoped", () =>7
{8
assert.equal(editorChunkSaveKey(OVERWORLD_ID, 1, 2), "overworld:1,2");9
assert.equal(editorChunkSaveKey("Cave A", 1, 2), "cave-a:1,2");10
assert.notEqual(11
editorChunkSaveKey(OVERWORLD_ID, 0, 0),12
editorChunkSaveKey("cave_a", 0, 0)13
);14
assert.equal(editorChunkSaveKey(undefined, 0, 0), editorChunkSaveKey(OVERWORLD_ID, 0, 0));15
});16
17
test("save ack applies even when the local draft is still dirty", () =>18
{19
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: true }), true);20
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: false }), true);21
});22
23
test("non-ack reload keeps unsaved paint on the selected chunk", () =>24
{25
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);26
assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);27
});@@ -7,6 +7,7 @@ import { WorldDatabase } from "../src/persistence/worldDb.js";
7
7
import { StateDatabase } from "../src/persistence/stateDb.js";
8
8
import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js";
9
9
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
10
import { BlockType } from "../../shared/definitions/tiles.js";
10
11
11
12
function tempDirs ()
12
13
{
@@ -78,3 +79,37 @@ test("quest and dialogue saves append changelogs with usernames", () =>
78
79
stateDb.close();
79
80
}
80
81
});
82
83
test("saveChunk persists Void in a non-overworld dimension and returns it on the ack payload", () =>
84
{
85
const { worldDb, stateDb } = tempDirs();
86
try
87
{
88
stateDb.createAccount("auth:42", true, null, "timfa");
89
const editor = new EditorService(worldDb, stateDb);
90
editor.createDimension("void_hall", "Void Hall", "auth:42");
91
const loaded = editor.loadChunk(1, 2, "void_hall");
92
const blocks = loaded.data.blocks.slice();
93
blocks[0] = { typeId: BlockType.void, state: null, config: null };
94
const saved = editor.saveChunk(1, 2, {
95
open: true,
96
grounds: loaded.data.grounds,
97
blocks,
98
spawners: []
99
}, "auth:42", "void_hall");
100
assert.equal(saved.dimensionId, "void_hall");
101
assert.equal(saved.chunkX, 1);
102
assert.equal(saved.chunkY, 2);
103
assert.equal(saved.data.blocks[0]?.typeId, BlockType.void);
104
assert.ok(saved.changelog.some((entry) =>
105
(entry.diffEntries ?? []).some((line) => line.includes("Block Void placed"))));
106
const reloaded = editor.loadChunk(1, 2, "void_hall");
107
assert.equal(reloaded.data.blocks[0]?.typeId, BlockType.void);
108
assert.equal(reloaded.dimensionId, "void_hall");
109
}
110
finally
111
{
112
worldDb.close();
113
stateDb.close();
114
}
115
});
@@ -7,6 +7,7 @@ import { WorldDatabase } from "../src/persistence/worldDb.js";7
import { StateDatabase } from "../src/persistence/stateDb.js";7
import { StateDatabase } from "../src/persistence/stateDb.js";8
import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js";8
import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js";9
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";9
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";10
import { BlockType } from "../../shared/definitions/tiles.js";10
11
11
function tempDirs ()12
function tempDirs ()12
{13
{@@ -78,3 +79,37 @@ test("quest and dialogue saves append changelogs with usernames", () =>78
stateDb.close();79
stateDb.close();79
}80
}80
});81
});82
83
test("saveChunk persists Void in a non-overworld dimension and returns it on the ack payload", () =>84
{85
const { worldDb, stateDb } = tempDirs();86
try87
{88
stateDb.createAccount("auth:42", true, null, "timfa");89
const editor = new EditorService(worldDb, stateDb);90
editor.createDimension("void_hall", "Void Hall", "auth:42");91
const loaded = editor.loadChunk(1, 2, "void_hall");92
const blocks = loaded.data.blocks.slice();93
blocks[0] = { typeId: BlockType.void, state: null, config: null };94
const saved = editor.saveChunk(1, 2, {95
open: true,96
grounds: loaded.data.grounds,97
blocks,98
spawners: []99
}, "auth:42", "void_hall");100
assert.equal(saved.dimensionId, "void_hall");101
assert.equal(saved.chunkX, 1);102
assert.equal(saved.chunkY, 2);103
assert.equal(saved.data.blocks[0]?.typeId, BlockType.void);104
assert.ok(saved.changelog.some((entry) =>105
(entry.diffEntries ?? []).some((line) => line.includes("Block Void placed"))));106
const reloaded = editor.loadChunk(1, 2, "void_hall");107
assert.equal(reloaded.data.blocks[0]?.typeId, BlockType.void);108
assert.equal(reloaded.dimensionId, "void_hall");109
}110
finally111
{112
worldDb.close();113
stateDb.close();114
}115
});@@ -29,3 +29,12 @@ test("chunk diffs describe spawner placement", () =>
29
29
const bullets = diffChunks(0, 0, previous, next, true);
30
30
assert.ok(bullets.some((line) => line.includes("Spawner (deer) placed at 2, 3.")));
31
31
});
32
33
test("chunk diffs name the Void block", () =>
34
{
35
const previous = createEmptyChunkData();
36
const next = createEmptyChunkData();
37
next.blocks[0] = { typeId: BlockType.void, state: null, config: null };
38
const bullets = diffChunks(0, 0, previous, next, true);
39
assert.ok(bullets.some((line) => line.includes("Block Void placed at 0, 0.")));
40
});
@@ -29,3 +29,12 @@ test("chunk diffs describe spawner placement", () =>29
const bullets = diffChunks(0, 0, previous, next, true);29
const bullets = diffChunks(0, 0, previous, next, true);30
assert.ok(bullets.some((line) => line.includes("Spawner (deer) placed at 2, 3.")));30
assert.ok(bullets.some((line) => line.includes("Spawner (deer) placed at 2, 3.")));31
});31
});32
33
test("chunk diffs name the Void block", () =>34
{35
const previous = createEmptyChunkData();36
const next = createEmptyChunkData();37
next.blocks[0] = { typeId: BlockType.void, state: null, config: null };38
const bullets = diffChunks(0, 0, previous, next, true);39
assert.ok(bullets.some((line) => line.includes("Block Void placed at 0, 0.")));40
});