UNPKG

react-typestyle

Version:

A react integration of the TypeStyle ecosystem

70 lines 2.9 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var free_style_1 = require("free-style"); var registry_1 = require("./registry"); var utils_1 = require("./utils"); var Cache = (function (_super) { __extends(Cache, _super); function Cache(options) { var _this = _super.call(this, options) || this; _this.classNames = {}; _this.dynamicStyles = {}; _this.dynamicRegistry = new registry_1.default(options); _this.mount(_this.dynamicRegistry); return _this; } Cache.prototype.clear = function () { this.dynamicSheets = undefined; this.dynamicStyles = {}; this.dynamicRegistry.off(this.update); this.dynamicRegistry.clear(); this.dynamicRegistry.on(this.update); this.classNames = {}; var patch = this.freeStyle; this.freeStyle = free_style_1.create(undefined, true); return this.publish(patch, true); }; Cache.prototype.register = function (sheet) { if (sheet) { if (typeof sheet === 'function') { if (!this.dynamicSheets) this.dynamicSheets = []; this.dynamicSheets.push(sheet); } else { var result = utils_1.splitSheet(sheet); Object.assign(this.dynamicStyles, result.dynamic); Object.assign(this.classNames, this.sheet(result.static)); } } return this; }; Cache.prototype.render = function (props) { var patch = free_style_1.create(undefined, true); var dynamicSheet = this.dynamicSheets ? Object.assign.apply(Object, [{}].concat(this.dynamicSheets.map(function (sheet) { return sheet(props); }), [this.dynamicStyles])) : this.dynamicStyles; var classNames = utils_1.processSheet(this.registerStyle.bind(this, patch), dynamicSheet, props, __assign({}, this.classNames)); this.dynamicRegistry.clear().update(patch); return classNames; }; return Cache; }(registry_1.default)); exports.default = Cache; //# sourceMappingURL=cache.js.map