Skip to content

Commit

Permalink
Force async (#3996)
Browse files Browse the repository at this point in the history
* removed forced async
* tests
* fixed lint
  • Loading branch information
asturur committed Jun 11, 2017
1 parent 69f37b0 commit 145bd3b
Show file tree
Hide file tree
Showing 29 changed files with 453 additions and 513 deletions.
13 changes: 1 addition & 12 deletions src/elements_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,7 @@ fabric.ElementsParser.prototype.createObject = function(el, index) {
};

fabric.ElementsParser.prototype._createObject = function(klass, el, index) {
if (klass.async) {
klass.fromElement(el, this.createCallback(index, el), this.options);
}
else {
var obj = klass.fromElement(el, this.options);
this.resolveGradient(obj, 'fill');
this.resolveGradient(obj, 'stroke');
obj._removeTransformMatrix();
this.reviver && this.reviver(el, obj);
this.instances[index] = obj;
this.checkIfDone();
}
klass.fromElement(el, this.createCallback(index, el), this.options);
};

fabric.ElementsParser.prototype.createCallback = function(index, el) {
Expand Down
11 changes: 5 additions & 6 deletions src/shapes/circle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@
* @memberOf fabric.Circle
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @param {Function} [callback] Options callback invoked after parsing is finished
* @throws {Error} If value of `r` attribute is missing or invalid
* @return {fabric.Circle} Instance of fabric.Circle
*/
fabric.Circle.fromElement = function(element, options) {
fabric.Circle.fromElement = function(element, callback, options) {
options || (options = { });

var parsedAttributes = fabric.parseAttributes(element, fabric.Circle.ATTRIBUTE_NAMES);
Expand All @@ -204,7 +204,7 @@
parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.radius;
parsedAttributes.originX = 'left';
parsedAttributes.originY = 'top';
return new fabric.Circle(extend(parsedAttributes, options));
callback(new fabric.Circle(extend(parsedAttributes, options)));
};

/**
Expand All @@ -221,11 +221,10 @@
* @memberOf fabric.Circle
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {Object} Instance of fabric.Circle
*/
fabric.Circle.fromObject = function(object, callback, forceAsync) {
return fabric.Object._fromObject('Circle', object, callback, forceAsync);
fabric.Circle.fromObject = function(object, callback) {
return fabric.Object._fromObject('Circle', object, callback);
};

})(typeof exports !== 'undefined' ? exports : this);
10 changes: 5 additions & 5 deletions src/shapes/ellipse.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@
* @memberOf fabric.Ellipse
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @param {Function} [callback] Options callback invoked after parsing is finished
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromElement = function(element, options) {
fabric.Ellipse.fromElement = function(element, callback, options) {
options || (options = { });

var parsedAttributes = fabric.parseAttributes(element, fabric.Ellipse.ATTRIBUTE_NAMES);
Expand All @@ -179,7 +180,7 @@
parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.ry;
parsedAttributes.originX = 'left';
parsedAttributes.originY = 'top';
return new fabric.Ellipse(extend(parsedAttributes, options));
callback(new fabric.Ellipse(extend(parsedAttributes, options)));
};
/* _FROM_SVG_END_ */

Expand All @@ -189,11 +190,10 @@
* @memberOf fabric.Ellipse
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromObject = function(object, callback, forceAsync) {
return fabric.Object._fromObject('Ellipse', object, callback, forceAsync);
fabric.Ellipse.fromObject = function(object, callback) {
return fabric.Object._fromObject('Ellipse', object, callback);
};

})(typeof exports !== 'undefined' ? exports : this);
9 changes: 0 additions & 9 deletions src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,4 @@
});
};

/**
* Indicates that instances of this type are async
* @static
* @memberOf fabric.Group
* @type Boolean
* @default
*/
fabric.Group.async = true;

})(typeof exports !== 'undefined' ? exports : this);
18 changes: 1 addition & 17 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,8 @@
* Returns {@link fabric.Image} instance from an SVG element
* @static
* @param {SVGElement} element Element to parse
* @param {Function} callback Callback to execute when fabric.Image object is created
* @param {Object} [options] Options object
* @param {Function} callback Callback to execute when fabric.Image object is created
* @return {fabric.Image} Instance of fabric.Image
*/
fabric.Image.fromElement = function(element, callback, options) {
Expand All @@ -682,20 +682,4 @@
};
/* _FROM_SVG_END_ */

/**
* Indicates that instances of this type are async
* @static
* @type Boolean
* @default
*/
fabric.Image.async = true;

/**
* Indicates compression level used when generating PNG under Node (in applyFilters). Any of 0-9
* @static
* @type Number
* @default
*/
fabric.Image.pngCompression = 1;

})(typeof exports !== 'undefined' ? exports : this);
6 changes: 2 additions & 4 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,8 @@
* @memberOf fabric.IText
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as argument
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {fabric.IText} instance of fabric.IText
*/
fabric.IText.fromObject = function(object, callback, forceAsync) {
fabric.IText.fromObject = function(object, callback) {
parseDecoration(object);
if (object.styles) {
for (var i in object.styles) {
Expand All @@ -576,6 +574,6 @@
}
}
}
return fabric.Object._fromObject('IText', object, callback, forceAsync, 'text');
fabric.Object._fromObject('IText', object, callback, 'text');
};
})();
16 changes: 5 additions & 11 deletions src/shapes/line.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@
* @memberOf fabric.Line
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {fabric.Line} instance of fabric.Line
* @param {Function} [callback] callback function invoked after parsing
*/
fabric.Line.fromElement = function(element, options) {
fabric.Line.fromElement = function(element, callback, options) {
options = options || { };
var parsedAttributes = fabric.parseAttributes(element, fabric.Line.ATTRIBUTE_NAMES),
points = [
Expand All @@ -307,7 +307,7 @@
];
options.originX = 'left';
options.originY = 'top';
return new fabric.Line(points, extend(parsedAttributes, options));
callback(new fabric.Line(points, extend(parsedAttributes, options)));
};
/* _FROM_SVG_END_ */

Expand All @@ -317,21 +317,15 @@
* @memberOf fabric.Line
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {fabric.Line} instance of fabric.Line
*/
fabric.Line.fromObject = function(object, callback, forceAsync) {
fabric.Line.fromObject = function(object, callback) {
function _callback(instance) {
delete instance.points;
callback && callback(instance);
};
var options = clone(object, true);
options.points = [object.x1, object.y1, object.x2, object.y2];
var line = fabric.Object._fromObject('Line', options, _callback, forceAsync, 'points');
if (line) {
delete line.points;
}
return line;
fabric.Object._fromObject('Line', options, _callback, 'points');
};

/**
Expand Down
36 changes: 15 additions & 21 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1473,17 +1473,18 @@
},

/**
* Clones an instance, some objects are async, so using callback method will work for every object.
* Using the direct return does not work for images and groups.
* Clones an instance, using a callback method will work for every object.
* @param {Function} callback Callback is invoked with a clone as a first argument
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {fabric.Object} clone of an instance
*/
clone: function(callback, propertiesToInclude) {
var objectForm = this.toObject(propertiesToInclude);
if (this.constructor.fromObject) {
return this.constructor.fromObject(this.toObject(propertiesToInclude), callback);
this.constructor.fromObject(objectForm, callback);
}
else {
fabric.Object._fromObject('Object', objectForm, callback);
}
return new fabric.Object(this.toObject(propertiesToInclude));
},

/**
Expand Down Expand Up @@ -1867,26 +1868,19 @@
*/
fabric.Object.NUM_FRACTION_DIGITS = 2;

fabric.Object._fromObject = function(className, object, callback, forceAsync, extraParam) {
fabric.Object._fromObject = function(className, object, callback, extraParam) {
var klass = fabric[className];
object = clone(object, true);
if (forceAsync) {
fabric.util.enlivenPatterns([object.fill, object.stroke], function(patterns) {
if (typeof patterns[0] !== 'undefined') {
object.fill = patterns[0];
}
if (typeof patterns[1] !== 'undefined') {
object.stroke = patterns[1];
}
var instance = extraParam ? new klass(object[extraParam], object) : new klass(object);
callback && callback(instance);
});
}
else {
fabric.util.enlivenPatterns([object.fill, object.stroke], function(patterns) {
if (typeof patterns[0] !== 'undefined') {
object.fill = patterns[0];
}
if (typeof patterns[1] !== 'undefined') {
object.stroke = patterns[1];
}
var instance = extraParam ? new klass(object[extraParam], object) : new klass(object);
callback && callback(instance);
return instance;
}
});
};

/**
Expand Down
17 changes: 4 additions & 13 deletions src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,9 +909,8 @@
* @memberOf fabric.Path
* @param {Object} object
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
*/
fabric.Path.fromObject = function(object, callback, forceAsync) {
fabric.Path.fromObject = function(object, callback) {
if (typeof object.path === 'string') {
var pathUrl = object.path;
fabric.loadSVGFromURL(pathUrl, function (elements) {
Expand All @@ -925,7 +924,7 @@
});
}
else {
return fabric.Object._fromObject('Path', object, callback, forceAsync, 'path');
fabric.Object._fromObject('Path', object, callback, 'path');
}
};

Expand All @@ -945,22 +944,14 @@
* @param {SVGElement} element to parse
* @param {Function} callback Callback to invoke when an fabric.Path instance is created
* @param {Object} [options] Options object
* @param {Function} [callback] Options callback invoked after parsing is finished
*/
fabric.Path.fromElement = function(element, callback, options) {
var parsedAttributes = fabric.parseAttributes(element, fabric.Path.ATTRIBUTE_NAMES);
parsedAttributes.originX = 'left';
parsedAttributes.originY = 'top';
callback && callback(new fabric.Path(parsedAttributes.d, extend(parsedAttributes, options)));
callback(new fabric.Path(parsedAttributes.d, extend(parsedAttributes, options)));
};
/* _FROM_SVG_END_ */

/**
* Indicates that instances of this type are async
* @static
* @memberOf fabric.Path
* @type Boolean
* @default
*/
fabric.Path.async = true;

})(typeof exports !== 'undefined' ? exports : this);
14 changes: 6 additions & 8 deletions src/shapes/polygon.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@
* @static
* @memberOf fabric.Polygon
* @param {SVGElement} element Element to parse
* @param {Function} callback callback function invoked after parsing
* @param {Object} [options] Options object
* @return {fabric.Polygon} Instance of fabric.Polygon
*/
fabric.Polygon.fromElement = function(element, options) {
fabric.Polygon.fromElement = function(element, callback, options) {
if (!element) {
return null;
return callback(null);
}

options || (options = { });

var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);

return new fabric.Polygon(points, extend(parsedAttributes, options));
callback(new fabric.Polygon(points, extend(parsedAttributes, options)));
};
/* _FROM_SVG_END_ */

Expand All @@ -85,11 +85,9 @@
* @memberOf fabric.Polygon
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {fabric.Polygon} Instance of fabric.Polygon
*/
fabric.Polygon.fromObject = function(object, callback, forceAsync) {
return fabric.Object._fromObject('Polygon', object, callback, forceAsync, 'points');
fabric.Polygon.fromObject = function(object, callback) {
return fabric.Object._fromObject('Polygon', object, callback, 'points');
};

})(typeof exports !== 'undefined' ? exports : this);
16 changes: 7 additions & 9 deletions src/shapes/polyline.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@
* Returns fabric.Polyline instance from an SVG element
* @static
* @memberOf fabric.Polyline
* @param {SVGElement} element Element to parse
* @param {SVGElement} element Element to parser
* @param {Function} callback callback function invoked after parsing
* @param {Object} [options] Options object
* @return {fabric.Polyline} Instance of fabric.Polyline
*/
fabric.Polyline.fromElement = function(element, options) {
fabric.Polyline.fromElement = function(element, callback, options) {
if (!element) {
return null;
return callback(null);
}
options || (options = { });

var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES);

return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options));
callback(new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options)));
};
/* _FROM_SVG_END_ */

Expand All @@ -244,11 +244,9 @@
* @memberOf fabric.Polyline
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
* @param {Boolean} [forceAsync] Force an async behaviour trying to create pattern first
* @return {fabric.Polyline} Instance of fabric.Polyline
*/
fabric.Polyline.fromObject = function(object, callback, forceAsync) {
return fabric.Object._fromObject('Polyline', object, callback, forceAsync, 'points');
fabric.Polyline.fromObject = function(object, callback) {
return fabric.Object._fromObject('Polyline', object, callback, 'points');
};

})(typeof exports !== 'undefined' ? exports : this);
Loading

0 comments on commit 145bd3b

Please sign in to comment.