Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use single ResizeObserver for better performance #222

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions addon/modifiers/did-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,46 @@ export default class DidResizeModifier extends Modifier {
options = {};

// Private API
observer = null;
static observer = null;
static handlers = null;

observe() {
if (this.observer) {
this.observer.observe(this.element, this.options);
constructor() {
super(...arguments);

if (!('ResizeObserver' in window)) {
return;
}

if (!DidResizeModifier.observer) {
DidResizeModifier.handlers = new WeakMap();
DidResizeModifier.observer = new ResizeObserver((entries, observer) => {
for (let entry of entries) {
const handler = DidResizeModifier.handlers.get(entry.target);
elwayman02 marked this conversation as resolved.
Show resolved Hide resolved
if (handler) handler(entry, observer);
}
});
}
}

unobserve() {
if (this.observer) {
this.observer.unobserve();
addHandler() {
DidResizeModifier.handlers.set(this.element, this.handler);
}

removeHandler() {
DidResizeModifier.handlers.delete(this.element);
}

observe() {
if (DidResizeModifier.observer) {
this.addHandler();
DidResizeModifier.observer.observe(this.element, this.options);
}
}

disconnect() {
if (this.observer) {
this.observer.disconnect();
unobserve() {
if (DidResizeModifier.observer) {
DidResizeModifier.observer.unobserve(this.element);
elwayman02 marked this conversation as resolved.
Show resolved Hide resolved
this.removeHandler();
}
}

Expand All @@ -41,19 +64,7 @@ export default class DidResizeModifier extends Modifier {
this.observe();
}

didInstall() {
if (!('ResizeObserver' in window)) {
return;
}

this.observer = new ResizeObserver((entries, observer) => {
this.handler(entries[0], observer);
});

this.observe();
}

willRemove() {
this.disconnect();
this.unobserve();
}
}
63 changes: 31 additions & 32 deletions tests/integration/modifiers/did-resize-test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { find, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import sinon from 'sinon';

let resizeCallback;
let observeStub;
let unobserveStub;
let disconnectStub;
let MockResizeObserver;
import DidResizeModifier from 'ember-resize-modifier/modifiers/did-resize';

module('Integration | Modifier | did-resize', function (hooks) {
setupRenderingTest(hooks);

let resizeObserver;
let resizeCallback = null;
let observeStub = sinon.stub();
let unobserveStub = sinon.stub();
let disconnectStub = sinon.stub();
let resizeObserver = window.ResizeObserver;
let mockResizeObserver = class MockResizeObserver {
constructor(callback) {
resizeCallback = callback;
}

observe = observeStub;
unobserve = unobserveStub;
disconnect = disconnectStub;
};

hooks.beforeEach(function () {
resizeCallback = null;
observeStub = sinon.stub();
unobserveStub = sinon.stub();
disconnectStub = sinon.stub();

MockResizeObserver = class MockResizeObserver {
constructor(callback) {
resizeCallback = callback;
}

observe = observeStub;
unobserve = unobserveStub;
disconnect = disconnectStub;
};

resizeObserver = window.ResizeObserver;
window.ResizeObserver = MockResizeObserver;

observeStub.reset();
unobserveStub.reset();
disconnectStub.reset();
this.resizeStub = sinon.stub();
window.ResizeObserver = mockResizeObserver;

// reset static properties to make sure every test case runs independently
DidResizeModifier.observer = null;
DidResizeModifier.handlers = null;
resizeCallback = null;
});

hooks.afterEach(function () {
Expand All @@ -58,15 +57,16 @@ module('Integration | Modifier | did-resize', function (hooks) {
});

test('modifier triggers handler when ResizeObserver fires callback', async function (assert) {
await render(hbs`<div {{did-resize this.resizeStub}}></div>`);

let fakeEntry = { target: {} };
await render(
hbs`<div id="test-element" {{did-resize this.resizeStub}}></div>`
);
let entry = { target: find('#test-element') };
elwayman02 marked this conversation as resolved.
Show resolved Hide resolved
let fakeObserver = { observe: {} };

resizeCallback([fakeEntry], fakeObserver);
resizeCallback([entry], fakeObserver);

assert.ok(
this.resizeStub.calledOnceWith(fakeEntry, fakeObserver),
this.resizeStub.calledOnceWith(entry, fakeObserver),
'handler fired with correct parameters'
);
});
Expand All @@ -86,7 +86,6 @@ module('Integration | Modifier | did-resize', function (hooks) {
delete window.ResizeObserver;

await render(hbs`<div {{did-resize this.resizeStub}}></div>`);

assert.notOk(resizeCallback, 'no callback received');
elwayman02 marked this conversation as resolved.
Show resolved Hide resolved
assert.notOk(observeStub.calledOnce, 'observe was not called');
});
Expand Down