Skip to content

Commit 51ff911

Browse files
committed
Remove props value trackers
1 parent cf1c5d2 commit 51ff911

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
@@ -2817,41 +2817,20 @@ class HistoryStrategy {
28172817
}
28182818
}
28192819

2820-
class Tracker {
2821-
constructor(mapping, initialValue, initiallyPresentInUrl) {
2822-
this.mapping = mapping;
2823-
this.initialValue = JSON.stringify(initialValue);
2824-
this.initiallyPresentInUrl = initiallyPresentInUrl;
2825-
}
2826-
hasReturnedToInitialValue(currentValue) {
2827-
return JSON.stringify(currentValue) === this.initialValue;
2828-
}
2829-
}
28302820
class QueryStringPlugin {
28312821
constructor(mapping) {
28322822
this.mapping = mapping;
2833-
this.trackers = new Map;
28342823
}
28352824
attachToComponent(component) {
2836-
component.on('connect', (component) => {
2837-
const urlUtils = new UrlUtils(window.location.href);
2838-
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2839-
const tracker = new Tracker(mapping, component.valueStore.get(prop), urlUtils.has(prop));
2840-
this.trackers.set(prop, tracker);
2841-
});
2842-
});
28432825
component.on('render:finished', (component) => {
28442826
const urlUtils = new UrlUtils(window.location.href);
2845-
this.trackers.forEach((tracker, prop) => {
2846-
const value = component.valueStore.get(prop);
2847-
if (!tracker.initiallyPresentInUrl && tracker.hasReturnedToInitialValue(value)) {
2848-
urlUtils.remove(tracker.mapping.name);
2849-
}
2850-
else {
2851-
urlUtils.set(tracker.mapping.name, value);
2852-
}
2827+
const currentUrl = urlUtils.toString();
2828+
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2829+
urlUtils.set(mapping.name, component.valueStore.get(prop));
28532830
});
2854-
HistoryStrategy.replace(urlUtils);
2831+
if (currentUrl !== urlUtils.toString()) {
2832+
HistoryStrategy.replace(urlUtils);
2833+
}
28552834
});
28562835
}
28572836
}

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)