Skip to content

Commit

Permalink
Optimize response row
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Aug 17, 2021
1 parent a588a3d commit ba6f9a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/ResponseRow.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VisibilityStructure } from './VisibilityCalculator';
import EntityRow from './EntityRow';
import { PromiseExprEvaluatorRow, Schema, Row } from 'mwater-expressions';
import { ResponseData } from './response';
Expand Down Expand Up @@ -30,6 +31,8 @@ export default class ResponseRow implements PromiseExprEvaluatorRow {
submittedOn?: string;
/** Optional code */
code?: string;
/** Cached visibility structure, needed when visible is queried */
visibilityStructure?: VisibilityStructure;
constructor(options: {
/** data of entire response */
responseData: ResponseData;
Expand Down
6 changes: 5 additions & 1 deletion lib/ResponseRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class ResponseRow {
}
// Visible
if (parts.length === 3 && parts[2] === "visible") {
if (this.visibilityStructure) {
return this.visibilityStructure[parts[1]];
}
const visibilityCalculator = new VisibilityCalculator_1.default(this.formDesign, this.schema);
const visibilityStructure = yield new Promise((resolve, reject) => {
visibilityCalculator.createVisibilityStructure(this.responseData, this, (error, visibilityStructure) => {
Expand All @@ -119,7 +122,8 @@ class ResponseRow {
}
});
});
return visibilityStructure[parts[1]];
this.visibilityStructure = visibilityStructure;
return this.visibilityStructure[parts[1]];
}
// Simple values
if (parts.length === 3 && parts[2] === "value") {
Expand Down
11 changes: 10 additions & 1 deletion src/ResponseRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default class ResponseRow implements PromiseExprEvaluatorRow {
/** Optional code */
code?: string

/** Cached visibility structure, needed when visible is queried */
visibilityStructure?: VisibilityStructure

// Create a response row from a response data object.
// Options:
// responseData: data of entire response
Expand Down Expand Up @@ -184,6 +187,10 @@ export default class ResponseRow implements PromiseExprEvaluatorRow {

// Visible
if (parts.length === 3 && parts[2] === "visible") {
if (this.visibilityStructure) {
return this.visibilityStructure[parts[1]]
}

const visibilityCalculator = new VisibilityCalculator(this.formDesign, this.schema)
const visibilityStructure = await new Promise<VisibilityStructure>((resolve, reject) => {
visibilityCalculator.createVisibilityStructure(this.responseData, this, (error, visibilityStructure) => {
Expand All @@ -195,7 +202,9 @@ export default class ResponseRow implements PromiseExprEvaluatorRow {
}
})
})
return visibilityStructure[parts[1]]
this.visibilityStructure = visibilityStructure

return this.visibilityStructure[parts[1]]
}

// Simple values
Expand Down

0 comments on commit ba6f9a7

Please sign in to comment.