Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simulation for a database read with limit/offset pagination.
- * Records are just padded id strings - e.g. 0001, 0002, ... 5062.
- * Each read takes randomly between 500-2500ms.
- * limit - how many records to return
- * offset - how many records to skip
- * MAX_RECORDS - max records in "database"
- */
- const read = async ({ limit, offset }: { limit: number, offset: number }) => {
- const MAX_RECORDS = 22;
- await new Promise((resolve) => setTimeout(resolve, Math.round(Math.random() * 2000 + 500)));
- const returnSize = Math.max(limit - Math.max(limit + offset - MAX_RECORDS, 0), 0);
- const startId = offset;
- return new Array(returnSize)
- .fill('0'.repeat(String(MAX_RECORDS).length))
- .map((nullId, i) => {
- return nullId.concat(String(startId + i))
- .slice(-String(MAX_RECORDS).length);
- });
- };
- /**
- * Readable-stream datasource for simulated "database".
- * Chunks from DB are pushed when ready.
- */
- class StreamSource extends Readable {
- private reads = 0;
- constructor() {
- super({ highWaterMark: 5 });
- }
- async _read(size: any) {
- console.log(`_read #${this.reads++} size=${size}`);
- const data = await read({ limit: size, offset: (this.reads - 1) * size });
- data.forEach(item => {
- this.push(`${item} `);
- });
- this.push('\n');
- // Pagination reached end. Finish the data stream.
- if (data.length < size) {
- this.push('-|\n');
- this.push(null);
- }
- }
- }
- new StreamSource().pipe(process.stdout);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement