Skip to content

Commit ebd3677

Browse files
committed
Fixes memory leak.
1 parent c9a8674 commit ebd3677

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

src/__tests__/sizeMe.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('Given the SizeMe library', () => {
5858
beforeEach(() => {
5959
resizeDetectorMock = {
6060
// :: domEl -> void
61-
removeAllListeners: jest.fn(),
61+
uninstall: jest.fn(),
6262
// :: (domeEl, callback) -> void
6363
listenTo: jest.fn(),
6464
}
@@ -160,12 +160,12 @@ describe('Given the SizeMe library', () => {
160160
const mounted = mount(<SizeAwareComponent />)
161161

162162
expect(resizeDetectorMock.listenTo).toHaveBeenCalledTimes(1)
163-
expect(resizeDetectorMock.removeAllListeners).toHaveBeenCalledTimes(0)
163+
expect(resizeDetectorMock.uninstall).toHaveBeenCalledTimes(0)
164164

165165
mounted.unmount()
166166

167167
expect(resizeDetectorMock.listenTo).toHaveBeenCalledTimes(1)
168-
expect(resizeDetectorMock.removeAllListeners).toHaveBeenCalledTimes(1)
168+
expect(resizeDetectorMock.uninstall).toHaveBeenCalledTimes(1)
169169
})
170170
})
171171

@@ -212,7 +212,7 @@ describe('Given the SizeMe library', () => {
212212

213213
// An add listener should have been called for the placeholder.
214214
expect(resizeDetectorMock.listenTo).toHaveBeenCalledTimes(1)
215-
expect(resizeDetectorMock.removeAllListeners).toHaveBeenCalledTimes(0)
215+
expect(resizeDetectorMock.uninstall).toHaveBeenCalledTimes(0)
216216

217217
// Get the callback for size changes.
218218
const checkIfSizeChangedCallback =
@@ -229,14 +229,14 @@ describe('Given the SizeMe library', () => {
229229
// on the newly mounted component.
230230
expect(mounted.text()).toEqual(expected({ width: 100, height: 50 }))
231231
expect(resizeDetectorMock.listenTo).toHaveBeenCalledTimes(2)
232-
expect(resizeDetectorMock.removeAllListeners).toHaveBeenCalledTimes(1)
232+
expect(resizeDetectorMock.uninstall).toHaveBeenCalledTimes(1)
233233

234234
// umount
235235
mounted.unmount()
236236

237237
// The remove listener should have been called!
238238
expect(resizeDetectorMock.listenTo).toHaveBeenCalledTimes(2)
239-
expect(resizeDetectorMock.removeAllListeners).toHaveBeenCalledTimes(2)
239+
expect(resizeDetectorMock.uninstall).toHaveBeenCalledTimes(2)
240240
})
241241
})
242242

src/resizeDetector.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import createResizeDetector from 'element-resize-detector'
22

3-
let instance
3+
const instances = {}
44

55
// Lazily require to not cause bug
66
// https://github.com/ctrlplusb/react-sizeme/issues/6
77
function resizeDetector(strategy = 'scroll') {
8-
if (!instance) {
9-
instance = createResizeDetector({
8+
if (!instances[strategy]) {
9+
instances[strategy] = createResizeDetector({
1010
strategy,
1111
})
1212
}
1313

14-
return instance
14+
return instances[strategy]
1515
}
1616

1717
export default resizeDetector

src/sizeMe.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ function sizeMe(config = defaultConfig) {
168168

169169
const refreshDelayStrategy = refreshMode === 'throttle' ? throttle : debounce
170170

171+
const detector = resizeDetector(resizeDetectorStrategy)
172+
171173
return function WrapComponent(WrappedComponent) {
172174
const SizeMeRenderWrapper = renderWrapper(WrappedComponent)
173175

@@ -204,7 +206,7 @@ function sizeMe(config = defaultConfig) {
204206
this.checkIfSizeChanged = () => undefined
205207

206208
if (this.domEl) {
207-
resizeDetector(resizeDetectorStrategy).removeAllListeners(this.domEl)
209+
detector.uninstall(this.domEl)
208210
this.domEl = null
209211
}
210212
}
@@ -244,23 +246,18 @@ function sizeMe(config = defaultConfig) {
244246
if (!found) {
245247
// This is for special cases where the element may be null.
246248
if (this.domEl) {
247-
resizeDetector(resizeDetectorStrategy).removeAllListeners(
248-
this.domEl,
249-
)
249+
detector.uninstall(this.domEl)
250250
this.domEl = null
251251
}
252252
return
253253
}
254254

255255
if (this.domEl) {
256-
resizeDetector(resizeDetectorStrategy).removeAllListeners(this.domEl)
256+
detector.uninstall(this.domEl)
257257
}
258258

259259
this.domEl = found
260-
resizeDetector(resizeDetectorStrategy).listenTo(
261-
this.domEl,
262-
this.checkIfSizeChanged,
263-
)
260+
detector.listenTo(this.domEl, this.checkIfSizeChanged)
264261
}
265262

266263
refCallback = element => {

0 commit comments

Comments
 (0)