Skip to content

Commit

Permalink
socket: Let the geojsonCollection socket route return a Status
Browse files Browse the repository at this point in the history
The Status object is the preferred way to communicate between server and
client and handle error and proper response unwrapping.
  • Loading branch information
tahini committed Mar 22, 2024
1 parent b08cd5b commit 2278c72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import GenericMapObjectCollection from '../../utils/objects/GenericMapObjectColl
import { GenericAttributes, GenericObject } from '../../utils/objects/GenericObject';
import { isProgressable } from '../../utils/objects/Progressable';
import TrError from '../../utils/TrError';
import * as Status from '../../utils/Status';

/**
*
Expand Down Expand Up @@ -159,12 +160,17 @@ const loadGeojsonFromServer = function (
socket.emit(
socketEventName ? socketEventName : `${socketPrefix}.geojsonCollection`,
{ dataSourceIds, sampleSize, format: 'geobuf' },
(geojsonResponse) => {
if ((geojsonResponse && geojsonResponse.geojson) || geojsonResponse.geobuf) {
//console.log(`parsing ${geojsonResponse.geobuf ? 'geobuf' : 'geojson'}`);
const geojson = geojsonResponse.geobuf
? geobuf.decode(new Pbf(geojsonResponse.geobuf))
: geojsonResponse.geojson;
(
responseStatus: Status.Status<
{ type: 'geojson'; geojson: GeoJSON.FeatureCollection } | { type: 'geobuf'; geobuf: Buffer }
>
) => {
if (Status.isStatusOk(responseStatus)) {
const geojsonResponse = Status.unwrap(responseStatus);
const geojson =
geojsonResponse.type === 'geobuf'
? geobuf.decode(new Pbf(geojsonResponse.geobuf))
: geojsonResponse.geojson;
if (
geojson &&
geojson.features &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { GenericAttributes } from 'chaire-lib-common/lib/utils/objects/GenericOb
import * as Status from 'chaire-lib-common/lib/utils/Status';
import TrError from 'chaire-lib-common/lib/utils/TrError';
import { isSocketIo } from '../../api/socketUtils';
import { FeatureCollection } from '@turf/turf';

interface TransitObjectDataHandler {
lowerCaseName: string;
Expand All @@ -43,7 +44,9 @@ interface TransitObjectDataHandler {
read: (id: string, customCachePath: string | undefined) => Promise<Record<string, any>>;
update: (socket: EventEmitter, id: string, attributes: GenericAttributes) => Promise<Record<string, any>>;
delete: (socket: EventEmitter, id: string, customCachePath: string | undefined) => Promise<Record<string, any>>;
geojsonCollection?: (params?) => Promise<Record<string, any>>;
geojsonCollection?: (
params?
) => Promise<Status.Status<{ type: 'geojson'; geojson: FeatureCollection } | { type: 'geobuf'; geobuf: Buffer }>>;
collection?: (dataSourceId) => Promise<Record<string, any>>;
saveCache?: (attributes) => Promise<Record<string, any>>;
deleteCache?: (id: string, customCachePath: string | undefined) => Promise<Record<string, any>>;
Expand Down Expand Up @@ -249,13 +252,13 @@ function createDataHandlers(): Record<string, TransitObjectDataHandler> {
const geojson = await transitClassConfig.dbQueries.geojsonCollection(params);
if (params.format === 'geobuf') {
const geobufjson = Buffer.from(geobuf.encode(geojson, new Pbf()));
return { geobuf: geobufjson };
return Status.createOk({ type: 'geobuf', geobuf: geobufjson });
} else {
return { geojson };
return Status.createOk({ type: 'geojson', geojson });
}
} catch (error) {
console.error(error);
return TrError.isTrError(error) ? error.export() : { error };
return Status.createError(TrError.isTrError(error) ? error.export() : { error });
}
};
}
Expand Down

0 comments on commit 2278c72

Please sign in to comment.