react-csv-autopilot 0.0.5 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -59,14 +59,6 @@ resolveStrategy_fn = function() {
59
59
  return this.deps.blobExportStrategy;
60
60
  };
61
61
 
62
- // src/core/strategy/BolbExportStrategy.ts
63
- var BolbExportStrategy = class {
64
- export(_params) {
65
- return Promise.resolve({ finished: true, totalRowsLoaded: 10 });
66
- }
67
- };
68
- var BolbExportStrategy_default = BolbExportStrategy;
69
-
70
62
  // src/core/contants/index.ts
71
63
  var WEB_WORKER_NAME = "scv-worker";
72
64
  var BROADCAST_CHANNEL_NAME = "react-csv-exporter";
@@ -135,6 +127,103 @@ listenerRegistry_fn = function() {
135
127
  var WorkerManager = _WorkerManager;
136
128
  var WorkerManager_default = WorkerManager;
137
129
 
130
+ // src/core/strategy/BolbExportStrategy.ts
131
+ var BolbExportStrategy = class {
132
+ constructor() {
133
+ this.workerManager = WorkerManager_default.initialise();
134
+ }
135
+ async export(params) {
136
+ const suggestedName = params.fileName ?? "export";
137
+ const filename = suggestedName.endsWith(".csv") ? suggestedName : `${suggestedName}.csv`;
138
+ let iterator = 0;
139
+ let totalRowsLoaded = 0;
140
+ const messaging = new BroadcastChannel(BROADCAST_CHANNEL_NAME);
141
+ const csvParts = [];
142
+ try {
143
+ while (true) {
144
+ const response = await params.getNextPage(iterator++);
145
+ const safeRows = Array.isArray(response.rows) ? response.rows : [];
146
+ const safeTotal = response.total ?? 0;
147
+ const isRowsEmpty = safeRows.length === 0;
148
+ const nextRowsLoaded = totalRowsLoaded + safeRows.length;
149
+ totalRowsLoaded = isRowsEmpty ? safeTotal : nextRowsLoaded;
150
+ const isFinished = safeTotal > 0 ? totalRowsLoaded >= safeTotal : isRowsEmpty;
151
+ if (isRowsEmpty) {
152
+ messaging.postMessage(
153
+ JSON.stringify({
154
+ loadedItemsCount: totalRowsLoaded,
155
+ total: safeTotal,
156
+ type: "done"
157
+ })
158
+ );
159
+ await this.workerManager.triggerWorker({
160
+ id: iterator,
161
+ type: "completed"
162
+ });
163
+ break;
164
+ }
165
+ const csvChunk = await this.workerManager.triggerWorker({
166
+ columns: params.columns,
167
+ data: safeRows,
168
+ id: iterator,
169
+ type: "to_csv_chunk"
170
+ });
171
+ csvParts.push(csvChunk);
172
+ messaging.postMessage(
173
+ JSON.stringify({
174
+ loadedItemsCount: totalRowsLoaded,
175
+ total: safeTotal,
176
+ type: "progress"
177
+ })
178
+ );
179
+ if (isFinished) {
180
+ messaging.postMessage(
181
+ JSON.stringify({
182
+ loadedItemsCount: totalRowsLoaded,
183
+ total: safeTotal,
184
+ type: "done"
185
+ })
186
+ );
187
+ await this.workerManager.triggerWorker({
188
+ id: iterator,
189
+ type: "completed"
190
+ });
191
+ break;
192
+ }
193
+ }
194
+ const blob = new Blob(["\uFEFF", ...csvParts], {
195
+ type: "text/csv;charset=utf-8"
196
+ });
197
+ this.downloadBlob(blob, filename);
198
+ return { finished: true, totalRowsLoaded };
199
+ } catch (error) {
200
+ messaging.postMessage(
201
+ JSON.stringify({
202
+ loadedItemsCount: totalRowsLoaded,
203
+ total: 0,
204
+ type: "failed"
205
+ })
206
+ );
207
+ throw error;
208
+ } finally {
209
+ messaging.close();
210
+ this.workerManager.terminate();
211
+ }
212
+ }
213
+ downloadBlob(blob, filename) {
214
+ const url = URL.createObjectURL(blob);
215
+ const a = document.createElement("a");
216
+ a.href = url;
217
+ a.download = filename;
218
+ a.rel = "noopener";
219
+ document.body.appendChild(a);
220
+ a.click();
221
+ a.remove();
222
+ URL.revokeObjectURL(url);
223
+ }
224
+ };
225
+ var BolbExportStrategy_default = BolbExportStrategy;
226
+
138
227
  // src/core/strategy/FsAccessExportStrategy.ts
139
228
  var FsAccessExportStrategy = class {
140
229
  constructor() {
package/dist/index.d.cts CHANGED
@@ -22,7 +22,10 @@ interface ExportStrategy {
22
22
  }
23
23
 
24
24
  declare class BolbExportStrategy implements ExportStrategy {
25
- export<T>(_params: ExportParams<T>): Promise<ExportResponse>;
25
+ private workerManager;
26
+ constructor();
27
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
28
+ private downloadBlob;
26
29
  }
27
30
 
28
31
  declare class FsAccessExportStrategy implements ExportStrategy {
package/dist/index.d.ts CHANGED
@@ -22,7 +22,10 @@ interface ExportStrategy {
22
22
  }
23
23
 
24
24
  declare class BolbExportStrategy implements ExportStrategy {
25
- export<T>(_params: ExportParams<T>): Promise<ExportResponse>;
25
+ private workerManager;
26
+ constructor();
27
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
28
+ private downloadBlob;
26
29
  }
27
30
 
28
31
  declare class FsAccessExportStrategy implements ExportStrategy {
package/dist/index.js CHANGED
@@ -33,14 +33,6 @@ resolveStrategy_fn = function() {
33
33
  return this.deps.blobExportStrategy;
34
34
  };
35
35
 
36
- // src/core/strategy/BolbExportStrategy.ts
37
- var BolbExportStrategy = class {
38
- export(_params) {
39
- return Promise.resolve({ finished: true, totalRowsLoaded: 10 });
40
- }
41
- };
42
- var BolbExportStrategy_default = BolbExportStrategy;
43
-
44
36
  // src/core/contants/index.ts
45
37
  var WEB_WORKER_NAME = "scv-worker";
46
38
  var BROADCAST_CHANNEL_NAME = "react-csv-exporter";
@@ -108,6 +100,103 @@ listenerRegistry_fn = function() {
108
100
  var WorkerManager = _WorkerManager;
109
101
  var WorkerManager_default = WorkerManager;
110
102
 
103
+ // src/core/strategy/BolbExportStrategy.ts
104
+ var BolbExportStrategy = class {
105
+ constructor() {
106
+ this.workerManager = WorkerManager_default.initialise();
107
+ }
108
+ async export(params) {
109
+ const suggestedName = params.fileName ?? "export";
110
+ const filename = suggestedName.endsWith(".csv") ? suggestedName : `${suggestedName}.csv`;
111
+ let iterator = 0;
112
+ let totalRowsLoaded = 0;
113
+ const messaging = new BroadcastChannel(BROADCAST_CHANNEL_NAME);
114
+ const csvParts = [];
115
+ try {
116
+ while (true) {
117
+ const response = await params.getNextPage(iterator++);
118
+ const safeRows = Array.isArray(response.rows) ? response.rows : [];
119
+ const safeTotal = response.total ?? 0;
120
+ const isRowsEmpty = safeRows.length === 0;
121
+ const nextRowsLoaded = totalRowsLoaded + safeRows.length;
122
+ totalRowsLoaded = isRowsEmpty ? safeTotal : nextRowsLoaded;
123
+ const isFinished = safeTotal > 0 ? totalRowsLoaded >= safeTotal : isRowsEmpty;
124
+ if (isRowsEmpty) {
125
+ messaging.postMessage(
126
+ JSON.stringify({
127
+ loadedItemsCount: totalRowsLoaded,
128
+ total: safeTotal,
129
+ type: "done"
130
+ })
131
+ );
132
+ await this.workerManager.triggerWorker({
133
+ id: iterator,
134
+ type: "completed"
135
+ });
136
+ break;
137
+ }
138
+ const csvChunk = await this.workerManager.triggerWorker({
139
+ columns: params.columns,
140
+ data: safeRows,
141
+ id: iterator,
142
+ type: "to_csv_chunk"
143
+ });
144
+ csvParts.push(csvChunk);
145
+ messaging.postMessage(
146
+ JSON.stringify({
147
+ loadedItemsCount: totalRowsLoaded,
148
+ total: safeTotal,
149
+ type: "progress"
150
+ })
151
+ );
152
+ if (isFinished) {
153
+ messaging.postMessage(
154
+ JSON.stringify({
155
+ loadedItemsCount: totalRowsLoaded,
156
+ total: safeTotal,
157
+ type: "done"
158
+ })
159
+ );
160
+ await this.workerManager.triggerWorker({
161
+ id: iterator,
162
+ type: "completed"
163
+ });
164
+ break;
165
+ }
166
+ }
167
+ const blob = new Blob(["\uFEFF", ...csvParts], {
168
+ type: "text/csv;charset=utf-8"
169
+ });
170
+ this.downloadBlob(blob, filename);
171
+ return { finished: true, totalRowsLoaded };
172
+ } catch (error) {
173
+ messaging.postMessage(
174
+ JSON.stringify({
175
+ loadedItemsCount: totalRowsLoaded,
176
+ total: 0,
177
+ type: "failed"
178
+ })
179
+ );
180
+ throw error;
181
+ } finally {
182
+ messaging.close();
183
+ this.workerManager.terminate();
184
+ }
185
+ }
186
+ downloadBlob(blob, filename) {
187
+ const url = URL.createObjectURL(blob);
188
+ const a = document.createElement("a");
189
+ a.href = url;
190
+ a.download = filename;
191
+ a.rel = "noopener";
192
+ document.body.appendChild(a);
193
+ a.click();
194
+ a.remove();
195
+ URL.revokeObjectURL(url);
196
+ }
197
+ };
198
+ var BolbExportStrategy_default = BolbExportStrategy;
199
+
111
200
  // src/core/strategy/FsAccessExportStrategy.ts
112
201
  var FsAccessExportStrategy = class {
113
202
  constructor() {
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "sideEffects": false,
61
61
  "type": "module",
62
62
  "types": "dist/index.d.ts",
63
- "version": "0.0.5"
63
+ "version": "1.0.1"
64
64
  }