UNPKG

@shopify/react-html

Version:

A component to render your react app with no static HTML.

113 lines (112 loc) 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var utilities_1 = require("./utilities"); exports.EFFECT_ID = Symbol('html'); var HtmlManager = /** @class */ (function () { function HtmlManager() { var _this = this; this.effect = { id: exports.EFFECT_ID, betweenEachPass: function () { return _this.reset(); }, }; this.serializations = utilities_1.getSerializationsFromDocument(); this.titles = []; this.metas = []; this.links = []; this.htmlAttributes = []; this.bodyAttributes = []; this.subscriptions = new Set(); } Object.defineProperty(HtmlManager.prototype, "state", { get: function () { var lastTitle = this.titles[this.titles.length - 1]; return { title: lastTitle && lastTitle.title, metas: this.metas, links: this.links, bodyAttributes: Object.assign.apply(Object, tslib_1.__spread([{}], this.bodyAttributes)), htmlAttributes: Object.assign.apply(Object, tslib_1.__spread([{}], this.htmlAttributes)), }; }, enumerable: true, configurable: true }); HtmlManager.prototype.reset = function (_a) { var _b = (_a === void 0 ? {} : _a).includeSerializations, includeSerializations = _b === void 0 ? false : _b; this.titles = []; this.metas = []; this.links = []; this.subscriptions.clear(); if (includeSerializations) { this.serializations.clear(); } }; HtmlManager.prototype.subscribe = function (subscription) { var _this = this; this.subscriptions.add(subscription); return function () { _this.subscriptions.delete(subscription); }; }; HtmlManager.prototype.addTitle = function (title) { return this.addDescriptor({ title: title }, this.titles); }; HtmlManager.prototype.addMeta = function (meta) { return this.addDescriptor(meta, this.metas); }; HtmlManager.prototype.addLink = function (link) { return this.addDescriptor(link, this.links); }; HtmlManager.prototype.addHtmlAttributes = function (attributes) { return this.addDescriptor(attributes, this.htmlAttributes); }; HtmlManager.prototype.addBodyAttributes = function (attributes) { return this.addDescriptor(attributes, this.bodyAttributes); }; HtmlManager.prototype.setSerialization = function (id, data) { this.serializations.set(id, data); }; HtmlManager.prototype.getSerialization = function (id) { return this.serializations.get(id); }; HtmlManager.prototype.extract = function () { return tslib_1.__assign({}, this.state, { serializations: tslib_1.__spread(this.serializations.entries()).map(function (_a) { var _b = tslib_1.__read(_a, 2), id = _b[0], data = _b[1]; return ({ id: id, data: data, }); }) }); }; HtmlManager.prototype.addDescriptor = function (item, list) { var _this = this; list.push(item); this.updateSubscriptions(); return function () { var index = list.indexOf(item); if (index >= 0) { list.splice(index, 1); _this.updateSubscriptions(); } }; }; HtmlManager.prototype.updateSubscriptions = function () { var e_1, _a; try { for (var _b = tslib_1.__values(this.subscriptions), _c = _b.next(); !_c.done; _c = _b.next()) { var subscription = _c.value; subscription(this.state); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; return HtmlManager; }()); exports.HtmlManager = HtmlManager;