-
Notifications
You must be signed in to change notification settings - Fork 8
/
txt-reader.ts
380 lines (327 loc) · 12.3 KB
/
txt-reader.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { TextDecoder } from 'text-encoding-shim'
import 'promise-polyfill/src/polyfill'
import './polyfill'
import { IRequestMessage, IResponseMessage, IIteratorConfigMessage, SporadicLinesMap, IGetSporadicLinesResult } from './txt-reader-common'
import { TextDecoder_Instance } from 'text-encoding-shim'
import cloneDeep from "lodash.clonedeep"
interface ITaskResponse {
timeTaken: number;
message: string;
result: any;
}
interface ILoadFileTaskResponse extends ITaskResponse {
result: LoadFileResult;
}
interface ISniffLinesTaskResponse extends ITaskResponse {
result: (string | Uint8Array)[];
}
interface IGetLinesTaskResponse extends ITaskResponse {
result: (string | Uint8Array)[];
}
interface IGetSporadicLinesTaskResponse extends ITaskResponse {
result: IGetSporadicLinesResult[]
}
interface ISetChunkSizeResponse extends ITaskResponse {
result: number;
}
interface IIterateLinesTaskResponse extends ITaskResponse {
result: any;
}
type LoadFileResult = {
lineCount: number;
scope?: any;
}
interface IResponseMessageEvent extends MessageEvent {
data: IResponseMessage;
}
interface IIteratorScope {
[key: string]: any;
}
export interface IIteratorConfig {
eachLine: (this: IIteratorEachLineThis, raw: Uint8Array, progress: number, lineNumber: number) => void;
scope?: IIteratorScope;
}
interface IIteratorEachLineThis {
decode(value: Uint8Array): string;
[key: string]: any;
}
class RequestMessage implements IRequestMessage {
// When TxtReader in window context needs ask the worker to do a new task, it needs to send a requestMessage to the worker
// action to perform
public action: string;
// data to post
public data: any;
// unique task id, set by TxtReader.newTask method
public taskId!: number;
constructor(action: string, data?: any) {
this.action = action;
this.data = data !== undefined ? data : null;
}
}
enum TxtReaderTaskState {
Initialized,
Queued,
Running,
Completed
}
class TxtReaderTask<T> {
// All the communications between the Window context and the worker context are based on TxtReaderTask and following conventions
// 1. Only one task can be running at a time
// 2. Task is created through TxtReader.newTask method, DO NOT directly create instance of TxtReaderTask
// 3. TxtReader.newTask method returns an instance of TxtReaderTask
// 4. TxtReaderTask is async (based on promise)
// Usage: task.progress(progress=>{}).then(response=>{}).catch(reason=>{})
// task id, generated by TxtReader.newTaskId method
public id: number;
// requestMessage which will be used when window context posts message to the worker context
public requestMessage: RequestMessage;
// task state, initialized, queued, running, completed
public state: TxtReaderTaskState;
// reference to the TxtReader instance that created this task
private parser: TxtReader;
// property to save the onProgress callback function
private onProgress: Function | null;
// define the promise object used by the task
private promise: Promise<any> | null;
// promise resolve
private resolve: any;
// promise reject
private reject: any;
// record task startTime
private startTime: number;
constructor(id: number, reqMsg: RequestMessage, parser: TxtReader) {
this.id = id;
this.requestMessage = reqMsg;
this.parser = parser;
this.requestMessage.taskId = id;
this.state = TxtReaderTaskState.Initialized;
this.onProgress = null;
this.startTime = 0;
// initialize the task promise object, assign the resolve, reject methods.
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
private dispose() {
// release the memory inside promise obejct
this.resolve = null;
this.reject = null;
this.promise = null;
}
// run the task, postMessage would be triggered in TxtReader
// just change the state and record the task start time here
public run() {
this.state = TxtReaderTaskState.Running;
this.startTime = new Date().getTime();
}
// be called when a task completes no matter it succeeds or fails
public complete(response: IResponseMessage) {
this.state = TxtReaderTaskState.Completed;
let timeTaken: number = new Date().getTime() - this.startTime;
if (response.success) {
let taskResponse: ITaskResponse = {
timeTaken: timeTaken,
message: response.message,
result: response.result
}
this.resolve(taskResponse);
} else {
this.reject(response.message);
}
this.dispose();
}
public updateProgress(progress: number) {
if (this.onProgress !== null) {
this.onProgress.call(this.parser, progress);
}
}
public then(onFulFilled: (response: T) => void): TxtReaderTask<T> {
if (this.promise) {
this.promise.then((data) => {
onFulFilled.call(this.parser, data);
}).catch((reason) => { });
}
return this;
}
public catch(onFailed: (reason: string) => void): TxtReaderTask<T> {
if (this.promise) {
this.promise.catch((reason) => {
onFailed.call(this.parser, reason);
});
}
return this;
}
public progress(onProgress: (progress: number) => void): TxtReaderTask<T> {
this.onProgress = onProgress;
return this;
}
}
export class TxtReader {
private worker: Worker;
private taskList: TxtReaderTask<any>[];
private runningTask: TxtReaderTask<any> | null;
private queuedTaskList: TxtReaderTask<any>[];
private verboseLogging: boolean;
public utf8decoder: TextDecoder_Instance;
public lineCount: number;
private file: File | null;
constructor() {
this.taskList = [];
this.runningTask = null;
this.queuedTaskList = [];
this.verboseLogging = false;
this.utf8decoder = new TextDecoder('utf-8');
this.lineCount = 0;
Object.defineProperty(this, 'lineCount', {
value: 0,
writable: false
});
this.file = null;
this.worker = new Worker('txt-reader-worker.js?i=' + new Date().getTime());
this.worker.addEventListener('message', (event: IResponseMessageEvent) => {
if (this.verboseLogging) {
console.log('Main thread received a message from worker thread: \r\n', event.data);
}
if (this.runningTask === null) {
return;
}
let response = event.data;
if (response.taskId !== this.runningTask.id) {
throw (`Received task ID (${response.taskId}) does not match the running task ID (${this.runningTask.id}).`);
}
if (response.done) {
// the task completes
this.completeTask(response);
} else {
// the task is incomplete, means it is a progress message
if (Object.prototype.toString.call(response.result).toLowerCase() === '[object number]' && response.result >= 0 && response.result <= 100) {
this.runningTask.updateProgress(response.result);
} else {
throw ('Unkown message type');
}
}
}, false);
}
public sniffLines(file: File, lineNumber: number, decode: boolean = true): TxtReaderTask<ISniffLinesTaskResponse> {
return this.newTask<ISniffLinesTaskResponse>('sniffLines', {
file: file,
lineNumber: lineNumber,
decode: decode
});
}
public loadFile(file: File, config?: IIteratorConfig): TxtReaderTask<ILoadFileTaskResponse> {
this.file = file;
Object.defineProperty(this, 'lineCount', {
value: 0,
writable: false
});
let data: any = {
file: file
};
if (config) {
data.config = this.getItertorConfigMessage(cloneDeep(config));
}
return this.newTask<ILoadFileTaskResponse>('loadFile', data).then((response) => {
Object.defineProperty(this, 'lineCount', {
value: response.result.lineCount,
writable: false
});
});
}
public setChunkSize(chunkSize: number): TxtReaderTask<ISetChunkSizeResponse> {
return this.newTask('setChunkSize', chunkSize);
}
public enableVerbose() {
this.verboseLogging = true;
return this.newTask('enableVerbose');
}
public getLines(start: number, count: number, decode: boolean = true): TxtReaderTask<IGetLinesTaskResponse> {
return this.newTask<IGetLinesTaskResponse>('getLines', { start: start, count: count }).then((response) => {
for (let i = 0; i < response.result.length; i++) {
if (decode) {
response.result[i] = this.utf8decoder.decode(response.result[i] as any as Uint8Array);
}
}
});
}
public getSporadicLines(sporadicLinesMap: SporadicLinesMap, decode: boolean = true): TxtReaderTask<IGetSporadicLinesTaskResponse> {
return this.newTask<IGetSporadicLinesTaskResponse>('getSporadicLines', {
sporadicLinesMap: sporadicLinesMap,
decode: decode
});
}
public iterateLines(config: IIteratorConfig, start?: number, count?: number): TxtReaderTask<IIterateLinesTaskResponse> {
return this.newTask<IIterateLinesTaskResponse>('iterateLines', {
config: this.getItertorConfigMessage(cloneDeep(config)),
start: start !== undefined ? start : null,
count: count !== undefined ? count : null
});
}
public iterateSporadicLines(config: IIteratorConfig, sporadicLinesMap: SporadicLinesMap): TxtReaderTask<IIterateLinesTaskResponse> {
return this.newTask<IIterateLinesTaskResponse>('iterateSporadicLines', {
config: this.getItertorConfigMessage(cloneDeep(config)),
lines: sporadicLinesMap
});
}
private getItertorConfigMessage(config: IIteratorConfig): IIteratorConfigMessage {
let functionMap: string[] = [];
function functionToString(obj: any, entry: string): any {
let path = entry;
if (typeof obj === 'object') {
for (let i in obj) {
let pathi = `${path}["${i}"]`;
if (typeof obj[i] === 'function') {
obj[i] = obj[i].toString();
functionMap.push(pathi);
} else if (typeof obj[i] === 'object') {
obj[i] = functionToString(obj[i], pathi);
}
}
}
return obj;
}
return {
eachLine: config.eachLine.toString(),
scope: functionToString(config.scope, "") || {},
functionMap: functionMap
};
}
private newTask<T>(action: string, data?: any): TxtReaderTask<T> {
let reqMsg: RequestMessage = new RequestMessage(action, data);
let task: TxtReaderTask<T> = new TxtReaderTask<T>(this.newTaskId(), reqMsg, this);
this.taskList.push(task);
if (!this.runningTask) {
this.runTask(task);
} else {
this.queuedTaskList.push(task);
task.state = TxtReaderTaskState.Queued;
}
return task;
}
private completeTask(response: IResponseMessage) {
if (this.runningTask) {
this.runningTask.complete(response);
this.runningTask = null;
this.runNextTask();
}
}
private runNextTask() {
if (this.queuedTaskList.length) {
this.runTask(this.queuedTaskList.shift()!);
}
}
private runTask(task: TxtReaderTask<any>) {
this.runningTask = task;
this.worker.postMessage(task.requestMessage);
task.run();
}
private newTaskId(): number {
let taskListLength: number = this.taskList.length;
if (taskListLength === 0) {
return 1;
} else {
return this.taskList[taskListLength - 1].id + 1;
}
}
}