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.50.5 examples/fetch/public/stream.js
async-http-0.50.4 examples/fetch/public/stream.js
async-http-0.50.3 examples/fetch/public/stream.js
async-http-0.50.2 examples/fetch/public/stream.js
async-http-0.50.1 examples/fetch/public/stream.js
async-http-0.50.0 examples/fetch/public/stream.js
async-http-0.49.1 examples/fetch/public/stream.js
async-http-0.49.0 examples/fetch/public/stream.js
async-http-0.48.2 examples/fetch/public/stream.js
async-http-0.48.1 examples/fetch/public/stream.js
async-http-0.48.0 examples/fetch/public/stream.js
async-http-0.47.0 examples/fetch/public/stream.js
async-http-0.46.5 examples/fetch/public/stream.js
async-http-0.46.4 examples/fetch/public/stream.js
async-http-0.46.3 examples/fetch/public/stream.js
async-http-0.46.2 examples/fetch/public/stream.js
async-http-0.46.1 examples/fetch/public/stream.js
async-http-0.46.0 examples/fetch/public/stream.js
async-http-0.45.9 examples/fetch/public/stream.js
async-http-0.45.8 examples/fetch/public/stream.js