Sha256: 36cddfa45473553243259d83db418b6dc50f89fef02099cc3c88edc74c56821a

Contents?: true

Size: 1.25 KB

Versions: 89

Compression:

Stored size: 1.25 KB

Contents

const inputStream = new ReadableStream({
	start(controller) {
		interval = setInterval(() => {
			let string = "Hello World!";

			// Add the string to the stream
			controller.enqueue(string);

			// show it on the screen
			let listItem = document.createElement('li');
			listItem.textContent = string;
			sent.appendChild(listItem);
		}, 10000);

		stopButton.addEventListener('click', function() {
			clearInterval(interval);
			controller.close();
		})
	},
	pull(controller) {
		// We don't really need a pull in this example
	},
	cancel() {
		// This is called if the reader cancels,
		// so we should stop generating strings
		clearInterval(interval);
	}
});

fetch("/echo", {method: 'POST', body: inputStream})
	.then(response => {
		const reader = response.body.getReader();
		const decoder = new TextDecoder("utf-8");
		
		function push() {
			reader.read().then(({done, value}) => {
				console.log("done:", done, "value:", value);
				const string = decoder.decode(value);
				
				// show it on the screen
				let listItem = document.createElement('li');
				
				if (done)
					listItem.textContent = "<EOF>"
				else
					listItem.textContent = string;
				
				received.appendChild(listItem);
				
				if (done) return;
				else push();
			});
		};
		
		push();
	});

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
async-http-0.52.4 examples/fetch/public/stream.js
async-http-0.52.3 examples/fetch/public/stream.js
async-http-0.52.2 examples/fetch/public/stream.js
async-http-0.52.1 examples/fetch/public/stream.js
async-http-0.52.0 examples/fetch/public/stream.js
async-http-0.51.6 examples/fetch/public/stream.js
async-http-0.51.5 examples/fetch/public/stream.js
async-http-0.51.4 examples/fetch/public/stream.js
async-http-0.51.3 examples/fetch/public/stream.js
async-http-0.51.2 examples/fetch/public/stream.js
async-http-0.51.1 examples/fetch/public/stream.js
async-http-0.51.0 examples/fetch/public/stream.js
async-http-0.50.13 examples/fetch/public/stream.js
async-http-0.50.12 examples/fetch/public/stream.js
async-http-0.50.11 examples/fetch/public/stream.js
async-http-0.50.10 examples/fetch/public/stream.js
async-http-0.50.9 examples/fetch/public/stream.js
async-http-0.50.8 examples/fetch/public/stream.js
async-http-0.50.7 examples/fetch/public/stream.js
async-http-0.50.6 examples/fetch/public/stream.js