Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: liveQuery support for unsorted distance queries #8221

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,40 @@ describe('ParseLiveQuery', function () {
]);
});

it('can subscribe to query and return object with withinKilometers with last parameter on update', async done => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
const object = new TestObject();
const firstPoint = new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 });
object.set({ location: firstPoint });
await object.save();

// unsorted will use $centerSphere operator
const sorted = false;
const query = new Parse.Query(TestObject);
query.withinKilometers(
'location',
new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 }),
2,
sorted
);
const subscription = await query.subscribe();
subscription.on('update', obj => {
expect(obj.id).toBe(object.id);
done();
});

const secondPoint = new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 });
object.set({ location: secondPoint });
await object.save();
});

afterEach(async function (done) {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.close();
Expand Down
22 changes: 19 additions & 3 deletions src/LiveQuery/QueryTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,25 @@ function matchesKeyConstraints(object, key, constraints) {
return true;
}
case '$geoWithin': {
const points = compareTo.$polygon.map(geoPoint => [geoPoint.latitude, geoPoint.longitude]);
const polygon = new Parse.Polygon(points);
return polygon.containsPoint(object[key]);
if (compareTo.$polygon) {
const points = compareTo.$polygon.map(geoPoint => [
geoPoint.latitude,
geoPoint.longitude,
]);
const polygon = new Parse.Polygon(points);
return polygon.containsPoint(object[key]);
}
if (compareTo.$centerSphere) {
const [WGS84Point, maxDistance] = compareTo.$centerSphere;
const centerPoint = new Parse.GeoPoint({
latitude: WGS84Point[1],
longitude: WGS84Point[0],
});
const point = new Parse.GeoPoint(object[key]);
const distance = point.radiansTo(centerPoint);
return distance <= maxDistance;
}
break;
}
case '$geoIntersects': {
const polygon = new Parse.Polygon(object[key].coordinates);
Expand Down