1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
import type { DelegateEvent } from 'delegate-it';
import type Swup from '../Swup.js';
import { isPromise, runAsPromise } from '../utils.js';
import { Visit } from './Visit.js';
import type { FetchOptions, PageData } from './fetchPage.js';
export interface HookDefinitions {
'animation:out:start': undefined;
'animation:out:await': { skip: boolean };
'animation:out:end': undefined;
'animation:in:start': undefined;
'animation:in:await': { skip: boolean };
'animation:in:end': undefined;
'animation:skip': undefined;
'cache:clear': undefined;
'cache:set': { page: PageData };
'content:replace': { page: PageData };
'content:scroll': undefined;
'enable': undefined;
'disable': undefined;
'fetch:request': { url: string; options: FetchOptions };
'fetch:error': { url: string; status: number; response: Response };
'fetch:timeout': { url: string };
'history:popstate': { event: PopStateEvent };
'link:click': { el: HTMLAnchorElement; event: DelegateEvent<MouseEvent> };
'link:self': undefined;
'link:anchor': { hash: string };
'link:newtab': { href: string };
'page:load': { page?: PageData; cache?: boolean; options: FetchOptions };
'page:view': { url: string; title: string };
'scroll:top': { options: ScrollIntoViewOptions };
'scroll:anchor': { hash: string; options: ScrollIntoViewOptions };
'visit:start': undefined;
'visit:transition': undefined;
'visit:abort': undefined;
'visit:end': undefined;
}
export interface HookReturnValues {
'content:scroll': Promise<boolean> | boolean;
'fetch:request': Promise<Response>;
'page:load': Promise<PageData>;
'scroll:top': boolean;
'scroll:anchor': boolean;
}
export type HookArguments<T extends HookName> = HookDefinitions[T];
export type HookName = keyof HookDefinitions;
export type HookNameWithModifier = `${HookName}.${HookModifier}`;
type HookModifier = 'once' | 'before' | 'replace';
/** A generic hook handler. */
export type HookHandler<T extends HookName> = (
/** Context about the current visit. */
visit: Visit,
/** Local arguments passed into the handler. */
args: HookArguments<T>
) => Promise<unknown> | unknown;
/** A default hook handler with an expected return type. */
export type HookDefaultHandler<T extends HookName> = (
/** Context about the current visit. */
visit: Visit,
/** Local arguments passed into the handler. */
args: HookArguments<T>,
/** Default handler to be executed. Available if replacing an internal hook handler. */
defaultHandler?: HookDefaultHandler<T>
) => T extends keyof HookReturnValues ? HookReturnValues[T] : Promise<unknown> | unknown;
export type Handlers = {
[K in HookName]: HookHandler<K>[];
};
export type HookInitOptions = {
[K in HookName as K | `${K}.${HookModifier}`]: HookHandler<K>;
} & {
[K in HookName as K | `${K}.${HookModifier}.${HookModifier}`]: HookHandler<K>;
};
/** Unregister a previously registered hook handler. */
export type HookUnregister = () => void;
/** Define when and how a hook handler is executed. */
export type HookOptions = {
/** Execute the hook once, then remove the handler */
once?: boolean;
/** Execute the hook before the internal default handler */
before?: boolean;
/** Set a priority for when to execute this hook. Lower numbers execute first. Default: `0` */
priority?: number;
/** Replace the internal default handler with this hook handler */
replace?: boolean;
};
export type HookRegistration<
T extends HookName,
H extends HookHandler<T> | HookDefaultHandler<T> = HookHandler<T>
> = {
id: number;
hook: T;
handler: H;
defaultHandler?: HookDefaultHandler<T>;
} & HookOptions;
type HookEventDetail = {
hook: HookName;
args: unknown;
visit: Visit;
};
export type HookEvent = CustomEvent<HookEventDetail>;
type HookLedger<T extends HookName> = Map<HookHandler<T>, HookRegistration<T>>;
interface HookRegistry extends Map<HookName, HookLedger<HookName>> {
get<K extends HookName>(key: K): HookLedger<K> | undefined;
set<K extends HookName>(key: K, value: HookLedger<K>): this;
}
/**
* Hook registry.
*
* Create, trigger and handle hooks.
*
*/
export class Hooks {
/** Swup instance this registry belongs to */
protected swup: Swup;
/** Map of all registered hook handlers. */
protected registry: HookRegistry = new Map();
// Can we deduplicate this somehow? Or make it error when not in sync with HookDefinitions?
// https://stackoverflow.com/questions/53387838/how-to-ensure-an-arrays-values-the-keys-of-a-typescript-interface/53395649
protected readonly hooks: HookName[] = [
'animation:out:start',
'animation:out:await',
'animation:out:end',
'animation:in:start',
'animation:in:await',
'animation:in:end',
'animation:skip',
'cache:clear',
'cache:set',
'content:replace',
'content:scroll',
'enable',
'disable',
'fetch:request',
'fetch:error',
'fetch:timeout',
'history:popstate',
'link:click',
'link:self',
'link:anchor',
'link:newtab',
'page:load',
'page:view',
'scroll:top',
'scroll:anchor',
'visit:start',
'visit:transition',
'visit:abort',
'visit:end'
];
constructor(swup: Swup) {
this.swup = swup;
this.init();
}
/**
* Create ledgers for all core hooks.
*/
protected init() {
this.hooks.forEach((hook) => this.create(hook));
}
/**
* Create a new hook type.
*/
create(hook: string) {
if (!this.registry.has(hook as HookName)) {
this.registry.set(hook as HookName, new Map());
}
}
/**
* Check if a hook type exists.
*/
exists(hook: HookName): boolean {
return this.registry.has(hook);
}
/**
* Get the ledger with all registrations for a hook.
*/
protected get<T extends HookName>(hook: T): HookLedger<T> | undefined {
const ledger = this.registry.get(hook);
if (ledger) {
return ledger;
}
console.error(`Unknown hook '${hook}'`);
}
/**
* Remove all handlers of all hooks.
*/
clear() {
this.registry.forEach((ledger) => ledger.clear());
}
/**
* Register a new hook handler.
* @param hook Name of the hook to listen for
* @param handler The handler function to execute
* @param options Object to specify how and when the handler is executed
* Available options:
* - `once`: Only execute the handler once
* - `before`: Execute the handler before the default handler
* - `priority`: Specify the order in which the handlers are executed
* - `replace`: Replace the default handler with this handler
* @returns A function to unregister the handler
*/
// Overload: replacing default handler
on<T extends HookName, O extends HookOptions>(hook: T, handler: HookDefaultHandler<T>, options: O & { replace: true }): HookUnregister; // prettier-ignore
// Overload: passed in handler options
on<T extends HookName, O extends HookOptions>(hook: T, handler: HookHandler<T>, options: O): HookUnregister; // prettier-ignore
// Overload: no handler options
on<T extends HookName>(hook: T, handler: HookHandler<T>): HookUnregister; // prettier-ignore
// Implementation
on<T extends HookName, O extends HookOptions>(
hook: T,
handler: O['replace'] extends true ? HookDefaultHandler<T> : HookHandler<T>,
options: Partial<O> = {}
): HookUnregister {
const ledger = this.get(hook);
if (!ledger) {
console.warn(`Hook '${hook}' not found.`);
return () => {};
}
const id = ledger.size + 1;
const registration: HookRegistration<T> = { ...options, id, hook, handler };
ledger.set(handler, registration);
return () => this.off(hook, handler);
}
/**
* Register a new hook handler to run before the default handler.
* Shortcut for `hooks.on(hook, handler, { before: true })`.
* @param hook Name of the hook to listen for
* @param handler The handler function to execute
* @param options Any other event options (see `hooks.on()` for details)
* @returns A function to unregister the handler
* @see on
*/
// Overload: passed in handler options
before<T extends HookName>(hook: T, handler: HookHandler<T>, options: HookOptions): HookUnregister; // prettier-ignore
// Overload: no handler options
before<T extends HookName>(hook: T, handler: HookHandler<T>): HookUnregister;
// Implementation
before<T extends HookName>(
hook: T,
handler: HookHandler<T>,
options: HookOptions = {}
): HookUnregister {
return this.on(hook, handler, { ...options, before: true });
}
/**
* Register a new hook handler to replace the default handler.
* Shortcut for `hooks.on(hook, handler, { replace: true })`.
* @param hook Name of the hook to listen for
* @param handler The handler function to execute instead of the default handler
* @param options Any other event options (see `hooks.on()` for details)
* @returns A function to unregister the handler
* @see on
*/
// Overload: passed in handler options
replace<T extends HookName>(hook: T, handler: HookDefaultHandler<T>, options: HookOptions): HookUnregister; // prettier-ignore
// Overload: no handler options
replace<T extends HookName>(hook: T, handler: HookDefaultHandler<T>): HookUnregister; // prettier-ignore
// Implementation
replace<T extends HookName>(
hook: T,
handler: HookDefaultHandler<T>,
options: HookOptions = {}
): HookUnregister {
return this.on(hook, handler, { ...options, replace: true });
}
/**
* Register a new hook handler to run once.
* Shortcut for `hooks.on(hook, handler, { once: true })`.
* @param hook Name of the hook to listen for
* @param handler The handler function to execute
* @param options Any other event options (see `hooks.on()` for details)
* @see on
*/
// Overload: passed in handler options
once<T extends HookName>(hook: T, handler: HookHandler<T>, options: HookOptions): HookUnregister; // prettier-ignore
// Overload: no handler options
once<T extends HookName>(hook: T, handler: HookHandler<T>): HookUnregister;
// Implementation
once<T extends HookName>(
hook: T,
handler: HookHandler<T>,
options: HookOptions = {}
): HookUnregister {
return this.on(hook, handler, { ...options, once: true });
}
/**
* Unregister a hook handler.
* @param hook Name of the hook the handler is registered for
* @param handler The handler function that was registered.
* If omitted, all handlers for the hook will be removed.
*/
// Overload: unregister a specific handler
off<T extends HookName>(hook: T, handler: HookHandler<T> | HookDefaultHandler<T>): void;
// Overload: unregister all handlers
off<T extends HookName>(hook: T): void;
// Implementation
off<T extends HookName>(hook: T, handler?: HookHandler<T> | HookDefaultHandler<T>): void {
const ledger = this.get(hook);
if (ledger && handler) {
const deleted = ledger.delete(handler);
if (!deleted) {
console.warn(`Handler for hook '${hook}' not found.`);
}
} else if (ledger) {
ledger.clear();
}
}
/**
* Trigger a hook asynchronously, executing its default handler and all registered handlers.
* Will execute all handlers in order and `await` any `Promise`s they return.
* @param hook Name of the hook to trigger
* @param visit The visit object this hook belongs to
* @param args Arguments to pass to the handler
* @param defaultHandler A default implementation of this hook to execute
* @returns The resolved return value of the executed default handler
*/
// Overload: default order of arguments
async call<T extends HookName>(hook: T, visit: Visit | undefined, args: HookArguments<T>, defaultHandler?: HookDefaultHandler<T>): Promise<Awaited<ReturnType<HookDefaultHandler<T>>>>; // prettier-ignore
// Overload: legacy order of arguments, with visit missing
async call<T extends HookName>(hook: T, args: HookArguments<T>, defaultHandler?: HookDefaultHandler<T>): Promise<Awaited<ReturnType<HookDefaultHandler<T>>>>; // prettier-ignore
// Implementation
async call<T extends HookName>(
hook: T,
arg1: Visit | HookArguments<T>,
arg2: HookArguments<T> | HookDefaultHandler<T>,
arg3?: HookDefaultHandler<T>
): Promise<Awaited<ReturnType<HookDefaultHandler<T>>>> {
const [visit, args, defaultHandler] = this.parseCallArgs(hook, arg1, arg2, arg3);
const { before, handler, after } = this.getHandlers(hook, defaultHandler);
await this.run(before, visit, args);
const [result] = await this.run(handler, visit, args, true);
await this.run(after, visit, args);
this.dispatchDomEvent(hook, visit, args);
return result;
}
/**
* Trigger a hook synchronously, executing its default handler and all registered handlers.
* Will execute all handlers in order, but will **not** `await` any `Promise`s they return.
* @param hook Name of the hook to trigger
* @param visit The visit object this hook belongs to
* @param args Arguments to pass to the handler
* @param defaultHandler A default implementation of this hook to execute
* @returns The (possibly unresolved) return value of the executed default handler
*/
// Overload: default order of arguments
callSync<T extends HookName>(hook: T, visit: Visit | undefined, args: HookArguments<T>, defaultHandler?: HookDefaultHandler<T>): ReturnType<HookDefaultHandler<T>>; // prettier-ignore
// Overload: legacy order of arguments, with visit missing
callSync<T extends HookName>(hook: T, args: HookArguments<T>, defaultHandler?: HookDefaultHandler<T>): ReturnType<HookDefaultHandler<T>>; // prettier-ignore
// Implementation
callSync<T extends HookName>(
hook: T,
arg1: Visit | HookArguments<T>,
arg2: HookArguments<T> | HookDefaultHandler<T>,
arg3?: HookDefaultHandler<T>
): ReturnType<HookDefaultHandler<T>> {
const [visit, args, defaultHandler] = this.parseCallArgs(hook, arg1, arg2, arg3);
const { before, handler, after } = this.getHandlers(hook, defaultHandler);
this.runSync(before, visit, args);
const [result] = this.runSync(handler, visit, args, true);
this.runSync(after, visit, args);
this.dispatchDomEvent(hook, visit, args);
return result;
}
/**
* Parse the call arguments for call() and callSync() to allow legacy argument order.
*/
protected parseCallArgs<T extends HookName>(
hook: T,
arg1: Visit | HookArguments<T> | undefined,
arg2: HookArguments<T> | HookDefaultHandler<T>,
arg3?: HookDefaultHandler<T>
): [Visit | undefined, HookArguments<T>, HookDefaultHandler<T> | undefined] {
const isLegacyOrder =
!(arg1 instanceof Visit) && (typeof arg1 === 'object' || typeof arg2 === 'function');
if (isLegacyOrder) {
// Legacy positioning: arguments in second or handler passed in third place
return [undefined, arg1 as HookArguments<T>, arg2 as HookDefaultHandler<T>];
} else {
// Default positioning: visit passed in as first argument
return [arg1, arg2 as HookArguments<T>, arg3];
}
}
/**
* Execute the handlers for a hook, in order, as `Promise`s that will be `await`ed.
* @param registrations The registrations (handler + options) to execute
* @param args Arguments to pass to the handler
*/
// Overload: running HookDefaultHandler: expect HookDefaultHandler return type
protected async run<T extends HookName>(registrations: HookRegistration<T, HookDefaultHandler<T>>[], visit: Visit | undefined, args: HookArguments<T>, rethrow: true): Promise<Awaited<ReturnType<HookDefaultHandler<T>>>[]>; // prettier-ignore
// Overload: running user handler: expect no specific type
protected async run<T extends HookName>(registrations: HookRegistration<T>[], visit: Visit | undefined, args: HookArguments<T>): Promise<unknown[]>; // prettier-ignore
// Implementation
protected async run<T extends HookName, R extends HookRegistration<T>[]>(
registrations: R,
visit: Visit | undefined = this.swup.visit,
args: HookArguments<T>,
rethrow: boolean = false
): Promise<Awaited<ReturnType<HookDefaultHandler<T>>> | unknown[]> {
const results = [];
for (const { hook, handler, defaultHandler, once } of registrations) {
if (visit?.done) continue;
if (once) this.off(hook, handler);
try {
const result = await runAsPromise(handler, [visit, args, defaultHandler]);
results.push(result);
} catch (error) {
if (rethrow) {
throw error;
} else {
console.error(`Error in hook '${hook}':`, error);
}
}
}
return results;
}
/**
* Execute the handlers for a hook, in order, without `await`ing any returned `Promise`s.
* @param registrations The registrations (handler + options) to execute
* @param args Arguments to pass to the handler
*/
// Overload: running HookDefaultHandler: expect HookDefaultHandler return type
protected runSync<T extends HookName>(registrations: HookRegistration<T, HookDefaultHandler<T>>[], visit: Visit | undefined, args: HookArguments<T>, rethrow: true): ReturnType<HookDefaultHandler<T>>[]; // prettier-ignore
// Overload: running user handler: expect no specific type
protected runSync<T extends HookName>(registrations: HookRegistration<T>[], visit: Visit | undefined, args: HookArguments<T>): unknown[]; // prettier-ignore
// Implementation
protected runSync<T extends HookName, R extends HookRegistration<T>[]>(
registrations: R,
visit: Visit | undefined = this.swup.visit,
args: HookArguments<T>,
rethrow: boolean = false
): (ReturnType<HookDefaultHandler<T>> | unknown)[] {
const results = [];
for (const { hook, handler, defaultHandler, once } of registrations) {
if (visit?.done) continue;
if (once) this.off(hook, handler);
try {
const result = (handler as HookDefaultHandler<T>)(visit, args, defaultHandler);
results.push(result);
if (isPromise(result)) {
console.warn(
`Swup will not await Promises in handler for synchronous hook '${hook}'.`
);
}
} catch (error) {
if (rethrow) {
throw error;
} else {
console.error(`Error in hook '${hook}':`, error);
}
}
}
return results;
}
/**
* Get all registered handlers for a hook, sorted by priority and registration order.
* @param hook Name of the hook
* @param defaultHandler The optional default handler of this hook
* @returns An object with the handlers sorted into `before` and `after` arrays,
* as well as a flag indicating if the original handler was replaced
*/
protected getHandlers<T extends HookName>(hook: T, defaultHandler?: HookDefaultHandler<T>) {
const ledger = this.get(hook);
if (!ledger) {
return { found: false, before: [], handler: [], after: [], replaced: false };
}
const registrations = Array.from(ledger.values());
// Let TypeScript know that replaced handlers are default handlers by filtering to true
const def = (T: HookRegistration<T>): T is HookRegistration<T, HookDefaultHandler<T>> => true; // prettier-ignore
const sort = this.sortRegistrations;
// Filter into before, after, and replace handlers
const before = registrations.filter(({ before, replace }) => before && !replace).sort(sort);
const replace = registrations.filter(({ replace }) => replace).filter(def).sort(sort); // prettier-ignore
const after = registrations.filter(({ before, replace }) => !before && !replace).sort(sort);
const replaced = replace.length > 0;
// Define main handler registration
// Created as HookRegistration[] array to allow passing it into hooks.run() directly
let handler: HookRegistration<T, HookDefaultHandler<T>>[] = [];
if (defaultHandler) {
handler = [{ id: 0, hook, handler: defaultHandler }];
if (replaced) {
const index = replace.length - 1;
const { handler: replacingHandler, once } = replace[index];
const createDefaultHandler = (index: number): HookDefaultHandler<T> | undefined => {
const next = replace[index - 1];
if (next) {
return (visit, args) =>
next.handler(visit, args, createDefaultHandler(index - 1));
} else {
return defaultHandler;
}
};
const nestedDefaultHandler = createDefaultHandler(index);
handler = [{ id: 0, hook, once, handler: replacingHandler, defaultHandler: nestedDefaultHandler }]; // prettier-ignore
}
}
return { found: true, before, handler, after, replaced };
}
/**
* Sort two hook registrations by priority and registration order.
* @param a The registration object to compare
* @param b The other registration object to compare with
* @returns The sort direction
*/
protected sortRegistrations<T extends HookName>(
a: HookRegistration<T>,
b: HookRegistration<T>
): number {
const priority = (a.priority ?? 0) - (b.priority ?? 0);
const id = a.id - b.id;
return priority || id || 0;
}
/**
* Dispatch a custom event on the `document` for a hook. Prefixed with `swup:`
* @param hook Name of the hook.
*/
protected dispatchDomEvent<T extends HookName>(
hook: T,
visit: Visit | undefined,
args?: HookArguments<T>
): void {
if (visit?.done) return;
const detail: HookEventDetail = { hook, args, visit: visit || this.swup.visit };
document.dispatchEvent(
new CustomEvent<HookEventDetail>(`swup:any`, { detail, bubbles: true })
);
document.dispatchEvent(
new CustomEvent<HookEventDetail>(`swup:${hook}`, { detail, bubbles: true })
);
}
/**
* Parse a hook name into the name and any modifiers.
* @param hook Name of the hook.
*/
parseName(hook: HookName | HookNameWithModifier): [HookName, Partial<HookOptions>] {
const [name, ...modifiers] = hook.split('.');
const options = modifiers.reduce((acc, mod) => ({ ...acc, [mod]: true }), {});
return [name as HookName, options];
}
}
|