@image/packer
Version:
image packer
53 lines (52 loc) • 2.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ScaledSprite_1 = require("./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_1.default(_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;
}());
exports.default = Sprite;