Debian Codegraph @

plugin/src/index.ts

raw
1 import type Swup from 'swup';
2 import type { Plugin, HookName, HookOptions, HookUnregister, Handler } from 'swup';
3 import { checkDependencyVersion } from './pluginRequirements.js';
4
5 function isBound(func: Function) {
6 return func.name.startsWith('bound ') && !func.hasOwnProperty('prototype');
7 }
8
9 export default abstract class SwupPlugin implements Plugin {
10 /** Name of the plugin */
11 abstract name: string;
12
13 /** Identify as a swup plugin */
14 isSwupPlugin: true = true;
15
16 // Swup instance, assigned by swup itself
17 swup!: Swup;
18
19 /** Version of this plugin. Currently not in use, defined here for backward compatiblity. */
20 version?: string;
21
22 /** Version requirements of this plugin. Example: `{ swup: '>=4' }` */
23 requires?: Record<string, string | string[]> = {};
24
25 // List of hook handlers to unregister on unmount
26 private handlersToUnregister: HookUnregister[] = [];
27
28 /** Run on mount */
29 mount() {
30 // this is mount method rewritten by class extending
31 // and is executed when swup is enabled with plugin
32 }
33
34 /** Run on unmount */
35 unmount() {
36 // this is unmount method rewritten by class extending
37 // and is executed when swup with plugin is disabled
38
39 // Unsubscribe all registered hook handlers
40 this.handlersToUnregister.forEach((unregister) => unregister());
41 this.handlersToUnregister = [];
42 }
43
44 _beforeMount(): void {
45 if (!this.name) {
46 throw new Error('You must define a name of plugin when creating a class.');
47 }
48 }
49
50 _afterUnmount(): void {
51 // here for any future hidden auto-cleanup
52 }
53
54 _checkRequirements(): boolean {
55 if (typeof this.requires !== 'object') {
56 return true;
57 }
58
59 Object.entries(this.requires).forEach(([dependency, versions]) => {
60 versions = Array.isArray(versions) ? versions : [versions];
61 if (!checkDependencyVersion(dependency, versions, this.swup)) {
62 const requirement = `${dependency} ${versions.join(', ')}`;
63 throw new Error(`Plugin version mismatch: ${this.name} requires ${requirement}`);
64 }
65 });
66
67 return true;
68 }
69
70 /**
71 * Register a new hook handler.
72 *
73 * On plugin unmount, the handler will automatically be unregistered.
74 * The handler function is lexically bound to the plugin instance for convenience.
75 * @see swup.hooks.on
76 */
77 protected on<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {
78 handler = !isBound(handler) ? handler.bind(this) : handler;
79 const unregister = this.swup.hooks.on(hook, handler, options);
80 this.handlersToUnregister.push(unregister);
81 return unregister;
82 }
83
84 protected once<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {
85 return this.on(hook, handler, { ...options, once: true });
86 }
87
88 protected before<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {
89 return this.on(hook, handler, { ...options, before: true });
90 }
91
92 protected replace<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {
93 return this.on(hook, handler, { ...options, replace: true });
94 }
95
96 protected off<T extends HookName>(hook: T, handler?: Handler<T>): void {
97 return this.swup.hooks.off(hook, handler!);
98 }
99 }