UNPKG

@image/packer

Version:
51 lines (50 loc) 1.97 kB
import ScaledSprite from './ScaledSprite'; var Sprite = (function () { function Sprite(spritePath, convertOptions, cache, imageProcessor) { this.cache = cache; this.imageProcessor = imageProcessor; this.path = spritePath; this.convertOptions = convertOptions; } Sprite.prototype.calculateSize = function () { return this.imageProcessor.getSize(this.path); }; Sprite.prototype.createAndProcessScaledVersions = function (queue) { var _this = this; this.scaledSprites = this.convertOptions.map(function (convertOption) { return new ScaledSprite(_this, convertOption, _this.cache, _this.imageProcessor); }); return Promise.all(this.scaledSprites.map(function (scaledSprite) { return queue.add(function () { return scaledSprite.process(); }); })); }; Sprite.prototype.calculateHash = function () { var _this = this; return this.cache.calculateHash(this.path).then(function (hash) { _this.hash = hash; }); }; Sprite.prototype.cacheMiss = function () { return this.calculateSize() .then(function (size) { return { width: size.width, height: size.height }; }); }; Sprite.prototype.cacheInterpret = function (data) { this.width = data.width; this.height = data.height; }; Sprite.prototype.process = function (queue) { var _this = this; return this.calculateHash() .then(function () { return _this.cache.lookup('sprite', _this.hash, _this.cacheMiss.bind(_this)); }) .then(this.cacheInterpret.bind(this)) .then(function () { return _this.createAndProcessScaledVersions(queue); }) .then(function () { return _this; }); }; return Sprite; }()); export default Sprite;