Skip to content

Commit 5acd716

Browse files
committed
fixed memory leaks
1 parent 8788b5e commit 5acd716

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

src/component/Component.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ export default class Component {
6969
*/
7070
this.rerendered = false;
7171

72-
/**
73-
* The argument Map for rerendering on state changes
74-
*/
75-
this.argsMap = new Map();
76-
7772
/**
7873
* Event Emitter for the component
7974
*/
@@ -112,6 +107,7 @@ export default class Component {
112107
*/
113108
this.Reconciler = DOMReconciler;
114109
container.resolve("repository").addComponent(this);
110+
container.resolve("repository").addComponentArgs(this.id, []);
115111
}
116112

117113
/**
@@ -275,10 +271,10 @@ export default class Component {
275271

276272
releaseMemory() {
277273
container.resolve("repository").removeComponent(this.id);
274+
container.resolve("repository").removeComponentArgs(this.id);
278275
this.cleanUp();
279276

280277
this.emitter.clear();
281-
this.argsMap.clear();
282278

283279
for (let id in this.states) {
284280
this.states[id]?.off(`component-${this.id}`);
@@ -411,7 +407,8 @@ export default class Component {
411407
) ?? [];
412408

413409
current.forEach((e) => {
414-
let arg = this.argsMap.get(Number(e.getAttribute("uid")));
410+
let arg =
411+
container.resolve("repository").getComponentArgs(this.id) ?? [];
415412

416413
let attr = {};
417414

@@ -445,16 +442,14 @@ export default class Component {
445442
parent &&
446443
(this.getValue(resetParent) || this.getValue(replaceParent))
447444
) {
448-
if (!this.markup().length) this.argsMap.clear();
449-
else {
450-
let all = this.markup(parent);
451-
all.forEach((elem) => this.argsMap.delete(elem.getAttribute("uid")));
452-
}
445+
container.resolve("repository").addComponentArgs(this.id, []);
453446
}
454447

455448
let uuid = this.id;
456449

457-
if(states?.length) this.argsMap.set(uuid, args ?? []);
450+
if (states?.length) {
451+
container.resolve("repository").addComponentArgs(this.id, args ?? []);
452+
}
458453

459454
let attr = {
460455
uid: uuid,

src/core/Repository.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export default class Repository {
1616
*/
1717
this.components = new Map();
1818

19+
/**
20+
* Keeps arguments that was passed to the component
21+
* during rendering when a state was passed.
22+
*/
23+
this.componentArgsMap = new WeakMap();
24+
1925
/**
2026
* Map of State references.
2127
* @type {Map<string|number, State>}
@@ -111,11 +117,43 @@ export default class Repository {
111117
return this.mediators.get(Number(id));
112118
}
113119

120+
/**
121+
* Add arguments to the component
122+
* @param {string|number} componentId
123+
* @param {Array<*>} args
124+
*/
125+
addComponentArgs(componentId, args) {
126+
let component = this.findComponent(componentId);
127+
if (!component) return;
128+
this.componentArgsMap.set(component, args);
129+
}
130+
114131
/**
115132
* Remove a mediator from the repository
116133
* @param {string} id
117134
*/
118135
removeMediator(id) {
119136
this.mediators.delete(Number(id));
120137
}
138+
139+
/**
140+
* Get the arguments passed to the component
141+
* @param {string|number} componentId
142+
* @returns {Array<*>}
143+
*/
144+
getComponentArgs(componentId) {
145+
let component = this.findComponent(componentId);
146+
if (!component) return;
147+
return this.componentArgsMap.get(component);
148+
}
149+
150+
/**
151+
* Remove arguments from the component
152+
* @param {string|number} componentId
153+
*/
154+
removeComponentArgs(componentId) {
155+
let component = this.findComponent(componentId);
156+
if (!component) return;
157+
this.componentArgsMap.delete(component);
158+
}
121159
}

0 commit comments

Comments
 (0)