Skip to content

Commit

Permalink
Use rAFPool npm pkg (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored Nov 27, 2018
1 parent c9b500f commit 8e50b37
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
37 changes: 14 additions & 23 deletions addon/services/-raf-admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Service from '@ember/service';
import RafPool from 'raf-pool';

/**
* ensure use on requestAnimationFrame, no matter how many components
Expand All @@ -7,36 +8,26 @@ import Service from '@ember/service';
* @class RAFAdmin
*/
export default class RAFAdmin extends Service {
init(...args) {
super.init(...args);
this.pool = [];
this.flush();
/** @private **/
init() {
this._super(...arguments);
this._rafPool = new RafPool();
}

flush() {
window.requestAnimationFrame(() => {
// assign to a variable to avoid ensure no race conditions happen
// b/w flushing the pool and interating through the pool
let pool = this.pool;
this.reset();
pool.forEach((item) => {
item[Object.keys(item)[0]]();
});

this.flush();
});
add(...args) {
return this._rafPool.add(...args);
}

add(elementId, fn) {
this.pool.push({ [elementId]: fn });
return fn;
flush() {
return this._rafPool.flush();
}

remove(elementId) {
this.pool = this.pool.filter(obj => !obj[elementId]);
remove(...args) {
return this._rafPool.remove(...args);
}

reset() {
this.pool = [];
reset(...args) {
this._rafPool.reset(...args);
this._rafPool = null;
}
}
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"dependencies": {
"ember-auto-import": "^1.2.15",
"ember-cli-babel": "^6.16.0",
"intersection-observer-admin": "0.0.2"
"intersection-observer-admin": "0.0.2",
"raf-pool": "0.0.1"
},
"devDependencies": {
"@ember/optional-features": "^0.7.0",
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/mixins/in-viewport-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ import EmberObject from '@ember/object';
import InViewportMixin from 'ember-in-viewport/mixins/in-viewport';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import rAFPoolManager from 'ember-in-viewport/services/-raf-admin';

class rAFMock extends rAFPoolManager {
flush() {}
}
import RafPool from 'raf-pool';

module('Unit | Mixin | in viewport', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
this.rAFPoolManager = new rAFMock();
this.rAFPoolManager = new RafPool();
});

test('mixin works', function(assert) {
Expand All @@ -22,29 +18,33 @@ module('Unit | Mixin | in viewport', function(hooks) {
});

test('can add to pool manager', function(assert) {
let obj = this.rAFPoolManager.add(123, 'wat');
assert.equal(obj, 'wat');
let cb = () => {};
let obj = this.rAFPoolManager.add(123, cb);
assert.equal(obj, cb);
});

test('can remove from pool manager', function(assert) {
this.rAFPoolManager.add(123, 'wat');
let cb = () => {};
this.rAFPoolManager.add(123, cb);
assert.equal(this.rAFPoolManager.pool.length, 1);
this.rAFPoolManager.remove(123);
assert.equal(this.rAFPoolManager.pool.length, 0);
});

test('can remove from pool manager', function(assert) {
this.rAFPoolManager.add(123, 'wat');
this.rAFPoolManager.add(124, 'foo');
let cb = () => {};
this.rAFPoolManager.add(123, cb);
this.rAFPoolManager.add(124, cb);
assert.equal(this.rAFPoolManager.pool.length, 2);
this.rAFPoolManager.remove(123);
assert.equal(this.rAFPoolManager.pool.length, 1);
assert.equal(this.rAFPoolManager.pool[0]['124'], 'foo');
assert.equal(this.rAFPoolManager.pool[0]['124'], cb);
});

test('reset resets pool manager', function(assert) {
this.rAFPoolManager.add(123, 'wat');
this.rAFPoolManager.add(124, 'foo');
let cb = () => {};
this.rAFPoolManager.add(123, cb);
this.rAFPoolManager.add(124, cb);
assert.equal(this.rAFPoolManager.pool.length, 2);
this.rAFPoolManager.reset();
assert.equal(this.rAFPoolManager.pool.length, 0);
Expand Down

0 comments on commit 8e50b37

Please sign in to comment.