Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration-tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ async function test() {
currentHistoryIndex = Math.max(0, currentHistoryIndex - 1)
const assets = assetsUpdatesHistory[currentHistoryIndex]
creator.resetAssets(assets)
window.assetsSnapshot = assets
})

redoBtn.addEventListener('click', () => {
currentHistoryIndex = Math.min(assetsUpdatesHistory.length - 1, currentHistoryIndex + 1)
const assets = assetsUpdatesHistory[currentHistoryIndex]
creator.resetAssets(assets)
window.assetsSnapshot = assets
})
}

Expand Down
54 changes: 51 additions & 3 deletions src/logic/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ pub fn connect_web_gpu_programs(programs: *const WebGpuPrograms) void {
web_gpu_programs = programs; // orelse WebGpuPrograms{};
}

var on_asset_update_cb: *const fn ([]AssetZig) void = undefined;
pub fn connect_on_asset_update_callback(cb: *const fn ([]AssetZig) void) void {
var on_asset_update_cb: *const fn ([]const AssetZig) void = undefined;
pub fn connect_on_asset_update_callback(cb: *const fn ([]const AssetZig) void) void {
on_asset_update_cb = cb;
}

fn on_asset_update_noop(_: []const AssetZig) void {}

var on_asset_select_cb: *const fn (u32) void = undefined;
pub fn connect_on_asset_selection_callback(cb: *const fn (u32) void) void {
on_asset_select_cb = cb;
Expand Down Expand Up @@ -310,7 +312,10 @@ pub fn picks_render() void {
}
}

pub fn reset_assets(new_assets: []AssetZig) void {
pub fn reset_assets(new_assets: []const AssetZig) void {
const real_callback_pointer = on_asset_update_cb;
on_asset_update_cb = &on_asset_update_noop;

state.assets.clearAndFree();

for (new_assets) |asset| {
Expand All @@ -321,6 +326,8 @@ pub fn reset_assets(new_assets: []AssetZig) void {
state.active_asset_id = 0;
on_asset_select_cb(state.active_asset_id);
}

on_asset_update_cb = real_callback_pointer;
}

pub fn destroy_state() void {
Expand All @@ -338,3 +345,44 @@ pub fn destroy_state() void {
pub fn import_icons(data: []const f32) void {
Msdf.init_icons(data);
}

test "reset_assets does not call the real update callback" {
// Setup initial state
init_state(100, 100);
// Ensure state is cleaned up after the test
defer destroy_state();

// Define a mock callback function locally, with its own static state.
const MockCallback = struct {
// This static variable will hold the state for our mock.
// It's reset to false before each test run.
var was_called: bool = false;

fn assets_update(_: []const AssetZig) void {
// Modify the static variable within the struct.
was_called = true;
}

fn assets_selection(_: u32) void {}
};

// Connect our mock callback. This is the "real" callback for this test.
connect_on_asset_update_callback(MockCallback.assets_update);
connect_on_asset_selection_callback(MockCallback.assets_selection);

// Call the function we are testing
const initial_assets = [_]AssetZig{AssetZig{
.points = [_]Types.PointUV{
Types.PointUV{ .x = 0.0, .y = 0.0, .u = 0.0, .v = 0.0 },
Types.PointUV{ .x = 1.0, .y = 0.0, .u = 1.0, .v = 0.0 },
Types.PointUV{ .x = 1.0, .y = 1.0, .u = 1.0, .v = 1.0 },
Types.PointUV{ .x = 0.0, .y = 1.0, .u = 0.0, .v = 1.0 },
},
.texture_id = 1,
.id = 123,
}};
reset_assets(&initial_assets);

// for the duration of reset_assets, the update callback should NOT be called
try std.testing.expect(!MockCallback.was_called);
}
Loading