UNPKG

3.86 kBJavaScriptView Raw
1/** @license React v16.9.0-rc.0
2 * react-dom-unstable-fizz.browser.development.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';
11
12(function (global, factory) {
13 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
14 typeof define === 'function' && define.amd ? define(factory) :
15 (global.ReactDOMFizzServer = factory());
16}(this, (function () { 'use strict';
17
18function scheduleWork(callback) {
19 callback();
20}
21
22function flushBuffered(destination) {
23 // WHATWG Streams do not yet have a way to flush the underlying
24 // transform streams. https://github.com/whatwg/streams/issues/960
25}
26
27
28
29function writeChunk(destination, buffer) {
30 destination.enqueue(buffer);
31}
32
33
34
35function close(destination) {
36 destination.close();
37}
38
39var textEncoder = new TextEncoder();
40
41function convertStringToBuffer(content) {
42 return textEncoder.encode(content);
43}
44
45function formatChunk(type, props) {
46 var str = '<' + type + '>';
47 if (typeof props.children === 'string') {
48 str += props.children;
49 }
50 str += '</' + type + '>';
51 return convertStringToBuffer(str);
52}
53
54// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
55// nor polyfill, then a plain number is used for performance.
56var hasSymbol = typeof Symbol === 'function' && Symbol.for;
57
58var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
59
60
61
62
63
64
65// TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
66// (unstable) APIs that have been removed. Can we remove the symbols?
67
68function createRequest(children, destination) {
69 return { destination: destination, children: children, completedChunks: [], flowing: false };
70}
71
72function performWork(request) {
73 var element = request.children;
74 request.children = null;
75 if (element && element.$$typeof !== REACT_ELEMENT_TYPE) {
76 return;
77 }
78 var type = element.type;
79 var props = element.props;
80 if (typeof type !== 'string') {
81 return;
82 }
83 request.completedChunks.push(formatChunk(type, props));
84 if (request.flowing) {
85 flushCompletedChunks(request);
86 }
87
88 flushBuffered(request.destination);
89}
90
91function flushCompletedChunks(request) {
92 var destination = request.destination;
93 var chunks = request.completedChunks;
94 request.completedChunks = [];
95
96 try {
97 for (var i = 0; i < chunks.length; i++) {
98 var chunk = chunks[i];
99 writeChunk(destination, chunk);
100 }
101 } finally {
102
103 }
104 close(destination);
105}
106
107function startWork(request) {
108 request.flowing = true;
109 scheduleWork(function () {
110 return performWork(request);
111 });
112}
113
114function startFlowing(request, desiredBytes) {
115 request.flowing = false;
116 flushCompletedChunks(request);
117}
118
119// This file intentionally does *not* have the Flow annotation.
120// Don't add it. See `./inline-typed.js` for an explanation.
121
122function renderToReadableStream(children) {
123 var request = void 0;
124 return new ReadableStream({
125 start: function (controller) {
126 request = createRequest(children, controller);
127 startWork(request);
128 },
129 pull: function (controller) {
130 startFlowing(request, controller.desiredSize);
131 },
132 cancel: function (reason) {}
133 });
134}
135
136var ReactDOMFizzServerBrowser = {
137 renderToReadableStream: renderToReadableStream
138};
139
140var ReactDOMFizzServerBrowser$1 = Object.freeze({
141 default: ReactDOMFizzServerBrowser
142});
143
144var ReactDOMFizzServerBrowser$2 = ( ReactDOMFizzServerBrowser$1 && ReactDOMFizzServerBrowser ) || ReactDOMFizzServerBrowser$1;
145
146// TODO: decide on the top-level export form.
147// This is hacky but makes it work with both Rollup and Jest
148var unstableFizz_browser = ReactDOMFizzServerBrowser$2.default || ReactDOMFizzServerBrowser$2;
149
150return unstableFizz_browser;
151
152})));