вторник, 9 июля 2019 г.

Node.js - Simple Stream and Generator Demos

Simple Stream.

const {Readable, Writable} = require('stream');

// Reader
const createCounterReader = () => {
let count = 0;
return new Readable({
objectMode: true,
read () {
count += 1;
console.log('reading:', count);
this.push(count);
}
});
};

const counterReader = createCounterReader();

// Writer
const logWriter = new Writable({
objectMode: true,
write: (chunk, _, done) => {
setTimeout(() => {
console.log('writing:', chunk);
done();
}, 100);
}
});

// Pipe reader to Writer
counterReader.pipe(logWriter);


Stream for async counter.

const {Readable, Writable} = require('stream');

const createCounterReader = (delay) => {
let counter = 0;
const reader = new Readable({
objectMode: true,
read () {}
});
setInterval(() => {
counter += 1;
console.log('reading:', counter);
reader.push(counter);
}, delay);
return reader;
};

const counterReader = createCounterReader(1000);

const logWriter = new Writable({
objectMode: true,
write: (chunk, _, done) => {
console.log('writing:', chunk);
done();
}
});

counterReader.pipe(logWriter);


Stream for async counter writer.

const {Readable, Writable} = require('stream');

const createCounterReader = (delay) => {
let counter = 0;
const reader = new Readable({
objectMode: true,
read () {}
});
setInterval(() => {
counter += 1;
console.log('reading:', counter);
reader.push(counter);
}, delay);
return reader;
};

const counterReader = createCounterReader(1000);

const logWriter = new Writable({
objectMode: true,
write: (chunk, _, done) => {
setTimeout(() => {
console.log('writing:', chunk);
done();
}, 5 * 1000);
}
});

counterReader.pipe(logWriter);


Simple Generator Sync.

function* counterGenerator() {
let count = 0;
while (true) {
count += 1;
console.log('reading:', count);
yield count;
}
}

const counterIterator = counterGenerator();

const logIterator = (iterator) => {
for (const item of iterator) {
console.log('writing:', item);
}
};

logIterator(counterIterator);


Simple Generator Async.

function* counterGenerator() {
let count = 0;
while (true) {
count += 1;
console.log('reading:', count);
yield count;
}
}

const counterIterator = counterGenerator();

const logIterator = async (iterator) => {
for (const item of iterator) {
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 100);
});
console.log('writing:', item);
}
};

logIterator(counterIterator);


Async Generator Counter.

async function* counterGenerator(delay) {
let counter = 0;
while (true) {
await new Promise(r => setTimeout(r, delay));
counter += 1;
console.log('reading:', counter);
yield counter;
}
}
const counterIterator = counterGenerator(1000);

const logIterator = async (iterator) => {
for await (const item of iterator) {
console.log('writing:', item);
}
};

logIterator(counterIterator);


Async Generator Counter for Async Interator.

async function* counterGenerator(delay) {
let counter = 0;
while (true) {
await new Promise(r => setTimeout(r, delay));
counter += 1;
console.log('reading:', counter);
yield counter;
}
}
const counterIterator = counterGenerator(1000);

const logIterator = async (iterator) => {
for await (const item of iterator) {
console.log('writing:', item);
await new Promise(r => setTimeout(r, 5 * 1000));
}
};

logIterator(counterIterator);

Комментариев нет:

Отправить комментарий