Skip to content

Commit

Permalink
fix: memory leak when unbind
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Aug 30, 2023
1 parent d24d43b commit 38a1624
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
29 changes: 29 additions & 0 deletions __tests__/issue-17.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { clear, bind } from '../src';
import { Sensors } from '../src/sensorPool';

describe('#17', () => {
// 创建 div
const div = document.createElement('div');
document.body.appendChild(div);
div.innerHTML = `<div id="wrapper"></div>`;

const wrapper = document.getElementById('wrapper');

test('memory leak', () => {
const unbind1 = bind(wrapper, () => {});
const unbind2 = bind(wrapper, () => {});

const id = wrapper.getAttribute('size-sensor-id');

expect(Object.keys(Sensors).length).toBe(1);
expect(Sensors[id]).not.toBeUndefined();

unbind1();
expect(Object.keys(Sensors).length).toBe(1);
expect(Sensors[id]).not.toBeNull();

unbind2();
expect(Object.keys(Sensors).length).toBe(0);
expect(Sensors[id]).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion __tests__/unbind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ describe('unbind', () => {
expect(wrapper.getAttribute('size-sensor-id')).not.toBeNull();

unbind();
expect(wrapper.getAttribute('size-sensor-id')).not.toBeNull();
expect(wrapper.getAttribute('size-sensor-id')).toBeNull();
});
});
2 changes: 1 addition & 1 deletion dist/size-sensor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "size-sensor",
"version": "1.0.1",
"version": "1.0.2",
"description": "DOM element size sensor which will callback when size changed.",
"main": "lib/index.js",
"types": "index.d.ts",
Expand Down
22 changes: 13 additions & 9 deletions src/sensorPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import { SizeSensorId } from './constant';
* all the sensor objects.
* sensor pool
*/
const Sensors = {};
export const Sensors = {};

/**
* When destroy the sensor, remove it from the pool
*/
function clean(sensorId) {
// exist, then remove from pool
if (sensorId && Sensors[sensorId]) {
delete Sensors[sensorId];
}
}

/**
* get one sensor
Expand All @@ -30,7 +40,7 @@ export const getSensor = element => {
const newId = id();
element.setAttribute(SizeSensorId, newId);

const sensor = createSensor(element);
const sensor = createSensor(element, () => clean(newId));
// add sensor into pool
Sensors[newId] = sensor;

Expand All @@ -43,14 +53,8 @@ export const getSensor = element => {
*/
export const removeSensor = sensor => {
const sensorId = sensor.element.getAttribute(SizeSensorId);

// remove attribute
sensor.element.removeAttribute(SizeSensorId);
// remove event, dom of the sensor used
sensor.destroy();

// exist, then remove from pool
if (sensorId && Sensors[sensorId]) {
delete Sensors[sensorId];
}
clean(sensorId);
};
6 changes: 4 additions & 2 deletions src/sensors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

import debounce from '../debounce';
import { SensorClassName, SensorTabIndex } from '../constant';
import { SensorClassName, SensorTabIndex, SizeSensorId } from '../constant';

export const createSensor = element => {
export const createSensor = (element, whenDestroy) => {
let sensor = undefined;
// callback
let listeners = [];
Expand Down Expand Up @@ -85,8 +85,10 @@ export const createSensor = element => {
// remove dom
sensor.parentNode.removeChild(sensor);
// initial variable
element.removeAttribute(SizeSensorId);
sensor = undefined;
listeners = [];
whenDestroy && whenDestroy();
}
};

Expand Down
5 changes: 4 additions & 1 deletion src/sensors/resizeObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* Contract: i@hust.cc
*/

import { SizeSensorId } from '../constant';
import debounce from '../debounce';

export const createSensor = element => {
export const createSensor = (element, whenDestroy) => {
let sensor = undefined;
// callback
let listeners = [];
Expand Down Expand Up @@ -56,6 +57,8 @@ export const createSensor = element => {

listeners = [];
sensor = undefined;
element.removeAttribute(SizeSensorId);
whenDestroy && whenDestroy();
};

/**
Expand Down

0 comments on commit 38a1624

Please sign in to comment.