react-streaming
Advanced tools
Comparing version 0.3.39 to 0.3.40-commit-30e9b4d
@@ -10,3 +10,3 @@ export { createBuffer }; | ||
declare type Chunk = string | Promise<string>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => Promise<void>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => void; | ||
declare type StreamOperations = { | ||
@@ -13,0 +13,0 @@ operations: null | { |
@@ -7,9 +7,7 @@ "use strict"; | ||
function createBuffer(streamOperations, doNotClosePromise) { | ||
const buffer = []; | ||
let bufferIsFlushing = false; | ||
let state = 'UNSTARTED'; | ||
// See Rule 2: https://github.com/brillout/react-streaming/tree/main/src#rule-2 | ||
let writePermission = false; | ||
let userChunkPromise = null; | ||
let onHead = () => { }; | ||
return { injectToStream, onReactWrite, onBeforeEnd, hasStreamEnded }; | ||
async function injectToStream(chunk, options) { | ||
function injectToStream(chunk, options) { | ||
if (debug.isEnabled) { | ||
@@ -21,15 +19,16 @@ debug('injectToStream()', getChunkAsString(chunk)); | ||
} | ||
buffer.push({ chunk, flush: options === null || options === void 0 ? void 0 : options.flush }); | ||
await flushBuffer(); | ||
const userChunkPromisePrevious = userChunkPromise; | ||
userChunkPromise = (async () => { | ||
if (userChunkPromisePrevious) | ||
await userChunkPromisePrevious; | ||
if (state !== 'STREAMING') | ||
await new Promise((resolve) => { | ||
onHead = resolve; | ||
}); | ||
if ((0, utils_1.isPromise)(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk, options === null || options === void 0 ? void 0 : options.flush); | ||
})(); | ||
} | ||
async function flushBuffer() { | ||
if (!writePermission) { | ||
return; | ||
} | ||
if (buffer.length === 0) { | ||
return; | ||
} | ||
if (bufferIsFlushing) { | ||
return; | ||
} | ||
function writeChunk(chunk, flush) { | ||
if (state !== 'STREAMING') { | ||
@@ -39,18 +38,5 @@ (0, utils_1.assert)(state === 'UNSTARTED'); | ||
} | ||
let flushStream = false; | ||
bufferIsFlushing = true; | ||
while (buffer.length > 0) { | ||
let { chunk, flush } = buffer.shift(); | ||
(0, utils_1.assert)(streamOperations.operations); | ||
const { writeChunk } = streamOperations.operations; | ||
if ((0, utils_1.isPromise)(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk); | ||
if (flush) | ||
flushStream = true; | ||
} | ||
bufferIsFlushing = false; | ||
(0, utils_1.assert)(buffer.length === 0); | ||
(0, utils_1.assert)(streamOperations.operations); | ||
if (flushStream && streamOperations.operations.flush !== null) { | ||
streamOperations.operations.writeChunk(chunk); | ||
if (flush && streamOperations.operations.flush !== null) { | ||
streamOperations.operations.flush(); | ||
@@ -61,21 +47,24 @@ debug('stream flushed'); | ||
async function onReactWrite(chunk) { | ||
state === 'UNSTARTED' && debug('>>> START'); | ||
const firstWrite = state === 'UNSTARTED'; | ||
firstWrite && debug('>>> START'); | ||
if (debug.isEnabled) { | ||
debug('react write', getChunkAsString(chunk)); | ||
} | ||
state = 'STREAMING'; | ||
const bufferReactEntry = { chunk: chunk, flush: true }; | ||
if (!writePermission) { | ||
buffer.unshift(bufferReactEntry); | ||
// See Rule 2: https://github.com/brillout/react-streaming/tree/main/src#rule-2 | ||
if (firstWrite) { | ||
state = 'STREAMING'; | ||
onHead(); | ||
} | ||
else { | ||
buffer.push(bufferReactEntry); | ||
await userChunkPromise; | ||
} | ||
writePermission = true; | ||
await flushBuffer(); | ||
writeChunk(chunk, true); | ||
} | ||
async function onBeforeEnd() { | ||
writePermission = true; // in case React didn't write anything | ||
await flushBuffer(); | ||
(0, utils_1.assert)(buffer.length === 0); | ||
{ | ||
// in case React didn't write anything | ||
state = 'STREAMING'; | ||
onHead(); | ||
} | ||
await userChunkPromise; | ||
await doNotClosePromise.promise; | ||
@@ -82,0 +71,0 @@ state = 'ENDED'; |
@@ -5,3 +5,3 @@ "use strict"; | ||
const getGlobalObject_1 = require("./getGlobalObject"); | ||
const PROJECT_VERSION = '0.3.39'; | ||
const PROJECT_VERSION = '0.3.40-commit-30e9b4d'; | ||
const projectInfo = { | ||
@@ -8,0 +8,0 @@ projectName: 'react-streaming', |
@@ -10,3 +10,3 @@ export { createBuffer }; | ||
declare type Chunk = string | Promise<string>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => Promise<void>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => void; | ||
declare type StreamOperations = { | ||
@@ -13,0 +13,0 @@ operations: null | { |
@@ -5,9 +5,7 @@ export { createBuffer }; | ||
function createBuffer(streamOperations, doNotClosePromise) { | ||
const buffer = []; | ||
let bufferIsFlushing = false; | ||
let state = 'UNSTARTED'; | ||
// See Rule 2: https://github.com/brillout/react-streaming/tree/main/src#rule-2 | ||
let writePermission = false; | ||
let userChunkPromise = null; | ||
let onHead = () => { }; | ||
return { injectToStream, onReactWrite, onBeforeEnd, hasStreamEnded }; | ||
async function injectToStream(chunk, options) { | ||
function injectToStream(chunk, options) { | ||
if (debug.isEnabled) { | ||
@@ -19,15 +17,16 @@ debug('injectToStream()', getChunkAsString(chunk)); | ||
} | ||
buffer.push({ chunk, flush: options === null || options === void 0 ? void 0 : options.flush }); | ||
await flushBuffer(); | ||
const userChunkPromisePrevious = userChunkPromise; | ||
userChunkPromise = (async () => { | ||
if (userChunkPromisePrevious) | ||
await userChunkPromisePrevious; | ||
if (state !== 'STREAMING') | ||
await new Promise((resolve) => { | ||
onHead = resolve; | ||
}); | ||
if (isPromise(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk, options === null || options === void 0 ? void 0 : options.flush); | ||
})(); | ||
} | ||
async function flushBuffer() { | ||
if (!writePermission) { | ||
return; | ||
} | ||
if (buffer.length === 0) { | ||
return; | ||
} | ||
if (bufferIsFlushing) { | ||
return; | ||
} | ||
function writeChunk(chunk, flush) { | ||
if (state !== 'STREAMING') { | ||
@@ -37,18 +36,5 @@ assert(state === 'UNSTARTED'); | ||
} | ||
let flushStream = false; | ||
bufferIsFlushing = true; | ||
while (buffer.length > 0) { | ||
let { chunk, flush } = buffer.shift(); | ||
assert(streamOperations.operations); | ||
const { writeChunk } = streamOperations.operations; | ||
if (isPromise(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk); | ||
if (flush) | ||
flushStream = true; | ||
} | ||
bufferIsFlushing = false; | ||
assert(buffer.length === 0); | ||
assert(streamOperations.operations); | ||
if (flushStream && streamOperations.operations.flush !== null) { | ||
streamOperations.operations.writeChunk(chunk); | ||
if (flush && streamOperations.operations.flush !== null) { | ||
streamOperations.operations.flush(); | ||
@@ -59,21 +45,24 @@ debug('stream flushed'); | ||
async function onReactWrite(chunk) { | ||
state === 'UNSTARTED' && debug('>>> START'); | ||
const firstWrite = state === 'UNSTARTED'; | ||
firstWrite && debug('>>> START'); | ||
if (debug.isEnabled) { | ||
debug('react write', getChunkAsString(chunk)); | ||
} | ||
state = 'STREAMING'; | ||
const bufferReactEntry = { chunk: chunk, flush: true }; | ||
if (!writePermission) { | ||
buffer.unshift(bufferReactEntry); | ||
// See Rule 2: https://github.com/brillout/react-streaming/tree/main/src#rule-2 | ||
if (firstWrite) { | ||
state = 'STREAMING'; | ||
onHead(); | ||
} | ||
else { | ||
buffer.push(bufferReactEntry); | ||
await userChunkPromise; | ||
} | ||
writePermission = true; | ||
await flushBuffer(); | ||
writeChunk(chunk, true); | ||
} | ||
async function onBeforeEnd() { | ||
writePermission = true; // in case React didn't write anything | ||
await flushBuffer(); | ||
assert(buffer.length === 0); | ||
{ | ||
// in case React didn't write anything | ||
state = 'STREAMING'; | ||
onHead(); | ||
} | ||
await userChunkPromise; | ||
await doNotClosePromise.promise; | ||
@@ -80,0 +69,0 @@ state = 'ENDED'; |
export { projectInfo }; | ||
import { getGlobalObject } from './getGlobalObject'; | ||
const PROJECT_VERSION = '0.3.39'; | ||
const PROJECT_VERSION = '0.3.40-commit-30e9b4d'; | ||
const projectInfo = { | ||
@@ -5,0 +5,0 @@ projectName: 'react-streaming', |
{ | ||
"name": "react-streaming", | ||
"description": "React 18 Streaming. Full-fledged & Easy.", | ||
"version": "0.3.39", | ||
"version": "0.3.40-commit-30e9b4d", | ||
"peerDependencies": { | ||
@@ -6,0 +6,0 @@ "react": ">=18", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149365
3114