Skip to content

Commit 9992bd0

Browse files
committed
clean up history api
1 parent e74d7fa commit 9992bd0

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/browser/html/History.zig

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const History = @This();
2727

2828
const HistoryEntry = struct {
2929
url: []const u8,
30-
// Serialized as JSON.
30+
// This is serialized as JSON because
31+
// History must survive a JsContext.
3132
state: ?[]u8,
3233
};
3334

@@ -44,25 +45,26 @@ const ScrollRestorationMode = enum {
4445
return null;
4546
}
4647
}
48+
49+
pub fn toString(self: ScrollRestorationMode) []const u8 {
50+
return @tagName(self);
51+
}
4752
};
4853

49-
scrollRestoration: ScrollRestorationMode = .auto,
54+
scroll_restoration: ScrollRestorationMode = .auto,
5055
stack: std.ArrayListUnmanaged(HistoryEntry) = .empty,
5156
current: ?usize = null,
5257

5358
pub fn get_length(self: *History) u32 {
5459
return @intCast(self.stack.items.len);
5560
}
5661

57-
pub fn get_scrollRestoration(self: *History) []const u8 {
58-
return switch (self.scrollRestoration) {
59-
.auto => "auto",
60-
.manual => "manual",
61-
};
62+
pub fn get_scrollRestoration(self: *History) ScrollRestorationMode {
63+
return self.scroll_restoration;
6264
}
6365

6466
pub fn set_scrollRestoration(self: *History, mode: []const u8) void {
65-
self.scrollRestoration = ScrollRestorationMode.fromString(mode) orelse self.scrollRestoration;
67+
self.scroll_restoration = ScrollRestorationMode.fromString(mode) orelse self.scroll_restoration;
6668
}
6769

6870
pub fn get_state(self: *History, page: *Page) !?Env.Value {
@@ -102,23 +104,20 @@ pub fn dispatchPopStateEvent(state: ?[]const u8, page: *Page) void {
102104
};
103105
}
104106

105-
fn _dispatchPopStateEvent(
106-
state: ?[]const u8,
107-
page: *Page,
108-
) !void {
107+
fn _dispatchPopStateEvent(state: ?[]const u8, page: *Page) !void {
109108
var evt = try PopStateEvent.constructor("popstate", .{ .state = state });
110109

111110
_ = try parser.eventTargetDispatchEvent(
112111
@as(*parser.EventTarget, @ptrCast(&page.window)),
113-
@as(*parser.Event, @ptrCast(&evt)),
112+
&evt.proto,
114113
);
115114
}
116115

117116
pub fn _pushState(self: *History, state: Env.JsObject, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
118117
const arena = page.session.arena;
119118

119+
const json = try state.toJson(arena);
120120
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
121-
const json = try state.toJson(page.session.arena);
122121
const entry = HistoryEntry{ .state = json, .url = url };
123122
try self.stack.append(arena, entry);
124123
self.current = self.stack.items.len - 1;
@@ -129,8 +128,8 @@ pub fn _replaceState(self: *History, state: Env.JsObject, _: ?[]const u8, _url:
129128

130129
if (self.current) |curr| {
131130
const entry = &self.stack.items[curr];
132-
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
133131
const json = try state.toJson(arena);
132+
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
134133
entry.* = HistoryEntry{ .state = json, .url = url };
135134
} else {
136135
try self._pushState(state, "", _url, page);
@@ -152,11 +151,7 @@ pub fn go(self: *History, delta: i32, page: *Page) !void {
152151
self.current = index;
153152

154153
if (try page.isSameOrigin(entry.url)) {
155-
if (entry.state) |state| {
156-
History.dispatchPopStateEvent(state, page);
157-
} else {
158-
History.dispatchPopStateEvent(null, page);
159-
}
154+
History.dispatchPopStateEvent(entry.state, page);
160155
}
161156

162157
try page.navigateFromWebAPI(entry.url, .{ .reason = .history });

0 commit comments

Comments
 (0)