Skip to content

Commit

Permalink
fix(arcgis-rest-feature-layer): streamline promises
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman committed Jun 17, 2021
1 parent 1d3d2fa commit ebb8380
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions packages/arcgis-rest-feature-layer/src/decodeValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ export interface IDecodeValuesOptions extends IRequestOptions {
export function decodeValues(
requestOptions: IDecodeValuesOptions
): Promise<IQueryFeaturesResponse> {
return new Promise(resolve => {
if (!requestOptions.fields) {
// tslint:disable-next-line:no-floating-promises
getLayer({ url: requestOptions.url }).then(
(metadata: ILayerDefinition) => {
resolve((requestOptions.fields = metadata.fields));
}
);
} else {
resolve(requestOptions.fields);
}
}).then(fields => {
let prms;
if (requestOptions.fields) {
prms = Promise.resolve(requestOptions.fields);
} else {
prms = getLayer({ url: requestOptions.url }).then(
(metadata: ILayerDefinition) => {
return metadata.fields;
}
);
}
return prms.then((fields) => {
// extract coded value domains
const domains = extractCodedValueDomains(fields as IField[]);
if (Object.keys(domains).length < 1) {
Expand All @@ -98,22 +97,19 @@ export function decodeValues(
// merge decoded features into the response
return {
...requestOptions.queryResponse,
...{ features: decodedFeatures }
...{ features: decodedFeatures },
};
});
}

function extractCodedValueDomains(fields: IField[]) {
return fields.reduce(
(domains, field) => {
const domain = field.domain;
if (domain && domain.type === "codedValue") {
domains[field.name] = domain;
}
return domains;
},
{} as { [index: string]: any }
);
return fields.reduce((domains, field) => {
const domain = field.domain;
if (domain && domain.type === "codedValue") {
domains[field.name] = domain;
}
return domains;
}, {} as { [index: string]: any });
}

// TODO: add type for domain?
Expand Down

0 comments on commit ebb8380

Please sign in to comment.