Skip to content

Commit 1809b26

Browse files
committed
Remove props value trackers
1 parent 5a10c37 commit 1809b26

File tree

3 files changed

+15
-66
lines changed

3 files changed

+15
-66
lines changed

src/LiveComponent/assets/dist/Component/plugins/QueryStringPlugin.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ interface QueryMapping {
55
}
66
export default class implements PluginInterface {
77
private readonly mapping;
8-
private trackers;
98
constructor(mapping: {
109
[p: string]: QueryMapping;
1110
});

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,41 +2787,20 @@ class HistoryStrategy {
27872787
}
27882788
}
27892789

2790-
class Tracker {
2791-
constructor(mapping, initialValue, initiallyPresentInUrl) {
2792-
this.mapping = mapping;
2793-
this.initialValue = JSON.stringify(initialValue);
2794-
this.initiallyPresentInUrl = initiallyPresentInUrl;
2795-
}
2796-
hasReturnedToInitialValue(currentValue) {
2797-
return JSON.stringify(currentValue) === this.initialValue;
2798-
}
2799-
}
28002790
class QueryStringPlugin {
28012791
constructor(mapping) {
28022792
this.mapping = mapping;
2803-
this.trackers = new Map;
28042793
}
28052794
attachToComponent(component) {
2806-
component.on('connect', (component) => {
2807-
const urlUtils = new UrlUtils(window.location.href);
2808-
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2809-
const tracker = new Tracker(mapping, component.valueStore.get(prop), urlUtils.has(prop));
2810-
this.trackers.set(prop, tracker);
2811-
});
2812-
});
28132795
component.on('render:finished', (component) => {
28142796
const urlUtils = new UrlUtils(window.location.href);
2815-
this.trackers.forEach((tracker, prop) => {
2816-
const value = component.valueStore.get(prop);
2817-
if (!tracker.initiallyPresentInUrl && tracker.hasReturnedToInitialValue(value)) {
2818-
urlUtils.remove(tracker.mapping.name);
2819-
}
2820-
else {
2821-
urlUtils.set(tracker.mapping.name, value);
2822-
}
2797+
const currentUrl = urlUtils.toString();
2798+
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2799+
urlUtils.set(mapping.name, component.valueStore.get(prop));
28232800
});
2824-
HistoryStrategy.replace(urlUtils);
2801+
if (currentUrl !== urlUtils.toString()) {
2802+
HistoryStrategy.replace(urlUtils);
2803+
}
28252804
});
28262805
}
28272806
}

src/LiveComponent/assets/src/Component/plugins/QueryStringPlugin.ts

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,22 @@ interface QueryMapping {
99
name: string,
1010
}
1111

12-
/**
13-
* Tracks initial state and prop query mapping.
14-
*/
15-
class Tracker {
16-
readonly mapping: QueryMapping;
17-
private readonly initialValue: any;
18-
readonly initiallyPresentInUrl: boolean;
19-
20-
constructor(mapping: QueryMapping, initialValue: any, initiallyPresentInUrl: boolean) {
21-
this.mapping = mapping;
22-
this.initialValue = JSON.stringify(initialValue);
23-
this.initiallyPresentInUrl = initiallyPresentInUrl;
24-
}
25-
hasReturnedToInitialValue(currentValue: any) {
26-
return JSON.stringify(currentValue) === this.initialValue;
27-
}
28-
}
29-
3012
export default class implements PluginInterface {
31-
private trackers = new Map<string,Tracker>;
32-
33-
constructor(private readonly mapping: {[p: string]: QueryMapping}) {
34-
}
13+
constructor(private readonly mapping: {[p: string]: QueryMapping}) {}
3514

3615
attachToComponent(component: Component): void {
37-
component.on('connect', (component: Component) => {
38-
const urlUtils = new UrlUtils(window.location.href);
39-
Object.entries(this.mapping).forEach(([prop, mapping]) => {
40-
const tracker = new Tracker(mapping, component.valueStore.get(prop), urlUtils.has(prop));
41-
this.trackers.set(prop, tracker);
42-
});
43-
});
44-
4516
component.on('render:finished', (component: Component) => {
4617
const urlUtils = new UrlUtils(window.location.href);
47-
this.trackers.forEach((tracker, prop) => {
48-
const value = component.valueStore.get(prop);
49-
if (!tracker.initiallyPresentInUrl && tracker.hasReturnedToInitialValue(value)) {
50-
urlUtils.remove(tracker.mapping.name);
51-
} else {
52-
urlUtils.set(tracker.mapping.name, value);
53-
}
18+
const currentUrl = urlUtils.toString();
19+
20+
Object.entries(this.mapping).forEach(([prop, mapping]) => {
21+
urlUtils.set(mapping.name, component.valueStore.get(prop));
5422
});
5523

56-
HistoryStrategy.replace(urlUtils);
24+
// Only update URL if it has changed
25+
if (currentUrl !== urlUtils.toString()) {
26+
HistoryStrategy.replace(urlUtils);
27+
}
5728
});
5829
}
5930
}

0 commit comments

Comments
 (0)