Last active
June 21, 2019 10:23
-
-
Save rikvermeer/a8148cec3dd94838606f8c6e5d50ec6b to your computer and use it in GitHub Desktop.
JavaScript async generator/reader/transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const genline = async (i) => [Date.now(), i, "CRITICAL", "gui", ": asfd asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf"].join(" ") | |
const generate = amount => { | |
let arr = []; | |
for(let i = 0; i < amount; i++) { | |
arr.push(genline(i)) | |
} | |
return arr | |
} | |
const transformer = { | |
decoder: new TextDecoder(), | |
lastString: '', | |
transform: function(chunk, controller) { | |
const string = `${this.lastString}${this.decoder.decode(chunk)}`; | |
const lines = string.split(/\r\n|[\r\n]/g); | |
this.lastString = lines.pop() || ''; | |
for (const line of lines) { | |
controller.enqueue(line) | |
} | |
}, | |
flush: function(controller) { | |
if(this.lastString) controller.enqueue(this.lastString) | |
} | |
} | |
const url = async amount => | |
URL.createObjectURL( | |
new Blob([(await Promise.all(generate(amount))).join("\n")], {type: 'text/plain'} | |
) | |
) | |
const getme = async (amount, fn) => | |
fetch(await url(amount)) | |
.then(rs => rs.body) | |
.then(rs => rs.pipeThrough(new TransformStream(transformer))) | |
.then(rs => rs.getReader()) | |
.then(async reader => { | |
while(true){ | |
const {done, value} = await reader.read(); | |
if(done) break; | |
fn(value) | |
} | |
} | |
) | |
getme(100000, res => document.body.innerText = res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment