Skip to content

Commit

Permalink
Move deferred utils to their own file (shipshapecode#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieTheWagner authored Sep 20, 2019
1 parent cd85b7c commit ba7cee2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {

// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: [
'src/js/*.js'
'src/js/**/*.js'
],

// The directory where Jest should output its coverage files
Expand Down
3 changes: 2 additions & 1 deletion src/js/abutment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import TetherBase from './utils';
import { updateClasses } from './utils/classes';
import { defer } from './utils/deferred';

const { getBounds, defer } = TetherBase.Utils;
const { getBounds } = TetherBase.Utils;

TetherBase.modules.push({
position({ top, left }) {
Expand Down
4 changes: 2 additions & 2 deletions src/js/constraint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import TetherBase from './utils';
import { updateClasses } from './utils/classes';
import { defer } from './utils/deferred';

const {
getBounds,
extend,
defer
extend
} = TetherBase.Utils;

const BOUNDS_FORMAT = ['left', 'top', 'right', 'bottom'];
Expand Down
3 changes: 1 addition & 2 deletions src/js/tether.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../css/tether-theme-basic.scss';
import { Evented } from './evented';
import TetherBase from './utils';
import { addClass, removeClass, updateClasses } from './utils/classes';
import { defer, flush } from './utils/deferred';
import './constraint';
import './abutment';
import './shift';
Expand All @@ -16,8 +17,6 @@ const {
getBounds,
getOffsetParent,
extend,
defer,
flush,
getScrollBarSize,
removeUtilElements
} = TetherBase.Utils;
Expand Down
32 changes: 2 additions & 30 deletions src/js/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { defer } from './utils/deferred';

let TetherBase;
if (typeof TetherBase === 'undefined') {
TetherBase = { modules: [] };
Expand Down Expand Up @@ -221,41 +223,11 @@ function extend(out = {}) {
return out;
}

function getClassName(el) {
// Can't use just SVGAnimatedString here since nodes within a Frame in IE have
// completely separately SVGAnimatedString base classes
if (el.className instanceof el.ownerDocument.defaultView.SVGAnimatedString) {
return el.className.baseVal;
}
return el.className;
}

function setClassName(el, className) {
el.setAttribute('class', className);
}

const deferred = [];

const defer = (fn) => {
deferred.push(fn);
};

const flush = () => {
let fn;
// eslint-disable-next-line
while (fn = deferred.pop()) {
fn();
}
};

TetherBase.Utils = {
getActualBoundingClientRect,
getScrollParents,
getBounds,
getOffsetParent,
extend,
defer,
flush,
uniqueId,
getScrollBarSize,
removeUtilElements
Expand Down
13 changes: 13 additions & 0 deletions src/js/utils/deferred.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const deferred = [];

export function defer(fn) {
deferred.push(fn);
}

export function flush() {
let fn;
// eslint-disable-next-line
while (fn = deferred.pop()) {
fn();
}
}
18 changes: 18 additions & 0 deletions test/unit/deferred.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defer, flush } from '../../src/js/utils/deferred';
import { stub } from 'sinon';

describe('deferred', () => {
describe('defer/flush', () => {
it('calls deferred functions when flush is called', () => {
const stub1 = stub();
const stub2 = stub();
defer(stub1);
defer(stub2);
expect(stub1.called).toBe(false);
expect(stub2.called).toBe(false);
flush();
expect(stub1.called).toBe(true);
expect(stub2.called).toBe(true);
});
});
});

0 comments on commit ba7cee2

Please sign in to comment.