Skip to content

Commit b4d43b5

Browse files
feat: Add utilities#assert [#95469256]
DOMRenderer previously had methods for asserting state. Putting those methods on the prototype makes it impossible for the minifier to do any kind of dead-code elimination. This commit adds an assert utility function used in order to check if a certain condition is met. Checking for NODE_ENV and not including the passed in error message in the thrown error should be trivial and only needs to be done once in the more generic assert function.
1 parent b518234 commit b4d43b5

5 files changed

Lines changed: 162 additions & 88 deletions

File tree

dom-renderers/DOMRenderer.js

Lines changed: 29 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var math = require('./Math');
2929
var PathUtils = require('../core/Path');
3030
var vendorPrefix = require('../utilities/vendorPrefix');
3131
var eventMap = require('./events/EventMap');
32+
var assert = require('../utilities/assert');
3233

3334
var TRANSFORM = null;
3435

@@ -89,7 +90,6 @@ function DOMRenderer (element, selector, compositor) {
8990
this._lastEv = null;
9091
}
9192

92-
9393
/**
9494
* Attaches an EventListener to the element associated with the passed in path.
9595
* Prevents the default browser action on all subsequent events if
@@ -106,7 +106,7 @@ function DOMRenderer (element, selector, compositor) {
106106
* @return {undefined} undefined
107107
*/
108108
DOMRenderer.prototype.subscribe = function subscribe(type) {
109-
this._assertTargetLoaded();
109+
assert(this._target, 'No target loaded');
110110
this._listen(type);
111111
this._target.subscribe[type] = true;
112112
};
@@ -120,7 +120,8 @@ DOMRenderer.prototype.subscribe = function subscribe(type) {
120120
* @return {undefined} undefined
121121
*/
122122
DOMRenderer.prototype.unsubscribe = function unsubscribe(type) {
123-
this._assertTargetLoaded();
123+
assert(this._target, 'No target loaded');
124+
124125
this._listen(type);
125126
this._target.subscribe[type] = false;
126127
};
@@ -135,7 +136,8 @@ DOMRenderer.prototype.unsubscribe = function unsubscribe(type) {
135136
* @return {undefined} undefined
136137
*/
137138
DOMRenderer.prototype.preventDefault = function preventDefault(type) {
138-
this._assertTargetLoaded();
139+
assert(this._target, 'No target loaded');
140+
139141
this._listen(type);
140142
this._target.preventDefault[type] = true;
141143
};
@@ -152,7 +154,8 @@ DOMRenderer.prototype.preventDefault = function preventDefault(type) {
152154
* @return {undefined} undefined
153155
*/
154156
DOMRenderer.prototype.allowDefault = function allowDefault(type) {
155-
this._assertTargetLoaded();
157+
assert(this._target, 'No target loaded');
158+
156159
this._listen(type);
157160
this._target.preventDefault[type] = false;
158161
};
@@ -173,7 +176,7 @@ DOMRenderer.prototype.allowDefault = function allowDefault(type) {
173176
* @return {undefined} undefined
174177
*/
175178
DOMRenderer.prototype._listen = function _listen(type) {
176-
this._assertTargetLoaded();
179+
assert(this._target, 'No target loaded');
177180

178181
if (
179182
!this._target.listeners[type] && !this._root.listeners[type]
@@ -248,7 +251,6 @@ DOMRenderer.prototype._triggerEvent = function _triggerEvent(ev) {
248251
}
249252
};
250253

251-
252254
/**
253255
* getSizeOf gets the dom size of a particular DOM element. This is
254256
* needed for render sizing in the scene graph.
@@ -320,55 +322,6 @@ DOMRenderer.prototype.draw = function draw(renderState) {
320322
}
321323
};
322324

323-
324-
/**
325-
* Internal helper function used for ensuring that a path is currently loaded.
326-
*
327-
* @method
328-
* @private
329-
*
330-
* @return {undefined} undefined
331-
*/
332-
DOMRenderer.prototype._assertPathLoaded = function _asserPathLoaded() {
333-
if (!this._path) throw new Error('path not loaded');
334-
};
335-
336-
/**
337-
* Internal helper function used for ensuring that a parent is currently loaded.
338-
*
339-
* @method
340-
* @private
341-
*
342-
* @return {undefined} undefined
343-
*/
344-
DOMRenderer.prototype._assertParentLoaded = function _assertParentLoaded() {
345-
if (!this._parent) throw new Error('parent not loaded');
346-
};
347-
348-
/**
349-
* Internal helper function used for ensuring that children are currently
350-
* loaded.
351-
*
352-
* @method
353-
* @private
354-
*
355-
* @return {undefined} undefined
356-
*/
357-
DOMRenderer.prototype._assertChildrenLoaded = function _assertChildrenLoaded() {
358-
if (!this._children) throw new Error('children not loaded');
359-
};
360-
361-
/**
362-
* Internal helper function used for ensuring that a target is currently loaded.
363-
*
364-
* @method _assertTargetLoaded
365-
*
366-
* @return {undefined} undefined
367-
*/
368-
DOMRenderer.prototype._assertTargetLoaded = function _assertTargetLoaded() {
369-
if (!this._target) throw new Error('No target loaded');
370-
};
371-
372325
/**
373326
* Finds and sets the parent of the currently loaded element (path).
374327
*
@@ -378,7 +331,7 @@ DOMRenderer.prototype._assertTargetLoaded = function _assertTargetLoaded() {
378331
* @return {ElementCache} Parent element.
379332
*/
380333
DOMRenderer.prototype.findParent = function findParent () {
381-
this._assertPathLoaded();
334+
assert(this._path, 'No path loaded');
382335

383336
var path = this._path;
384337
var parent;
@@ -403,7 +356,6 @@ DOMRenderer.prototype.findTarget = function findTarget() {
403356
return this._target;
404357
};
405358

406-
407359
/**
408360
* Loads the passed in path.
409361
*
@@ -465,7 +417,7 @@ DOMRenderer.prototype.insertEl = function insertEl (tagName) {
465417

466418
this.findParent();
467419

468-
this._assertParentLoaded();
420+
assert(this._parent, 'No parent loaded');
469421

470422
if (this._parent.void)
471423
throw new Error(
@@ -487,7 +439,6 @@ DOMRenderer.prototype.insertEl = function insertEl (tagName) {
487439
}
488440
};
489441

490-
491442
/**
492443
* Sets a property on the currently loaded target.
493444
*
@@ -499,16 +450,15 @@ DOMRenderer.prototype.insertEl = function insertEl (tagName) {
499450
* @return {undefined} undefined
500451
*/
501452
DOMRenderer.prototype.setProperty = function setProperty (name, value) {
502-
this._assertTargetLoaded();
453+
assert(this._target, 'No target loaded');
503454
this._target.element.style[name] = value;
504455
};
505456

506-
507457
/**
508458
* Sets the size of the currently loaded target.
509459
* Removes any explicit sizing constraints when passed in `false`
510460
* ("true-sizing").
511-
*
461+
*
512462
* Invoking setSize is equivalent to a manual invocation of `setWidth` followed
513463
* by `setHeight`.
514464
*
@@ -520,27 +470,27 @@ DOMRenderer.prototype.setProperty = function setProperty (name, value) {
520470
* @return {undefined} undefined
521471
*/
522472
DOMRenderer.prototype.setSize = function setSize (width, height) {
523-
this._assertTargetLoaded();
473+
assert(this._target, 'No target loaded');
524474

525475
this.setWidth(width);
526476
this.setHeight(height);
527477
};
528478

529479
/**
530480
* Sets the width of the currently loaded ElementCache.
531-
*
481+
*
532482
* @method
533-
*
483+
*
534484
* @param {Number|false} width The explicit width to be set on the
535485
* ElementCache's target (and content) element.
536486
* `false` removes any explicit sizing
537487
* constraints from the underlying DOM
538488
* Elements.
539489
*
540490
* @return {undefined} undefined
541-
*/
491+
*/
542492
DOMRenderer.prototype.setWidth = function setWidth(width) {
543-
this._assertTargetLoaded();
493+
assert(this._target, 'No target loaded');
544494

545495
var contentWrapper = this._target.content;
546496

@@ -561,19 +511,19 @@ DOMRenderer.prototype.setWidth = function setWidth(width) {
561511

562512
/**
563513
* Sets the height of the currently loaded ElementCache.
564-
*
514+
*
565515
* @method setHeight
566-
*
516+
*
567517
* @param {Number|false} height The explicit height to be set on the
568518
* ElementCache's target (and content) element.
569519
* `false` removes any explicit sizing
570520
* constraints from the underlying DOM
571521
* Elements.
572522
*
573523
* @return {undefined} undefined
574-
*/
524+
*/
575525
DOMRenderer.prototype.setHeight = function setHeight(height) {
576-
this._assertTargetLoaded();
526+
assert(this._target, 'No target loaded');
577527

578528
var contentWrapper = this._target.content;
579529

@@ -603,7 +553,7 @@ DOMRenderer.prototype.setHeight = function setHeight(height) {
603553
* @return {undefined} undefined
604554
*/
605555
DOMRenderer.prototype.setAttribute = function setAttribute(name, value) {
606-
this._assertTargetLoaded();
556+
assert(this._target, 'No target loaded');
607557
this._target.element.setAttribute(name, value);
608558
};
609559

@@ -617,7 +567,7 @@ DOMRenderer.prototype.setAttribute = function setAttribute(name, value) {
617567
* @return {undefined} undefined
618568
*/
619569
DOMRenderer.prototype.setContent = function setContent(content) {
620-
this._assertTargetLoaded();
570+
assert(this._target, 'No target loaded');
621571

622572
if (this._target.formElement) {
623573
this._target.element.value = content;
@@ -641,7 +591,6 @@ DOMRenderer.prototype.setContent = function setContent(content) {
641591
);
642592
};
643593

644-
645594
/**
646595
* Sets the passed in transform matrix (world space). Inverts the parent's world
647596
* transform.
@@ -653,11 +602,10 @@ DOMRenderer.prototype.setContent = function setContent(content) {
653602
* @return {undefined} undefined
654603
*/
655604
DOMRenderer.prototype.setMatrix = function setMatrix (transform) {
656-
this._assertTargetLoaded();
605+
assert(this._target, 'No target loaded');
657606
this._target.element.style[TRANSFORM] = this._stringifyMatrix(transform);
658607
};
659608

660-
661609
/**
662610
* Adds a class to the classList associated with the currently loaded target.
663611
*
@@ -668,11 +616,10 @@ DOMRenderer.prototype.setMatrix = function setMatrix (transform) {
668616
* @return {undefined} undefined
669617
*/
670618
DOMRenderer.prototype.addClass = function addClass(domClass) {
671-
this._assertTargetLoaded();
619+
assert(this._target, 'No target loaded');
672620
this._target.element.classList.add(domClass);
673621
};
674622

675-
676623
/**
677624
* Removes a class from the classList associated with the currently loaded
678625
* target.
@@ -684,11 +631,10 @@ DOMRenderer.prototype.addClass = function addClass(domClass) {
684631
* @return {undefined} undefined
685632
*/
686633
DOMRenderer.prototype.removeClass = function removeClass(domClass) {
687-
this._assertTargetLoaded();
634+
assert(this._target, 'No target loaded');
688635
this._target.element.classList.remove(domClass);
689636
};
690637

691-
692638
/**
693639
* Stringifies the passed in matrix for setting the `transform` property.
694640
*
@@ -700,7 +646,7 @@ DOMRenderer.prototype.removeClass = function removeClass(domClass) {
700646
*/
701647
DOMRenderer.prototype._stringifyMatrix = function _stringifyMatrix(m) {
702648
var r = 'matrix3d(';
703-
649+
704650
r += (m[0] < 0.000001 && m[0] > -0.000001) ? '0,' : m[0] + ',';
705651
r += (m[1] < 0.000001 && m[1] > -0.000001) ? '0,' : m[1] + ',';
706652
r += (m[2] < 0.000001 && m[2] > -0.000001) ? '0,' : m[2] + ',';
@@ -716,7 +662,7 @@ DOMRenderer.prototype._stringifyMatrix = function _stringifyMatrix(m) {
716662
r += (m[12] < 0.000001 && m[12] > -0.000001) ? '0,' : m[12] + ',';
717663
r += (m[13] < 0.000001 && m[13] > -0.000001) ? '0,' : m[13] + ',';
718664
r += (m[14] < 0.000001 && m[14] > -0.000001) ? '0,' : m[14] + ',';
719-
665+
720666
r += m[15] + ')';
721667
return r;
722668
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@
4646
"tap-closer": "^1.0.0",
4747
"tape": "^4.0.0",
4848
"tap-spec": "^4.0.0",
49+
"escodegen": "^1.6.1",
50+
"esprima": "^2.2.0",
51+
"replace-method": "0.0.0",
52+
"through": "^2.3.7",
4953
"uglify-js": "^2.4.17"
5054
},
5155
"dependencies": {
5256
"glslify": "^2.0.0"
5357
},
5458
"browserify": {
5559
"transform": [
60+
["./scripts/optimize-assertions", { "stripErrors": false, "preCheck": true }],
5661
"glslify"
5762
]
5863
}

0 commit comments

Comments
 (0)