Skip to content

Commit c0fc9d8

Browse files
committed
fix: allow aggregation callback to return a Promise
1 parent 2fbbf4f commit c0fc9d8

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/aggregation/aggregate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getCommonIds,
1616
getLastSeqId
1717
} from '../mongo/models/source';
18-
import { IAggregationCallback } from '../types';
18+
import { IAggregationCallback, MaybePromiseBool } from '../types';
1919
import { debugUtil } from '../util/debug';
2020

2121
const debug = debugUtil('aggregation');
@@ -47,8 +47,8 @@ export async function aggregate(conf: IAggregationConfigElement) {
4747
maxSeqIds[sourceName] = lastCid
4848
? lastCid.sequentialID
4949
: lastSourceSeq
50-
? lastSourceSeq.sequentialID
51-
: 0;
50+
? lastSourceSeq.sequentialID
51+
: 0;
5252
const cids = ids.map((cid) => cid.commonID);
5353
commonIds = commonIds.concat(cids);
5454
}
@@ -116,10 +116,10 @@ async function aggregateValue(
116116
commonId: string
117117
) {
118118
const result = {};
119-
let accept: void | boolean = true;
119+
let accept: MaybePromiseBool = true;
120120
for (const key in filter) {
121121
if (data[key]) {
122-
accept = await Promise.resolve(
122+
accept = await Promise.resolve<MaybePromiseBool>(
123123
filter[key].call(
124124
null,
125125
data[key].map((d) => d.data),

src/types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ export interface IAggregationConfigFile<AggregationResultType = any> {
5656
chunkSize?: number;
5757
}
5858

59-
export type IAggregationCallback<
60-
SourceDataType extends object = any,
61-
AggregationResult = any
59+
export type MaybePromiseBool =
60+
| void
61+
| boolean
62+
| Promise<void>
63+
| Promise<boolean>;
64+
65+
export type IAggregationCallback <
66+
SourceDataType extends object = any ,
67+
AggregationResult = any
6268
> = (
6369
data: SourceDataType[],
6470
result: AggregationResult,
6571
commonID: string,
6672
ids: string[]
67-
) => void | boolean;
73+
) => MaybePromiseBool;

0 commit comments

Comments
 (0)