Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
view-container / dist / models / ServerStyleSheet.js
Size: Mime:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @flow
/* eslint-disable no-underscore-dangle */
const react_1 = __importDefault(require("react"));
const stream_1 = __importDefault(require("stream"));
const constants_1 = require("../constants");
const StyleSheet_1 = __importDefault(require("./StyleSheet"));
const StyleSheetManager_1 = __importDefault(require("./StyleSheetManager"));
/* this error is used for makeStyleTag */
const sheetClosedErr = process.env.NODE_ENV !== 'production'
    ? `
Can't collect styles once you've consumed a ServerStyleSheet's styles!
ServerStyleSheet is a one off instance for each server-side render cycle.
- Are you trying to reuse it across renders?
- Are you accidentally calling collectStyles twice?
`.trim()
    : '';
const streamBrowserErr = process.env.NODE_ENV !== 'production'
    ? 'Streaming SSR is only supported in a Node.js environment; Please do not try to call this method in the browser.'
    : '';
class ServerStyleSheet {
    constructor() {
        /* The master sheet might be reset, so keep a reference here */
        this.masterSheet = StyleSheet_1.default.master;
        this.instance = this.masterSheet.clone();
        this.closed = false;
    }
    complete() {
        if (!this.closed) {
            /* Remove closed StyleSheets from the master sheet */
            const index = this.masterSheet.clones.indexOf(this.instance);
            this.masterSheet.clones.splice(index, 1);
            this.closed = true;
        }
    }
    collectStyles(children) {
        if (this.closed) {
            throw new Error(sheetClosedErr);
        }
        return (react_1.default.createElement(StyleSheetManager_1.default, { sheet: this.instance }, children));
    }
    getStyleTags() {
        this.complete();
        return this.instance.toHTML();
    }
    getStyleElement() {
        this.complete();
        return this.instance.toReactElements();
    }
    interleaveWithNodeStream(readableStream) {
        if (!__SERVER__ || constants_1.IS_BROWSER) {
            throw new Error(streamBrowserErr);
        }
        /* the tag index keeps track of which tags have already been emitted */
        const { instance } = this;
        let instanceTagIndex = 0;
        const streamAttr = `${constants_1.SC_STREAM_ATTR}="true"`;
        const transformer = new stream_1.default.Transform({
            transform: function appendStyleChunks(chunk, /* encoding */ _, callback) {
                const { tags } = instance;
                let html = '';
                /* retrieve html for each new style tag */
                for (; instanceTagIndex < tags.length; instanceTagIndex += 1) {
                    const tag = tags[instanceTagIndex];
                    html += tag.toHTML(streamAttr);
                }
                /* force our StyleSheets to emit entirely new tags */
                instance.sealAllTags();
                /* prepend style html to chunk */
                this.push(html + chunk);
                callback();
            },
        });
        readableStream.on('end', () => this.complete());
        readableStream.on('error', err => {
            this.complete();
            // forward the error to the transform stream
            transformer.emit('error', err);
        });
        return readableStream.pipe(transformer);
    }
}
exports.default = ServerStyleSheet;
//# sourceMappingURL=ServerStyleSheet.js.map