|
1 | | -/* eslint-disable no-magic-numbers,@typescript-eslint/no-magic-numbers,max-classes-per-file */ |
2 | 1 | import {PlainObject} from '@angular-ru/cdk/typings'; |
3 | | -import {WebWorkerThreadService} from '@angular-ru/cdk/webworker'; |
4 | 2 |
|
5 | 3 | export class MocksGenerator { |
6 | | - // eslint-disable-next-line max-lines-per-function |
7 | 4 | public static async generator( |
8 | 5 | rowsNumber: number, |
9 | 6 | colsNumber: number, |
10 | 7 | startIndex = 0, |
11 | 8 | ): Promise<PlainObject[]> { |
12 | | - return new WebWorkerThreadService().run<PlainObject[], any>( |
13 | | - // eslint-disable-next-line max-lines-per-function |
14 | | - (data: any): PlainObject[] => { |
15 | | - class FakeGenerator { |
16 | | - // eslint-disable-next-line max-lines-per-function |
17 | | - public static generateTable( |
18 | | - rows: number, |
19 | | - cols: number, |
20 | | - start: number, |
21 | | - ): PlainObject[] { |
22 | | - const startDate: Date = new Date(); |
23 | | - const endDate: Date = new Date( |
24 | | - new Date().setFullYear(new Date().getFullYear() + 1), |
25 | | - ); |
26 | | - |
27 | | - // eslint-disable-next-line max-lines-per-function |
28 | | - return new Array(rows).fill(0).map( |
29 | | - // eslint-disable-next-line max-lines-per-function |
30 | | - (_: unknown, index: number): PlainObject => { |
31 | | - const idx: number = start + index + 1; |
32 | | - |
33 | | - const baseRow: PlainObject = { |
34 | | - id: idx, |
35 | | - reverseId: |
36 | | - Math.round( |
37 | | - Math.random() + |
38 | | - rows * 512 + |
39 | | - cols + |
40 | | - start * 10, |
41 | | - ) * 1024, |
42 | | - someDate: new Date( |
43 | | - startDate.getTime() + |
44 | | - Math.random() * |
45 | | - (endDate.getTime() - startDate.getTime()), |
46 | | - ).getTime(), |
47 | | - name: `Random - ${((Math.random() + 1) * 100).toFixed(0)}__${idx}`, |
48 | | - description: `Random - ${((Math.random() + 1) * 100).toFixed(0)}__${idx}`, |
49 | | - guid: `${'5cdae5b2ba0a57f709b72142' + '__'}${idx}`, |
50 | | - someBoolean: Math.random() > 0.5, |
51 | | - someNull: Math.random() > 0.5 ? null : 'not null', |
52 | | - }; |
53 | | - |
54 | | - // eslint-disable-next-line @typescript-eslint/typedef,@typescript-eslint/explicit-function-return-type |
55 | | - const random = (min: number, max: number) => |
56 | | - min + Math.random() * (max - min); |
57 | | - |
58 | | - if (cols > 7) { |
59 | | - baseRow[ |
60 | | - 'About Big Text And More Powerful Label Fugiat Tempor Sunt Nostrud' |
61 | | - ] = new Array(Math.ceil(random(0, 1000))) |
62 | | - .fill(null) |
63 | | - .map((): string => |
64 | | - (~~(Math.random() * 36)).toString(36), |
65 | | - ) |
66 | | - .join(''); |
67 | | - |
68 | | - for (let i = 7; i <= cols - 1; i++) { |
69 | | - baseRow[`column-${i}`] = `$row-${idx} $col-${i}`; |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - return baseRow; |
74 | | - }, |
75 | | - ); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - return FakeGenerator.generateTable(data.rows, data.cols, data.start); |
| 9 | + const worker = new Worker(new URL('./mocks-generator.worker', import.meta.url)); |
| 10 | + |
| 11 | + return new Promise<PlainObject[]>( |
| 12 | + (resolve: (value: PlainObject[]) => void): void => { |
| 13 | + worker.onmessage = ({data}: {data: PlainObject[]}) => { |
| 14 | + resolve(data); |
| 15 | + }; |
| 16 | + |
| 17 | + worker.postMessage({ |
| 18 | + rows: rowsNumber, |
| 19 | + cols: colsNumber, |
| 20 | + start: startIndex, |
| 21 | + }); |
80 | 22 | }, |
81 | | - {rows: rowsNumber, cols: colsNumber, start: startIndex}, |
82 | 23 | ); |
83 | 24 | } |
84 | 25 | } |
0 commit comments