Skip to content

Commit fb4a261

Browse files
authored
fix: Avoid updating snapshot after calling resetAssets (#29)
1 parent b23b235 commit fb4a261

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

integration-tests/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ async function test() {
6262
currentHistoryIndex = Math.max(0, currentHistoryIndex - 1)
6363
const assets = assetsUpdatesHistory[currentHistoryIndex]
6464
creator.resetAssets(assets)
65+
window.assetsSnapshot = assets
6566
})
6667

6768
redoBtn.addEventListener('click', () => {
6869
currentHistoryIndex = Math.min(assetsUpdatesHistory.length - 1, currentHistoryIndex + 1)
6970
const assets = assetsUpdatesHistory[currentHistoryIndex]
7071
creator.resetAssets(assets)
72+
window.assetsSnapshot = assets
7173
})
7274
}
7375

src/logic/index.zig

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ pub fn connect_web_gpu_programs(programs: *const WebGpuPrograms) void {
2525
web_gpu_programs = programs; // orelse WebGpuPrograms{};
2626
}
2727

28-
var on_asset_update_cb: *const fn ([]AssetZig) void = undefined;
29-
pub fn connect_on_asset_update_callback(cb: *const fn ([]AssetZig) void) void {
28+
var on_asset_update_cb: *const fn ([]const AssetZig) void = undefined;
29+
pub fn connect_on_asset_update_callback(cb: *const fn ([]const AssetZig) void) void {
3030
on_asset_update_cb = cb;
3131
}
3232

33+
fn on_asset_update_noop(_: []const AssetZig) void {}
34+
3335
var on_asset_select_cb: *const fn (u32) void = undefined;
3436
pub fn connect_on_asset_selection_callback(cb: *const fn (u32) void) void {
3537
on_asset_select_cb = cb;
@@ -310,7 +312,10 @@ pub fn picks_render() void {
310312
}
311313
}
312314

313-
pub fn reset_assets(new_assets: []AssetZig) void {
315+
pub fn reset_assets(new_assets: []const AssetZig) void {
316+
const real_callback_pointer = on_asset_update_cb;
317+
on_asset_update_cb = &on_asset_update_noop;
318+
314319
state.assets.clearAndFree();
315320

316321
for (new_assets) |asset| {
@@ -321,6 +326,8 @@ pub fn reset_assets(new_assets: []AssetZig) void {
321326
state.active_asset_id = 0;
322327
on_asset_select_cb(state.active_asset_id);
323328
}
329+
330+
on_asset_update_cb = real_callback_pointer;
324331
}
325332

326333
pub fn destroy_state() void {
@@ -338,3 +345,44 @@ pub fn destroy_state() void {
338345
pub fn import_icons(data: []const f32) void {
339346
Msdf.init_icons(data);
340347
}
348+
349+
test "reset_assets does not call the real update callback" {
350+
// Setup initial state
351+
init_state(100, 100);
352+
// Ensure state is cleaned up after the test
353+
defer destroy_state();
354+
355+
// Define a mock callback function locally, with its own static state.
356+
const MockCallback = struct {
357+
// This static variable will hold the state for our mock.
358+
// It's reset to false before each test run.
359+
var was_called: bool = false;
360+
361+
fn assets_update(_: []const AssetZig) void {
362+
// Modify the static variable within the struct.
363+
was_called = true;
364+
}
365+
366+
fn assets_selection(_: u32) void {}
367+
};
368+
369+
// Connect our mock callback. This is the "real" callback for this test.
370+
connect_on_asset_update_callback(MockCallback.assets_update);
371+
connect_on_asset_selection_callback(MockCallback.assets_selection);
372+
373+
// Call the function we are testing
374+
const initial_assets = [_]AssetZig{AssetZig{
375+
.points = [_]Types.PointUV{
376+
Types.PointUV{ .x = 0.0, .y = 0.0, .u = 0.0, .v = 0.0 },
377+
Types.PointUV{ .x = 1.0, .y = 0.0, .u = 1.0, .v = 0.0 },
378+
Types.PointUV{ .x = 1.0, .y = 1.0, .u = 1.0, .v = 1.0 },
379+
Types.PointUV{ .x = 0.0, .y = 1.0, .u = 0.0, .v = 1.0 },
380+
},
381+
.texture_id = 1,
382+
.id = 123,
383+
}};
384+
reset_assets(&initial_assets);
385+
386+
// for the duration of reset_assets, the update callback should NOT be called
387+
try std.testing.expect(!MockCallback.was_called);
388+
}

0 commit comments

Comments
 (0)