-
Notifications
You must be signed in to change notification settings - Fork 13
/
slicer.ts
47 lines (39 loc) · 1.39 KB
/
slicer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import fs from 'fs';
import path from 'path';
import { parseJSON, fastCloneDeep } from '@terascope/utils';
import { SlicerRecoveryData } from '../../interfaces';
import { TestReaderConfig } from './interfaces';
import defaultData from './data/slicer-data';
import { Slicer } from '../../operations';
export default class TestSlicer extends Slicer<TestReaderConfig> {
requests: Record<string, any>[] = [];
position = 0;
async initialize(recoveryData: SlicerRecoveryData[]): Promise<void> {
await super.initialize(recoveryData);
const filePath = this.opConfig.slicer_data_file_path;
if (!filePath) {
this.requests = fastCloneDeep(defaultData);
return;
}
try {
this.requests = parseJSON(fs.readFileSync(path.resolve(filePath)));
} catch (err) {
throw new Error(`Unable to read file at path ${filePath}`);
}
}
async shutdown(): Promise<void> {
this.requests = [];
await super.shutdown();
}
async slice(): Promise<any|null> {
if (this.executionConfig.lifecycle === 'once') {
const request = this.requests.shift();
if (request == null) return null;
return request;
}
if (this.position + 1 >= this.requests.length) {
this.position = 0;
}
return this.requests[++this.position];
}
}