Skip to content

Commit 350ea41

Browse files
crisbetoalxhub
authored andcommitted
refactor(core): align navigation types with built in ones
Aligns our clone of the navigation API types with the built-in TypeScript types. This is related to an internal issue. (cherry picked from commit d79b3b6)
1 parent 1b85b3c commit 350ea41

4 files changed

Lines changed: 43 additions & 32 deletions

File tree

packages/core/primitives/dom-navigation/src/navigation_types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export declare class Navigation extends EventTarget {
6464
}
6565

6666
export declare class NavigationTransition {
67-
readonly navigationType: NavigationTypeString;
67+
readonly navigationType: NavigationType;
6868
readonly from: NavigationHistoryEntry;
6969
readonly to: NavigationDestination;
7070
readonly finished: Promise<void>;
@@ -108,7 +108,7 @@ export declare class NavigationHistoryEntry extends EventTarget {
108108
): void;
109109
}
110110

111-
export type NavigationTypeString = 'reload' | 'push' | 'replace' | 'traverse';
111+
export type NavigationType = 'push' | 'reload' | 'replace' | 'traverse';
112112

113113
export interface NavigationUpdateCurrentEntryOptions {
114114
state: unknown;
@@ -130,19 +130,19 @@ export interface NavigationReloadOptions extends NavigationOptions {
130130
export declare class NavigationCurrentEntryChangeEvent extends Event {
131131
constructor(type: string, eventInit?: NavigationCurrentEntryChangeEventInit);
132132

133-
readonly navigationType: NavigationTypeString | null;
133+
readonly navigationType: NavigationType | null;
134134
readonly from: NavigationHistoryEntry;
135135
}
136136

137137
export interface NavigationCurrentEntryChangeEventInit extends EventInit {
138-
navigationType?: NavigationTypeString | null;
138+
navigationType?: NavigationType | null;
139139
from: NavigationHistoryEntry;
140140
}
141141

142142
export declare class NavigateEvent extends Event {
143143
constructor(type: string, eventInit?: NavigateEventInit);
144144

145-
readonly navigationType: NavigationTypeString;
145+
readonly navigationType: NavigationType;
146146
readonly canIntercept: boolean;
147147
readonly userInitiated: boolean;
148148
readonly hashChange: boolean;
@@ -157,7 +157,7 @@ export declare class NavigateEvent extends Event {
157157
}
158158

159159
export interface NavigateEventInit extends EventInit {
160-
navigationType?: NavigationTypeString;
160+
navigationType?: NavigationType;
161161
canIntercept?: boolean;
162162
userInitiated?: boolean;
163163
hashChange?: boolean;
@@ -176,8 +176,8 @@ export interface NavigationInterceptOptions {
176176

177177
export declare class NavigationDestination {
178178
readonly url: string;
179-
readonly key: string | null;
180-
readonly id: string | null;
179+
readonly key: string;
180+
readonly id: string;
181181
readonly index: number;
182182
readonly sameDocument: boolean;
183183

packages/core/primitives/dom-navigation/testing/fake_navigation.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// 3p-only-start
1010
import {
1111
NavigationNavigateOptions,
12-
NavigationTypeString,
12+
NavigationType,
1313
NavigationOptions,
1414
NavigateEvent,
1515
NavigationCurrentEntryChangeEvent,
@@ -80,6 +80,8 @@ export class FakeNavigation implements Navigation {
8080
*/
8181
eventTarget: EventTarget;
8282

83+
readonly activation: NavigationActivation | null = null;
84+
8385
/** The next unique id for created entries. Replace recreates this id. */
8486
private nextId = 0;
8587

@@ -179,7 +181,7 @@ export class FakeNavigation implements Navigation {
179181
const fromUrl = new URL(this.currentEntry.url!);
180182
const toUrl = new URL(url, this.currentEntry.url!);
181183

182-
let navigationType: NavigationTypeString;
184+
let navigationType: NavigationType;
183185
if (!options?.history || options.history === 'auto') {
184186
// Auto defaults to push, but if the URLs are the same, is a replace.
185187
if (fromUrl.toString() === toUrl.toString()) {
@@ -231,7 +233,7 @@ export class FakeNavigation implements Navigation {
231233
}
232234

233235
private pushOrReplaceState(
234-
navigationType: NavigationTypeString,
236+
navigationType: NavigationType,
235237
data: unknown,
236238
_title: string,
237239
url?: string,
@@ -617,13 +619,15 @@ export class FakeNavigation implements Navigation {
617619
}
618620

619621
set oncurrententrychange(
620-
_handler: // tslint:disable-next-line:no-any
622+
_handler:
623+
// tslint:disable-next-line:no-any
621624
((this: Navigation, ev: NavigationCurrentEntryChangeEvent) => any) | null,
622625
) {
623626
throw new Error('unimplemented');
624627
}
625628

626-
get oncurrententrychange(): // tslint:disable-next-line:no-any
629+
get oncurrententrychange():
630+
// tslint:disable-next-line:no-any
627631
((this: Navigation, ev: NavigationCurrentEntryChangeEvent) => any) | null {
628632
throw new Error('unimplemented');
629633
}
@@ -816,7 +820,7 @@ function dispatchNavigateEvent({
816820
canIntercept: boolean;
817821
userInitiated: boolean;
818822
hashChange: boolean;
819-
navigationType: NavigationTypeString;
823+
navigationType: NavigationType;
820824
destination: FakeNavigationDestination;
821825
info: unknown;
822826
sameDocument: boolean;
@@ -846,7 +850,7 @@ function dispatchNavigateEvent({
846850
event.sameDocument = sameDocument;
847851

848852
let precommitHandlers: Array<(controller: NavigationPrecommitController) => Promise<void>> = [];
849-
let handlers: Array<() => Promise<void>> = [];
853+
let handlers: Array<() => PromiseLike<void> | void> = [];
850854

851855
// https://whatpr.org/html/10919/nav-history-apis.html#dom-navigateevent-intercept
852856
event.intercept = function (
@@ -962,7 +966,14 @@ function dispatchNavigateEvent({
962966
}
963967
}
964968
(navigation.transition as InternalNavigationTransition)?.committedResolve();
965-
const promisesList: Array<Promise<unknown>> = handlers.map((handler) => handler());
969+
const promisesList: Array<PromiseLike<unknown>> = [];
970+
for (const handler of handlers) {
971+
const handlerResult = handler();
972+
973+
if (handlerResult) {
974+
promisesList.push(handlerResult);
975+
}
976+
}
966977
promisesList.push(result.committed);
967978
Promise.all(promisesList)
968979
.then(() => {
@@ -1134,7 +1145,7 @@ function createFakeNavigationCurrentEntryChangeEvent({
11341145
navigationType,
11351146
}: {
11361147
from: FakeNavigationHistoryEntry;
1137-
navigationType: NavigationTypeString;
1148+
navigationType: NavigationType;
11381149
}) {
11391150
const event = new Event('currententrychange', {
11401151
bubbles: false,
@@ -1176,8 +1187,8 @@ function createHashChangeEvent(newURL: string, oldURL: string) {
11761187
export class FakeNavigationDestination implements NavigationDestination {
11771188
url: string;
11781189
readonly sameDocument: boolean;
1179-
readonly key: string | null;
1180-
readonly id: string | null;
1190+
readonly key: string;
1191+
readonly id: string;
11811192
readonly index: number;
11821193

11831194
state?: unknown;
@@ -1204,8 +1215,8 @@ export class FakeNavigationDestination implements NavigationDestination {
12041215
this.sameDocument = sameDocument;
12051216
this.state = state;
12061217
this.historyState = historyState;
1207-
this.key = key;
1208-
this.id = id;
1218+
this.key = key ?? '';
1219+
this.id = id ?? '';
12091220
this.index = index;
12101221
}
12111222

@@ -1238,7 +1249,7 @@ class InternalNavigationTransition implements NavigationTransition {
12381249
constructor(
12391250
readonly from: NavigationHistoryEntry,
12401251
readonly to: NavigationDestination,
1241-
readonly navigationType: NavigationTypeString,
1252+
readonly navigationType: NavigationType,
12421253
) {
12431254
this.finished = new Promise<void>((resolve, reject) => {
12441255
this.finishedReject = reject;
@@ -1303,7 +1314,7 @@ class InternalNavigationResult {
13031314

13041315
/** Internal options for performing a navigate. */
13051316
interface InternalNavigateOptions {
1306-
navigationType: NavigationTypeString;
1317+
navigationType: NavigationType;
13071318
cancelable: boolean;
13081319
canIntercept: boolean;
13091320
userInitiated: boolean;

packages/core/primitives/dom-navigation/testing/test/fake_platform_navigation.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ describe('navigation', () => {
162162
signal: jasmine.any(AbortSignal),
163163
destination: jasmine.objectContaining({
164164
url: 'https://test.com/test',
165-
key: null,
166-
id: null,
165+
key: '',
166+
id: '',
167167
index: -1,
168168
sameDocument: false,
169169
}),
@@ -235,8 +235,8 @@ describe('navigation', () => {
235235
signal: jasmine.any(AbortSignal),
236236
destination: jasmine.objectContaining({
237237
url: 'https://test.com/test',
238-
key: null,
239-
id: null,
238+
key: '',
239+
id: '',
240240
index: -1,
241241
sameDocument: false,
242242
}),
@@ -1515,8 +1515,8 @@ describe('navigation', () => {
15151515
signal: jasmine.any(AbortSignal),
15161516
destination: jasmine.objectContaining({
15171517
url: 'https://test.com/test',
1518-
key: null,
1519-
id: null,
1518+
key: '',
1519+
id: '',
15201520
index: -1,
15211521
sameDocument: true,
15221522
}),
@@ -1571,8 +1571,8 @@ describe('navigation', () => {
15711571
signal: jasmine.any(AbortSignal),
15721572
destination: jasmine.objectContaining({
15731573
url: 'https://test.com/test',
1574-
key: null,
1575-
id: null,
1574+
key: '',
1575+
id: '',
15761576
index: -1,
15771577
sameDocument: true,
15781578
}),

packages/core/src/core_private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export {
1717
type NavigationReloadOptions as ɵNavigationReloadOptions,
1818
type NavigationResult as ɵNavigationResult,
1919
type NavigationTransition as ɵNavigationTransition,
20-
type NavigationTypeString as ɵNavigationTypeString,
20+
type NavigationType as ɵNavigationType,
2121
type NavigationUpdateCurrentEntryOptions as ɵNavigationUpdateCurrentEntryOptions,
2222
} from '../primitives/dom-navigation';
2323
export {maybeUnwrapDefaultExport as ɵmaybeUnwrapDefaultExport} from './util/default_export';

0 commit comments

Comments
 (0)