Skip to content

Commit f475f34

Browse files
committed
seperate Navigation State and History State
1 parent 56e30a9 commit f475f34

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/browser/html/History.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn set_scrollRestoration(self: *History, mode: ScrollRestorationMode) void {
4848
}
4949

5050
pub fn get_state(_: *History, page: *Page) !?js.Value {
51-
if (page.session.navigation.currentEntry().state) |state| {
51+
if (page.session.navigation.currentEntry().state.value) |state| {
5252
const value = try js.Value.fromJson(page.js, state);
5353
return value;
5454
} else {
@@ -61,15 +61,15 @@ pub fn _pushState(_: *const History, state: js.Object, _: ?[]const u8, _url: ?[]
6161
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
6262

6363
const json = state.toJson(arena) catch return error.DataClone;
64-
_ = try page.session.navigation.pushEntry(url, json, page, true);
64+
_ = try page.session.navigation.pushEntry(url, .{ .source = .history, .value = json }, page, true);
6565
}
6666

6767
pub fn _replaceState(_: *const History, state: js.Object, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
6868
const arena = page.session.arena;
6969
const url = if (_url) |u| try arena.dupe(u8, u) else try arena.dupe(u8, page.url.raw);
7070

7171
const json = try state.toJson(arena);
72-
_ = try page.session.navigation.replaceEntry(url, json, page, true);
72+
_ = try page.session.navigation.replaceEntry(url, .{ .source = .history, .value = json }, page, true);
7373
}
7474

7575
pub fn go(_: *const History, delta: i32, page: *Page) !void {
@@ -86,7 +86,7 @@ pub fn go(_: *const History, delta: i32, page: *Page) !void {
8686

8787
if (entry.url) |url| {
8888
if (try page.isSameOrigin(url)) {
89-
PopStateEvent.dispatch(entry.state, page);
89+
PopStateEvent.dispatch(entry.state.value, page);
9090
}
9191
}
9292

src/browser/navigation/Navigation.zig

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Navigation = @This();
3535
const NavigationKind = @import("root.zig").NavigationKind;
3636
const NavigationHistoryEntry = @import("root.zig").NavigationHistoryEntry;
3737
const NavigationTransition = @import("root.zig").NavigationTransition;
38+
const NavigationState = @import("root.zig").NavigationState;
3839
const NavigationCurrentEntryChangeEvent = @import("root.zig").NavigationCurrentEntryChangeEvent;
3940

4041
const NavigationEventTarget = @import("NavigationEventTarget.zig");
@@ -110,10 +111,10 @@ pub fn _forward(self: *Navigation, page: *Page) !NavigationReturn {
110111
pub fn updateEntries(self: *Navigation, url: []const u8, kind: NavigationKind, page: *Page, dispatch: bool) !void {
111112
switch (kind) {
112113
.replace => {
113-
_ = try self.replaceEntry(url, null, page, dispatch);
114+
_ = try self.replaceEntry(url, .{ .source = .navigation, .value = null }, page, dispatch);
114115
},
115116
.push => |state| {
116-
_ = try self.pushEntry(url, state, page, dispatch);
117+
_ = try self.pushEntry(url, .{ .source = .navigation, .value = state }, page, dispatch);
117118
},
118119
.traverse => |index| {
119120
self.index = index;
@@ -132,7 +133,13 @@ pub fn processNavigation(self: *Navigation, page: *Page) !void {
132133

133134
/// Pushes an entry into the Navigation stack WITHOUT actually navigating to it.
134135
/// For that, use `navigate`.
135-
pub fn pushEntry(self: *Navigation, _url: []const u8, state: ?[]const u8, page: *Page, dispatch: bool) !*NavigationHistoryEntry {
136+
pub fn pushEntry(
137+
self: *Navigation,
138+
_url: []const u8,
139+
state: NavigationState,
140+
page: *Page,
141+
dispatch: bool,
142+
) !*NavigationHistoryEntry {
136143
const arena = page.session.arena;
137144

138145
const url = try arena.dupe(u8, _url);
@@ -171,7 +178,13 @@ pub fn pushEntry(self: *Navigation, _url: []const u8, state: ?[]const u8, page:
171178
return entry;
172179
}
173180

174-
pub fn replaceEntry(self: *Navigation, _url: []const u8, state: ?[]const u8, page: *Page, dispatch: bool) !*NavigationHistoryEntry {
181+
pub fn replaceEntry(
182+
self: *Navigation,
183+
_url: []const u8,
184+
state: NavigationState,
185+
page: *Page,
186+
dispatch: bool,
187+
) !*NavigationHistoryEntry {
175188
const arena = page.session.arena;
176189
const url = try arena.dupe(u8, _url);
177190

@@ -242,7 +255,7 @@ pub fn navigate(
242255
// todo: Fire navigate event
243256
try finished.resolve({});
244257

245-
_ = try self.pushEntry(url, state, page, true);
258+
_ = try self.pushEntry(url, .{ .source = .navigation, .value = state }, page, true);
246259
} else {
247260
try page.navigateFromWebAPI(url, .{ .reason = .navigation }, kind);
248261
}
@@ -290,7 +303,7 @@ pub fn _reload(self: *Navigation, _opts: ?ReloadOptions, page: *Page) !Navigatio
290303
const entry = self.currentEntry();
291304
if (opts.state) |state| {
292305
const previous = entry;
293-
entry.state = state.toJson(arena) catch return error.DataClone;
306+
entry.state = .{ .source = .navigation, .value = state.toJson(arena) catch return error.DataClone };
294307
NavigationCurrentEntryChangeEvent.dispatch(self, previous, .reload);
295308
}
296309

@@ -323,6 +336,6 @@ pub fn _updateCurrentEntry(self: *Navigation, options: UpdateCurrentEntryOptions
323336
const arena = page.session.arena;
324337

325338
const previous = self.currentEntry();
326-
self.currentEntry().state = options.state.toJson(arena) catch return error.DataClone;
339+
self.currentEntry().state = .{ .source = .navigation, .value = options.state.toJson(arena) catch return error.DataClone };
327340
NavigationCurrentEntryChangeEvent.dispatch(self, previous, null);
328341
}

src/browser/navigation/root.zig

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ pub const NavigationKind = union(NavigationType) {
5656
reload,
5757
};
5858

59+
pub const NavigationState = struct {
60+
source: enum { history, navigation },
61+
value: ?[]const u8,
62+
};
63+
5964
// https://developer.mozilla.org/en-US/docs/Web/API/NavigationHistoryEntry
6065
pub const NavigationHistoryEntry = struct {
6166
pub const prototype = *EventTarget;
@@ -64,7 +69,7 @@ pub const NavigationHistoryEntry = struct {
6469
id: []const u8,
6570
key: []const u8,
6671
url: ?[]const u8,
67-
state: ?[]const u8,
72+
state: NavigationState,
6873

6974
pub fn get_id(self: *const NavigationHistoryEntry) []const u8 {
7075
return self.id;
@@ -95,12 +100,16 @@ pub const NavigationHistoryEntry = struct {
95100
return self.url;
96101
}
97102

98-
pub fn _getState(self: *const NavigationHistoryEntry, page: *Page) !?js.Value {
99-
if (self.state) |state| {
100-
return try js.Value.fromJson(page.js, state);
101-
} else {
102-
return null;
103+
pub const StateReturn = union(enum) { value: ?js.Value, undefined: void };
104+
105+
pub fn _getState(self: *const NavigationHistoryEntry, page: *Page) !StateReturn {
106+
if (self.state.source == .navigation) {
107+
if (self.state.value) |value| {
108+
return .{ .value = try js.Value.fromJson(page.js, value) };
109+
}
103110
}
111+
112+
return .undefined;
104113
}
105114
};
106115

0 commit comments

Comments
 (0)