@sprucelabs/mercury-chunking-emitter 1.4.2 → 1.5.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/build/chunkingEmitter/ChunkingEmitter.d.ts +3 -2
- package/build/chunkingEmitter/ChunkingEmitter.js +15 -62
- package/build/chunkingEmitter/SingleChunkEmitter.d.ts +37 -0
- package/build/chunkingEmitter/SingleChunkEmitter.js +92 -0
- package/build/esm/chunkingEmitter/ChunkingEmitter.d.ts +3 -2
- package/build/esm/chunkingEmitter/ChunkingEmitter.js +13 -65
- package/build/esm/chunkingEmitter/SingleChunkEmitter.d.ts +37 -0
- package/build/esm/chunkingEmitter/SingleChunkEmitter.js +104 -0
- package/package.json +14 -14
|
@@ -6,13 +6,12 @@ export default class ChunkingEmitterImpl {
|
|
|
6
6
|
protected chunkSize: number;
|
|
7
7
|
private log;
|
|
8
8
|
static Class?: new (options: ChunkingEmitterOptions) => ChunkingEmitter;
|
|
9
|
+
private emitters;
|
|
9
10
|
private totalErrors;
|
|
10
11
|
protected constructor(options: ChunkingEmitterOptions);
|
|
11
12
|
static Emitter(options: ChunkingEmitterOptions): Promise<ChunkingEmitter>;
|
|
12
13
|
emit(options: ChunkingEmitterEmitOptions): Promise<void>;
|
|
13
|
-
private emitChunk;
|
|
14
14
|
static reset(): void;
|
|
15
|
-
private Cursor;
|
|
16
15
|
getTotalErrors(): number;
|
|
17
16
|
}
|
|
18
17
|
interface ChunkingEmitterOptions {
|
|
@@ -25,6 +24,8 @@ export type ChunkingEmitterEmitOptions = {
|
|
|
25
24
|
cursor?: BatchCursor<Record<string, any>>;
|
|
26
25
|
payloadKey: string;
|
|
27
26
|
target?: Record<string, any>;
|
|
27
|
+
/** will stop any running operations that match the key */
|
|
28
|
+
uniqueKey?: string;
|
|
28
29
|
};
|
|
29
30
|
export interface ChunkingEmitter {
|
|
30
31
|
emit(options: ChunkingEmitterEmitOptions): Promise<void>;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
var m = o[Symbol.asyncIterator], i;
|
|
5
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
4
|
};
|
|
9
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
const data_stores_1 = require("@sprucelabs/data-stores");
|
|
11
6
|
const schema_1 = require("@sprucelabs/schema");
|
|
12
7
|
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
|
|
8
|
+
const SingleChunkEmitter_1 = __importDefault(require("./SingleChunkEmitter"));
|
|
13
9
|
class ChunkingEmitterImpl {
|
|
14
10
|
constructor(options) {
|
|
15
11
|
this.log = (0, spruce_skill_utils_1.buildLog)('ChunkingEmitter');
|
|
12
|
+
this.emitters = [];
|
|
16
13
|
this.totalErrors = 0;
|
|
17
14
|
const { client, chunkSize } = (0, schema_1.assertOptions)(options, ['client']);
|
|
18
15
|
this.client = client;
|
|
@@ -24,8 +21,8 @@ class ChunkingEmitterImpl {
|
|
|
24
21
|
return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(options);
|
|
25
22
|
}
|
|
26
23
|
async emit(options) {
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
const { items, cursor, uniqueKey: unqiueKey, } = (0, schema_1.assertOptions)(options, ['eventName', 'payloadKey']);
|
|
25
|
+
this.totalErrors = 0;
|
|
29
26
|
if (!items && !cursor) {
|
|
30
27
|
throw new schema_1.SchemaError({
|
|
31
28
|
code: 'MISSING_PARAMETERS',
|
|
@@ -33,64 +30,20 @@ class ChunkingEmitterImpl {
|
|
|
33
30
|
friendlyMessage: `You have to pass either 'items' or a 'cursor' to emit.`,
|
|
34
31
|
});
|
|
35
32
|
}
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const total = await actualCursor.getTotalRecords();
|
|
40
|
-
try {
|
|
41
|
-
for (var _d = true, actualCursor_1 = __asyncValues(actualCursor), actualCursor_1_1; actualCursor_1_1 = await actualCursor_1.next(), _a = actualCursor_1_1.done, !_a; _d = true) {
|
|
42
|
-
_c = actualCursor_1_1.value;
|
|
43
|
-
_d = false;
|
|
44
|
-
const chunk = _c;
|
|
45
|
-
current = await this.emitChunk({
|
|
46
|
-
payloadKey,
|
|
47
|
-
chunk,
|
|
48
|
-
current,
|
|
49
|
-
total,
|
|
50
|
-
target,
|
|
51
|
-
eventName,
|
|
52
|
-
});
|
|
53
|
-
current++;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (!_d && !_a && (_b = actualCursor_1.return)) await _b.call(actualCursor_1);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e_1) throw e_1.error; }
|
|
33
|
+
const match = this.emitters.find((e) => unqiueKey && e.matchesKey(unqiueKey));
|
|
34
|
+
if (match) {
|
|
35
|
+
await match.kill();
|
|
62
36
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
[payloadKey]: chunk,
|
|
70
|
-
chunk: {
|
|
71
|
-
current,
|
|
72
|
-
total,
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
if (target) {
|
|
77
|
-
targetAndPayload.target = target;
|
|
78
|
-
}
|
|
79
|
-
await this.client.emitAndFlattenResponses(eventName, targetAndPayload);
|
|
80
|
-
}
|
|
81
|
-
catch (err) {
|
|
82
|
-
this.log.error('Failed to emit chunk', err);
|
|
83
|
-
this.totalErrors++;
|
|
84
|
-
}
|
|
85
|
-
return current;
|
|
37
|
+
const emitter = new SingleChunkEmitter_1.default(Object.assign(Object.assign({}, options), { chunkSize: this.chunkSize, client: this.client, log: this.log }));
|
|
38
|
+
this.emitters.push(emitter);
|
|
39
|
+
await emitter.emit();
|
|
40
|
+
this.totalErrors += emitter.getTotalErrors();
|
|
41
|
+
this.emitters = this.emitters.filter((e) => e !== emitter);
|
|
42
|
+
return;
|
|
86
43
|
}
|
|
87
44
|
static reset() {
|
|
88
45
|
this.Class = undefined;
|
|
89
46
|
}
|
|
90
|
-
Cursor(items) {
|
|
91
|
-
const cursor = new data_stores_1.BatchArrayCursor(items, { batchSize: this.chunkSize });
|
|
92
|
-
return cursor;
|
|
93
|
-
}
|
|
94
47
|
getTotalErrors() {
|
|
95
48
|
return this.totalErrors;
|
|
96
49
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BatchCursor } from '@sprucelabs/data-stores';
|
|
2
|
+
import { MercuryClient } from '@sprucelabs/mercury-client';
|
|
3
|
+
import { EventName } from '@sprucelabs/mercury-types';
|
|
4
|
+
import { Log } from '@sprucelabs/spruce-skill-utils';
|
|
5
|
+
export default class SingleChunkEmitter {
|
|
6
|
+
private cursor?;
|
|
7
|
+
private items?;
|
|
8
|
+
private payloadKey;
|
|
9
|
+
private target?;
|
|
10
|
+
private eventName;
|
|
11
|
+
private chunkSize;
|
|
12
|
+
private client;
|
|
13
|
+
private total;
|
|
14
|
+
private uniqueKey?;
|
|
15
|
+
private log;
|
|
16
|
+
totalErrors: number;
|
|
17
|
+
private isKilled;
|
|
18
|
+
constructor(options: SingleChunkEmitterOptions);
|
|
19
|
+
matchesKey(key: string): boolean;
|
|
20
|
+
getTotalErrors(): number;
|
|
21
|
+
emit(): Promise<void>;
|
|
22
|
+
kill(): Promise<void>;
|
|
23
|
+
private emitChunk;
|
|
24
|
+
private Cursor;
|
|
25
|
+
}
|
|
26
|
+
interface SingleChunkEmitterOptions {
|
|
27
|
+
cursor?: BatchCursor<Record<string, any>>;
|
|
28
|
+
items?: Record<string, any>[];
|
|
29
|
+
payloadKey: string;
|
|
30
|
+
target?: Record<string, any>;
|
|
31
|
+
eventName: EventName;
|
|
32
|
+
chunkSize: number;
|
|
33
|
+
client: MercuryClient;
|
|
34
|
+
uniqueKey?: string;
|
|
35
|
+
log: Log;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
const data_stores_1 = require("@sprucelabs/data-stores");
|
|
11
|
+
class SingleChunkEmitter {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.totalErrors = 0;
|
|
14
|
+
this.isKilled = false;
|
|
15
|
+
const { cursor, items, payloadKey, target, eventName, chunkSize, client, uniqueKey, log, } = options;
|
|
16
|
+
this.cursor = cursor;
|
|
17
|
+
this.uniqueKey = uniqueKey;
|
|
18
|
+
this.items = items;
|
|
19
|
+
this.payloadKey = payloadKey;
|
|
20
|
+
this.target = target;
|
|
21
|
+
this.eventName = eventName;
|
|
22
|
+
this.chunkSize = chunkSize;
|
|
23
|
+
this.client = client;
|
|
24
|
+
this.log = log.buildLog('Emitter');
|
|
25
|
+
}
|
|
26
|
+
matchesKey(key) {
|
|
27
|
+
return this.uniqueKey === key;
|
|
28
|
+
}
|
|
29
|
+
getTotalErrors() {
|
|
30
|
+
return this.totalErrors;
|
|
31
|
+
}
|
|
32
|
+
async emit() {
|
|
33
|
+
var _a, e_1, _b, _c;
|
|
34
|
+
var _d, _e;
|
|
35
|
+
let current = 0;
|
|
36
|
+
const actualCursor = (_d = this.cursor) !== null && _d !== void 0 ? _d : this.Cursor((_e = this.items) !== null && _e !== void 0 ? _e : []);
|
|
37
|
+
this.total = await actualCursor.getTotalRecords();
|
|
38
|
+
try {
|
|
39
|
+
for (var _f = true, actualCursor_1 = __asyncValues(actualCursor), actualCursor_1_1; actualCursor_1_1 = await actualCursor_1.next(), _a = actualCursor_1_1.done, !_a; _f = true) {
|
|
40
|
+
_c = actualCursor_1_1.value;
|
|
41
|
+
_f = false;
|
|
42
|
+
const chunk = _c;
|
|
43
|
+
try {
|
|
44
|
+
current = await this.emitChunk({
|
|
45
|
+
chunk,
|
|
46
|
+
current,
|
|
47
|
+
});
|
|
48
|
+
if (this.isKilled) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
this.log.error('Failed to emit chunk', err);
|
|
54
|
+
this.totalErrors++;
|
|
55
|
+
}
|
|
56
|
+
current++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
60
|
+
finally {
|
|
61
|
+
try {
|
|
62
|
+
if (!_f && !_a && (_b = actualCursor_1.return)) await _b.call(actualCursor_1);
|
|
63
|
+
}
|
|
64
|
+
finally { if (e_1) throw e_1.error; }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async kill() {
|
|
68
|
+
this.isKilled = true;
|
|
69
|
+
}
|
|
70
|
+
async emitChunk(options) {
|
|
71
|
+
const { chunk, current } = options;
|
|
72
|
+
let targetAndPayload = {
|
|
73
|
+
payload: {
|
|
74
|
+
[this.payloadKey]: chunk,
|
|
75
|
+
chunk: {
|
|
76
|
+
current,
|
|
77
|
+
total: this.total,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
if (this.target) {
|
|
82
|
+
targetAndPayload.target = this.target;
|
|
83
|
+
}
|
|
84
|
+
await this.client.emitAndFlattenResponses(this.eventName, targetAndPayload);
|
|
85
|
+
return current;
|
|
86
|
+
}
|
|
87
|
+
Cursor(items) {
|
|
88
|
+
const cursor = new data_stores_1.BatchArrayCursor(items, { batchSize: this.chunkSize });
|
|
89
|
+
return cursor;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.default = SingleChunkEmitter;
|
|
@@ -6,13 +6,12 @@ export default class ChunkingEmitterImpl {
|
|
|
6
6
|
protected chunkSize: number;
|
|
7
7
|
private log;
|
|
8
8
|
static Class?: new (options: ChunkingEmitterOptions) => ChunkingEmitter;
|
|
9
|
+
private emitters;
|
|
9
10
|
private totalErrors;
|
|
10
11
|
protected constructor(options: ChunkingEmitterOptions);
|
|
11
12
|
static Emitter(options: ChunkingEmitterOptions): Promise<ChunkingEmitter>;
|
|
12
13
|
emit(options: ChunkingEmitterEmitOptions): Promise<void>;
|
|
13
|
-
private emitChunk;
|
|
14
14
|
static reset(): void;
|
|
15
|
-
private Cursor;
|
|
16
15
|
getTotalErrors(): number;
|
|
17
16
|
}
|
|
18
17
|
interface ChunkingEmitterOptions {
|
|
@@ -25,6 +24,8 @@ export type ChunkingEmitterEmitOptions = {
|
|
|
25
24
|
cursor?: BatchCursor<Record<string, any>>;
|
|
26
25
|
payloadKey: string;
|
|
27
26
|
target?: Record<string, any>;
|
|
27
|
+
/** will stop any running operations that match the key */
|
|
28
|
+
uniqueKey?: string;
|
|
28
29
|
};
|
|
29
30
|
export interface ChunkingEmitter {
|
|
30
31
|
emit(options: ChunkingEmitterEmitOptions): Promise<void>;
|
|
@@ -7,19 +7,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
-
var m = o[Symbol.asyncIterator], i;
|
|
13
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
-
};
|
|
17
|
-
import { BatchArrayCursor } from '@sprucelabs/data-stores';
|
|
18
10
|
import { SchemaError, assertOptions } from '@sprucelabs/schema';
|
|
19
11
|
import { buildLog } from '@sprucelabs/spruce-skill-utils';
|
|
12
|
+
import SingleChunkEmitter from './SingleChunkEmitter.js';
|
|
20
13
|
export default class ChunkingEmitterImpl {
|
|
21
14
|
constructor(options) {
|
|
22
15
|
this.log = buildLog('ChunkingEmitter');
|
|
16
|
+
this.emitters = [];
|
|
23
17
|
this.totalErrors = 0;
|
|
24
18
|
const { client, chunkSize } = assertOptions(options, ['client']);
|
|
25
19
|
this.client = client;
|
|
@@ -33,9 +27,9 @@ export default class ChunkingEmitterImpl {
|
|
|
33
27
|
});
|
|
34
28
|
}
|
|
35
29
|
emit(options) {
|
|
36
|
-
var _a, e_1, _b, _c;
|
|
37
30
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
const {
|
|
31
|
+
const { items, cursor, uniqueKey: unqiueKey, } = assertOptions(options, ['eventName', 'payloadKey']);
|
|
32
|
+
this.totalErrors = 0;
|
|
39
33
|
if (!items && !cursor) {
|
|
40
34
|
throw new SchemaError({
|
|
41
35
|
code: 'MISSING_PARAMETERS',
|
|
@@ -43,67 +37,21 @@ export default class ChunkingEmitterImpl {
|
|
|
43
37
|
friendlyMessage: `You have to pass either 'items' or a 'cursor' to emit.`,
|
|
44
38
|
});
|
|
45
39
|
}
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const total = yield actualCursor.getTotalRecords();
|
|
50
|
-
try {
|
|
51
|
-
for (var _d = true, actualCursor_1 = __asyncValues(actualCursor), actualCursor_1_1; actualCursor_1_1 = yield actualCursor_1.next(), _a = actualCursor_1_1.done, !_a; _d = true) {
|
|
52
|
-
_c = actualCursor_1_1.value;
|
|
53
|
-
_d = false;
|
|
54
|
-
const chunk = _c;
|
|
55
|
-
current = yield this.emitChunk({
|
|
56
|
-
payloadKey,
|
|
57
|
-
chunk,
|
|
58
|
-
current,
|
|
59
|
-
total,
|
|
60
|
-
target,
|
|
61
|
-
eventName,
|
|
62
|
-
});
|
|
63
|
-
current++;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
67
|
-
finally {
|
|
68
|
-
try {
|
|
69
|
-
if (!_d && !_a && (_b = actualCursor_1.return)) yield _b.call(actualCursor_1);
|
|
70
|
-
}
|
|
71
|
-
finally { if (e_1) throw e_1.error; }
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
emitChunk(options) {
|
|
76
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
const { payloadKey, chunk, current, total, target, eventName } = options;
|
|
78
|
-
try {
|
|
79
|
-
let targetAndPayload = {
|
|
80
|
-
payload: {
|
|
81
|
-
[payloadKey]: chunk,
|
|
82
|
-
chunk: {
|
|
83
|
-
current,
|
|
84
|
-
total,
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
if (target) {
|
|
89
|
-
targetAndPayload.target = target;
|
|
90
|
-
}
|
|
91
|
-
yield this.client.emitAndFlattenResponses(eventName, targetAndPayload);
|
|
40
|
+
const match = this.emitters.find((e) => unqiueKey && e.matchesKey(unqiueKey));
|
|
41
|
+
if (match) {
|
|
42
|
+
yield match.kill();
|
|
92
43
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
44
|
+
const emitter = new SingleChunkEmitter(Object.assign(Object.assign({}, options), { chunkSize: this.chunkSize, client: this.client, log: this.log }));
|
|
45
|
+
this.emitters.push(emitter);
|
|
46
|
+
yield emitter.emit();
|
|
47
|
+
this.totalErrors += emitter.getTotalErrors();
|
|
48
|
+
this.emitters = this.emitters.filter((e) => e !== emitter);
|
|
49
|
+
return;
|
|
98
50
|
});
|
|
99
51
|
}
|
|
100
52
|
static reset() {
|
|
101
53
|
this.Class = undefined;
|
|
102
54
|
}
|
|
103
|
-
Cursor(items) {
|
|
104
|
-
const cursor = new BatchArrayCursor(items, { batchSize: this.chunkSize });
|
|
105
|
-
return cursor;
|
|
106
|
-
}
|
|
107
55
|
getTotalErrors() {
|
|
108
56
|
return this.totalErrors;
|
|
109
57
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BatchCursor } from '@sprucelabs/data-stores';
|
|
2
|
+
import { MercuryClient } from '@sprucelabs/mercury-client';
|
|
3
|
+
import { EventName } from '@sprucelabs/mercury-types';
|
|
4
|
+
import { Log } from '@sprucelabs/spruce-skill-utils';
|
|
5
|
+
export default class SingleChunkEmitter {
|
|
6
|
+
private cursor?;
|
|
7
|
+
private items?;
|
|
8
|
+
private payloadKey;
|
|
9
|
+
private target?;
|
|
10
|
+
private eventName;
|
|
11
|
+
private chunkSize;
|
|
12
|
+
private client;
|
|
13
|
+
private total;
|
|
14
|
+
private uniqueKey?;
|
|
15
|
+
private log;
|
|
16
|
+
totalErrors: number;
|
|
17
|
+
private isKilled;
|
|
18
|
+
constructor(options: SingleChunkEmitterOptions);
|
|
19
|
+
matchesKey(key: string): boolean;
|
|
20
|
+
getTotalErrors(): number;
|
|
21
|
+
emit(): Promise<void>;
|
|
22
|
+
kill(): Promise<void>;
|
|
23
|
+
private emitChunk;
|
|
24
|
+
private Cursor;
|
|
25
|
+
}
|
|
26
|
+
interface SingleChunkEmitterOptions {
|
|
27
|
+
cursor?: BatchCursor<Record<string, any>>;
|
|
28
|
+
items?: Record<string, any>[];
|
|
29
|
+
payloadKey: string;
|
|
30
|
+
target?: Record<string, any>;
|
|
31
|
+
eventName: EventName;
|
|
32
|
+
chunkSize: number;
|
|
33
|
+
client: MercuryClient;
|
|
34
|
+
uniqueKey?: string;
|
|
35
|
+
log: Log;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
+
var m = o[Symbol.asyncIterator], i;
|
|
13
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
+
};
|
|
17
|
+
import { BatchArrayCursor } from '@sprucelabs/data-stores';
|
|
18
|
+
export default class SingleChunkEmitter {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.totalErrors = 0;
|
|
21
|
+
this.isKilled = false;
|
|
22
|
+
const { cursor, items, payloadKey, target, eventName, chunkSize, client, uniqueKey, log, } = options;
|
|
23
|
+
this.cursor = cursor;
|
|
24
|
+
this.uniqueKey = uniqueKey;
|
|
25
|
+
this.items = items;
|
|
26
|
+
this.payloadKey = payloadKey;
|
|
27
|
+
this.target = target;
|
|
28
|
+
this.eventName = eventName;
|
|
29
|
+
this.chunkSize = chunkSize;
|
|
30
|
+
this.client = client;
|
|
31
|
+
this.log = log.buildLog('Emitter');
|
|
32
|
+
}
|
|
33
|
+
matchesKey(key) {
|
|
34
|
+
return this.uniqueKey === key;
|
|
35
|
+
}
|
|
36
|
+
getTotalErrors() {
|
|
37
|
+
return this.totalErrors;
|
|
38
|
+
}
|
|
39
|
+
emit() {
|
|
40
|
+
var _a, e_1, _b, _c;
|
|
41
|
+
var _d, _e;
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
let current = 0;
|
|
44
|
+
const actualCursor = (_d = this.cursor) !== null && _d !== void 0 ? _d : this.Cursor((_e = this.items) !== null && _e !== void 0 ? _e : []);
|
|
45
|
+
this.total = yield actualCursor.getTotalRecords();
|
|
46
|
+
try {
|
|
47
|
+
for (var _f = true, actualCursor_1 = __asyncValues(actualCursor), actualCursor_1_1; actualCursor_1_1 = yield actualCursor_1.next(), _a = actualCursor_1_1.done, !_a; _f = true) {
|
|
48
|
+
_c = actualCursor_1_1.value;
|
|
49
|
+
_f = false;
|
|
50
|
+
const chunk = _c;
|
|
51
|
+
try {
|
|
52
|
+
current = yield this.emitChunk({
|
|
53
|
+
chunk,
|
|
54
|
+
current,
|
|
55
|
+
});
|
|
56
|
+
if (this.isKilled) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
this.log.error('Failed to emit chunk', err);
|
|
62
|
+
this.totalErrors++;
|
|
63
|
+
}
|
|
64
|
+
current++;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
68
|
+
finally {
|
|
69
|
+
try {
|
|
70
|
+
if (!_f && !_a && (_b = actualCursor_1.return)) yield _b.call(actualCursor_1);
|
|
71
|
+
}
|
|
72
|
+
finally { if (e_1) throw e_1.error; }
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
kill() {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
this.isKilled = true;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
emitChunk(options) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
const { chunk, current } = options;
|
|
84
|
+
let targetAndPayload = {
|
|
85
|
+
payload: {
|
|
86
|
+
[this.payloadKey]: chunk,
|
|
87
|
+
chunk: {
|
|
88
|
+
current,
|
|
89
|
+
total: this.total,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
if (this.target) {
|
|
94
|
+
targetAndPayload.target = this.target;
|
|
95
|
+
}
|
|
96
|
+
yield this.client.emitAndFlattenResponses(this.eventName, targetAndPayload);
|
|
97
|
+
return current;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
Cursor(items) {
|
|
101
|
+
const cursor = new BatchArrayCursor(items, { batchSize: this.chunkSize });
|
|
102
|
+
return cursor;
|
|
103
|
+
}
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sprucelabs/mercury-chunking-emitter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"build/**/*",
|
|
6
6
|
"!build/__tests__",
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
"release": "semantic-release"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@sprucelabs/esm-postbuild": "^5.0.
|
|
43
|
-
"@sprucelabs/jest-json-reporter": "^7.0.
|
|
44
|
-
"@sprucelabs/resolve-path-aliases": "^1.1.
|
|
42
|
+
"@sprucelabs/esm-postbuild": "^5.0.86",
|
|
43
|
+
"@sprucelabs/jest-json-reporter": "^7.0.113",
|
|
44
|
+
"@sprucelabs/resolve-path-aliases": "^1.1.255",
|
|
45
45
|
"@sprucelabs/semantic-release": "^4.0.8",
|
|
46
|
-
"@sprucelabs/spruce-test-fixtures": "^60.1.
|
|
47
|
-
"@sprucelabs/test": "^8.0.
|
|
48
|
-
"@sprucelabs/test-utils": "^4.0.
|
|
46
|
+
"@sprucelabs/spruce-test-fixtures": "^60.1.14",
|
|
47
|
+
"@sprucelabs/test": "^8.0.23",
|
|
48
|
+
"@sprucelabs/test-utils": "^4.0.63",
|
|
49
49
|
"@types/node": "^20.8.9",
|
|
50
50
|
"chokidar-cli": "^3.0.0",
|
|
51
51
|
"concurrently": "^8.2.2",
|
|
@@ -86,12 +86,12 @@
|
|
|
86
86
|
}
|
|
87
87
|
},
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"@sprucelabs/data-stores": "^25.7.
|
|
90
|
-
"@sprucelabs/mercury-client": "^41.0.
|
|
91
|
-
"@sprucelabs/mercury-types": "^46.0.
|
|
92
|
-
"@sprucelabs/schema": "^29.0.
|
|
93
|
-
"@sprucelabs/spruce-core-schemas": "^39.0.
|
|
94
|
-
"@sprucelabs/spruce-event-utils": "^38.0.
|
|
95
|
-
"@sprucelabs/spruce-skill-utils": "^30.1.
|
|
89
|
+
"@sprucelabs/data-stores": "^25.7.1",
|
|
90
|
+
"@sprucelabs/mercury-client": "^41.0.118",
|
|
91
|
+
"@sprucelabs/mercury-types": "^46.0.103",
|
|
92
|
+
"@sprucelabs/schema": "^29.0.74",
|
|
93
|
+
"@sprucelabs/spruce-core-schemas": "^39.0.109",
|
|
94
|
+
"@sprucelabs/spruce-event-utils": "^38.0.123",
|
|
95
|
+
"@sprucelabs/spruce-skill-utils": "^30.1.7"
|
|
96
96
|
}
|
|
97
97
|
}
|