Skip to content

Commit a19552e

Browse files
committed
Remove props value trackers
1 parent f67e80c commit a19552e

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
@@ -2791,41 +2791,20 @@ class HistoryStrategy {
27912791
}
27922792
}
27932793

2794-
class Tracker {
2795-
constructor(mapping, initialValue, initiallyPresentInUrl) {
2796-
this.mapping = mapping;
2797-
this.initialValue = JSON.stringify(initialValue);
2798-
this.initiallyPresentInUrl = initiallyPresentInUrl;
2799-
}
2800-
hasReturnedToInitialValue(currentValue) {
2801-
return JSON.stringify(currentValue) === this.initialValue;
2802-
}
2803-
}
28042794
class QueryStringPlugin {
28052795
constructor(mapping) {
28062796
this.mapping = mapping;
2807-
this.trackers = new Map;
28082797
}
28092798
attachToComponent(component) {
2810-
component.on('connect', (component) => {
2811-
const urlUtils = new UrlUtils(window.location.href);
2812-
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2813-
const tracker = new Tracker(mapping, component.valueStore.get(prop), urlUtils.has(prop));
2814-
this.trackers.set(prop, tracker);
2815-
});
2816-
});
28172799
component.on('render:finished', (component) => {
28182800
const urlUtils = new UrlUtils(window.location.href);
2819-
this.trackers.forEach((tracker, prop) => {
2820-
const value = component.valueStore.get(prop);
2821-
if (!tracker.initiallyPresentInUrl && tracker.hasReturnedToInitialValue(value)) {
2822-
urlUtils.remove(tracker.mapping.name);
2823-
}
2824-
else {
2825-
urlUtils.set(tracker.mapping.name, value);
2826-
}
2801+
const currentUrl = urlUtils.toString();
2802+
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2803+
urlUtils.set(mapping.name, component.valueStore.get(prop));
28272804
});
2828-
HistoryStrategy.replace(urlUtils);
2805+
if (currentUrl !== urlUtils.toString()) {
2806+
HistoryStrategy.replace(urlUtils);
2807+
}
28292808
});
28302809
}
28312810
}

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)