Skip to content

Commit 907bd33

Browse files
committed
split NavigationType and NavigationKind
1 parent e9b08f1 commit 907bd33

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/browser/html/Navigation.zig

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ pub const Interfaces = .{
3838
NavigationHistoryEntry,
3939
};
4040

41-
pub const NavigationKind = union(enum) {
42-
initial,
41+
pub const NavigationType = enum {
42+
push,
43+
replace,
44+
traverse,
45+
reload,
46+
};
47+
48+
pub const NavigationKind = union(NavigationType) {
4349
push: ?[]const u8,
4450
replace,
4551
traverse: usize,
@@ -191,21 +197,26 @@ pub fn _forward(self: *Navigation, page: *Page) !NavigationReturn {
191197
}
192198

193199
// This is for after true navigation processing, where we need to ensure that our entries are up to date.
194-
pub fn processNavigation(self: *Navigation, url: []const u8, kind: NavigationKind, page: *Page) !void {
195-
switch (kind) {
196-
.initial => {
197-
_ = try self.pushEntry(url, null, page);
198-
},
199-
.replace => {
200-
// When replacing, we just update the URL but the state is nullified.
201-
const entry = self.currentEntry();
202-
entry.url = url;
203-
entry.state = null;
204-
},
205-
.push => |state| {
206-
_ = try self.pushEntry(url, state, page);
207-
},
208-
.traverse, .reload => {},
200+
// This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct.
201+
pub fn processNavigation(self: *Navigation, page: *Page) !void {
202+
const url = page.url.raw;
203+
const kind = page.session.navigation_kind;
204+
205+
if (kind) |k| {
206+
switch (k) {
207+
.replace => {
208+
// When replacing, we just update the URL but the state is nullified.
209+
const entry = self.currentEntry();
210+
entry.url = url;
211+
entry.state = null;
212+
},
213+
.push => |state| {
214+
_ = try self.pushEntry(url, state, page);
215+
},
216+
.traverse, .reload => {},
217+
}
218+
} else {
219+
_ = try self.pushEntry(url, null, page);
209220
}
210221
}
211222

src/browser/page.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ pub const Page = struct {
817817
}
818818

819819
// We need to handle different navigation types differently.
820-
try self.session.navigation.processNavigation(self.url.raw, self.session.navigation_kind, self);
820+
try self.session.navigation.processNavigation(self);
821821
}
822822

823823
fn pageErrorCallback(ctx: *anyopaque, err: anyerror) void {

src/browser/session.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub const Session = struct {
6060
// https://developer.mozilla.org/en-US/docs/Web/API/History
6161
history: History = .{},
6262
navigation: Navigation = .{},
63-
navigation_kind: NavigationKind = .initial,
63+
navigation_kind: ?NavigationKind = null,
6464

6565
page: ?Page = null,
6666

0 commit comments

Comments
 (0)