swup 4.0.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Swup.cjs +1 -1
- package/dist/Swup.cjs.map +1 -1
- package/dist/Swup.modern.js +1 -1
- package/dist/Swup.modern.js.map +1 -1
- package/dist/Swup.module.js +1 -1
- package/dist/Swup.module.js.map +1 -1
- package/dist/Swup.umd.js +1 -1
- package/dist/Swup.umd.js.map +1 -1
- package/dist/types/Swup.d.ts +1 -1
- package/dist/types/helpers/delegateEvent.d.ts +2 -1
- package/dist/types/modules/Cache.d.ts +3 -3
- package/dist/types/modules/__test__/delegateEvent.d.ts +1 -0
- package/package.json +3 -3
- package/src/helpers/delegateEvent.ts +7 -8
- package/src/modules/Cache.ts +11 -5
- package/src/modules/__test__/cache.test.ts +17 -0
- package/src/modules/__test__/delegateEvent.ts +36 -0
package/src/modules/Cache.ts
CHANGED
|
@@ -25,7 +25,11 @@ export class Cache {
|
|
|
25
25
|
|
|
26
26
|
/** All cached pages. */
|
|
27
27
|
get all() {
|
|
28
|
-
|
|
28
|
+
const copy = new Map();
|
|
29
|
+
this.pages.forEach((page, key) => {
|
|
30
|
+
copy.set(key, { ...page });
|
|
31
|
+
});
|
|
32
|
+
return copy;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
/** Check if the given URL has been cached. */
|
|
@@ -33,9 +37,11 @@ export class Cache {
|
|
|
33
37
|
return this.pages.has(this.resolve(url));
|
|
34
38
|
}
|
|
35
39
|
|
|
36
|
-
/** Return the cached page object if
|
|
40
|
+
/** Return a shallow copy of the cached page object if available. */
|
|
37
41
|
get(url: string): CacheData | undefined {
|
|
38
|
-
|
|
42
|
+
const result = this.pages.get(this.resolve(url));
|
|
43
|
+
if (!result) return result;
|
|
44
|
+
return { ...result };
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
/** Create a cache record for the specified URL. */
|
|
@@ -47,9 +53,9 @@ export class Cache {
|
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
/** Update a cache record, overwriting or adding custom data. */
|
|
50
|
-
update(url: string,
|
|
56
|
+
update(url: string, payload: Record<string, any>) {
|
|
51
57
|
url = this.resolve(url);
|
|
52
|
-
page = { ...this.get(url), ...
|
|
58
|
+
const page = { ...this.get(url), ...payload, url } as CacheData;
|
|
53
59
|
this.pages.set(url, page);
|
|
54
60
|
}
|
|
55
61
|
|
|
@@ -116,6 +116,23 @@ describe('Cache', () => {
|
|
|
116
116
|
expect(cache.has(page2.url)).toBe(true);
|
|
117
117
|
expect(cache.has(page3.url)).toBe(false);
|
|
118
118
|
});
|
|
119
|
+
|
|
120
|
+
it('should return a copy from cache.get()', () => {
|
|
121
|
+
cache.set(page1.url, page1);
|
|
122
|
+
const page = cache.get(page1.url);
|
|
123
|
+
page!.html = 'new';
|
|
124
|
+
expect(cache.get(page1.url)?.html).toEqual(page1.html);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should return a new Map with shallow copies from cache.all', () => {
|
|
128
|
+
cache.set(page1.url, page1);
|
|
129
|
+
cache.set(page2.url, page2);
|
|
130
|
+
|
|
131
|
+
const all = cache.all;
|
|
132
|
+
all.get(page1.url)!.html = 'new';
|
|
133
|
+
|
|
134
|
+
expect(cache.get(page1.url)?.html).toEqual(page1.html);
|
|
135
|
+
});
|
|
119
136
|
});
|
|
120
137
|
|
|
121
138
|
describe('Types', () => {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DelegateEvent } from 'delegate-it';
|
|
2
|
+
import { describe, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { delegateEvent } from '../../helpers/delegateEvent.js';
|
|
5
|
+
|
|
6
|
+
describe('delegateEvent', () => {
|
|
7
|
+
it('should return correct types', () => {
|
|
8
|
+
delegateEvent('form', 'submit', (event) => {});
|
|
9
|
+
|
|
10
|
+
// @ts-expect-no-error
|
|
11
|
+
delegateEvent('form', 'submit', (event: SubmitEvent) => {});
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
delegateEvent('form', 'submit', (event: MouseEvent) => {});
|
|
14
|
+
|
|
15
|
+
// @ts-expect-no-error
|
|
16
|
+
delegateEvent('form', 'submit', (event: DelegateEvent<SubmitEvent>) => {});
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
delegateEvent('form', 'submit', (event: DelegateEvent<MouseEvent>) => {});
|
|
19
|
+
|
|
20
|
+
// @ts-expect-no-error
|
|
21
|
+
delegateEvent('form', 'submit', (event: DelegateEvent<SubmitEvent, HTMLFormElement>) => {});
|
|
22
|
+
delegateEvent(
|
|
23
|
+
'form',
|
|
24
|
+
'submit',
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
(event: DelegateEvent<MouseEvent, HTMLAnchorElement>) => {}
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
delegateEvent('form', 'submit', (event) => {
|
|
30
|
+
// @ts-expect-no-error
|
|
31
|
+
const el: HTMLFormElement = event.delegateTarget;
|
|
32
|
+
});
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
delegateEvent('form', 'submit', (event: MouseEvent) => {});
|
|
35
|
+
});
|
|
36
|
+
});
|