Skip to content

Commit fb54ff7

Browse files
committed
refactor ResultWrapper classes
1 parent 8270dd5 commit fb54ff7

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

packages/cubejs-backend-native/js/ResultWrapper.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface DataResult {
1212
getRawData(): any[];
1313
getTransformData(): any[];
1414
getRootResultObject(): any[];
15+
// eslint-disable-next-line no-use-before-define
16+
getResults(): ResultWrapper[];
1517
}
1618

1719
class BaseWrapper {
@@ -159,15 +161,19 @@ export class ResultWrapper extends BaseWrapper implements DataResult {
159161
public async getFinalResult(): Promise<any> {
160162
return getFinalQueryResult(this.transformData, this.getRawData()[0], this.rootResultObject);
161163
}
164+
165+
public getResults(): ResultWrapper[] {
166+
return [this];
167+
}
162168
}
163169

164-
export class ResultMultiWrapper extends BaseWrapper implements DataResult {
165-
public constructor(private readonly results: ResultWrapper[], private rootResultObject: any) {
170+
class BaseWrapperArray extends BaseWrapper {
171+
public constructor(protected readonly results: ResultWrapper[]) {
166172
super();
167173
}
168174

169-
public async getFinalResult(): Promise<any> {
170-
const [transformDataJson, rawDataRef, cleanResultList] = this.results.reduce<[Object[], any[], Object[]]>(
175+
protected getInternalDataArrays(): any[] {
176+
const [transformDataJson, rawData, resultDataJson] = this.results.reduce<[Object[], any[], Object[]]>(
171177
([transformList, rawList, resultList], r) => {
172178
transformList.push(r.getTransformData()[0]);
173179
rawList.push(r.getRawData()[0]);
@@ -177,13 +183,11 @@ export class ResultMultiWrapper extends BaseWrapper implements DataResult {
177183
[[], [], []]
178184
);
179185

180-
const responseDataObj = {
181-
queryType: this.rootResultObject.queryType,
182-
results: cleanResultList,
183-
slowQuery: this.rootResultObject.slowQuery,
184-
};
186+
return [transformDataJson, rawData, resultDataJson];
187+
}
185188

186-
return getFinalQueryResultMulti(transformDataJson, rawDataRef, responseDataObj);
189+
public getResults(): ResultWrapper[] {
190+
return this.results;
187191
}
188192

189193
public getTransformData(): any[] {
@@ -199,38 +203,33 @@ export class ResultMultiWrapper extends BaseWrapper implements DataResult {
199203
}
200204
}
201205

202-
// This is consumed by native side via Transport Bridge
203-
export class ResultArrayWrapper extends BaseWrapper implements DataResult {
204-
public constructor(private readonly results: ResultWrapper[]) {
205-
super();
206+
export class ResultMultiWrapper extends BaseWrapperArray implements DataResult {
207+
public constructor(results: ResultWrapper[], private rootResultObject: any) {
208+
super(results);
206209
}
207210

208211
public async getFinalResult(): Promise<any> {
209-
const [transformDataJson, rawData, resultDataJson] = this.results.reduce<[Object[], any[], Object[]]>(
210-
([transformList, rawList, resultList], r) => {
211-
transformList.push(r.getTransformData()[0]);
212-
rawList.push(r.getRawData()[0]);
213-
resultList.push(r.getRootResultObject()[0]);
214-
return [transformList, rawList, resultList];
215-
},
216-
[[], [], []]
217-
);
212+
const [transformDataJson, rawDataRef, cleanResultList] = this.getInternalDataArrays();
218213

219-
// It seems this is not needed anymore
220-
// return getFinalQueryResultArray(transformDataJson, rawData, resultDataJson);
214+
const responseDataObj = {
215+
queryType: this.rootResultObject.queryType,
216+
results: cleanResultList,
217+
slowQuery: this.rootResultObject.slowQuery,
218+
};
221219

222-
return [transformDataJson, rawData, resultDataJson];
220+
return getFinalQueryResultMulti(transformDataJson, rawDataRef, responseDataObj);
223221
}
222+
}
224223

225-
public getTransformData(): any[] {
226-
return this.results.map(r => r.getTransformData()[0]);
224+
// This is consumed by native side via Transport Bridge
225+
export class ResultArrayWrapper extends BaseWrapperArray implements DataResult {
226+
public constructor(results: ResultWrapper[]) {
227+
super(results);
227228
}
228229

229-
public getRawData(): any[] {
230-
return this.results.map(r => r.getRawData()[0]);
231-
}
230+
public async getFinalResult(): Promise<any> {
231+
const [transformDataJson, rawDataRef, cleanResultList] = this.getInternalDataArrays();
232232

233-
public getRootResultObject(): any[] {
234-
return this.results.map(r => r.getRootResultObject()[0]);
233+
return [transformDataJson, rawDataRef, cleanResultList];
235234
}
236235
}

0 commit comments

Comments
 (0)