Skip to content

Commit

Permalink
refactor: simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jun 13, 2020
1 parent 9444cae commit 3e0cc7b
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 118 deletions.
14 changes: 4 additions & 10 deletions src/GeoCollectionReference.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {GeoFirestoreTypes} from './GeoFirestoreTypes';
import {GeoDocumentReference} from './GeoDocumentReference';
import {GeoQuery} from './GeoQuery';
import {findCoordinates, encodeGeohash, encodeGeoDocument} from './utils';
import {encodeAddDocument} from './utils';

/**
* A `GeoCollectionReference` object can be used for adding documents, getting document references, and querying for documents (using the
Expand Down Expand Up @@ -60,15 +60,9 @@ export class GeoCollectionReference extends GeoQuery {
documentData: GeoFirestoreTypes.DocumentData,
customKey?: string
): Promise<GeoDocumentReference> {
if (Object.prototype.toString.call(documentData) === '[object Object]') {
const location = findCoordinates(documentData, customKey);
const geohash: string = encodeGeohash(location);
return (this._collection as GeoFirestoreTypes.cloud.CollectionReference)
.add(encodeGeoDocument(location, geohash, documentData))
.then(doc => new GeoDocumentReference(doc));
} else {
throw new Error('document must be an object');
}
return (this._collection as GeoFirestoreTypes.cloud.CollectionReference)
.add(encodeAddDocument(documentData, customKey))
.then(doc => new GeoDocumentReference(doc));
}

/**
Expand Down
32 changes: 9 additions & 23 deletions src/GeoDocumentReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ export class GeoDocumentReference {
) => () => void {
return (
onNext?: (snapshot: GeoDocumentSnapshot) => void,
onError?: (error: Error) => void
onError: (error: Error) => void = () => {}
) => {
return (this
._document as GeoFirestoreTypes.web.DocumentReference).onSnapshot(
snapshot => onNext(new GeoDocumentSnapshot(snapshot)),
error => {
if (onError) {
onError(error);
}
}
error => onError(error)
);
};
}
Expand Down Expand Up @@ -132,13 +128,10 @@ export class GeoDocumentReference {
get(
options: GeoFirestoreTypes.web.GetOptions = {source: 'default'}
): Promise<GeoDocumentSnapshot> {
return this._isWeb
? (this._document as GeoFirestoreTypes.web.DocumentReference)
.get(options)
.then(snapshot => new GeoDocumentSnapshot(snapshot))
: (this._document as GeoFirestoreTypes.cloud.DocumentReference)
.get()
.then(snapshot => new GeoDocumentSnapshot(snapshot));
const get = this._isWeb
? (this._document as GeoFirestoreTypes.web.DocumentReference).get(options)
: (this._document as GeoFirestoreTypes.web.DocumentReference).get();
return get.then(snapshot => new GeoDocumentSnapshot(snapshot));
}

/**
Expand All @@ -153,16 +146,9 @@ export class GeoDocumentReference {
| GeoFirestoreTypes.cloud.DocumentReference
| GeoFirestoreTypes.web.DocumentReference
): boolean {
if (other instanceof GeoDocumentReference) {
return (this
._document as GeoFirestoreTypes.cloud.DocumentReference).isEqual(
other['_document'] as GeoFirestoreTypes.cloud.DocumentReference
);
}
return (this
._document as GeoFirestoreTypes.cloud.DocumentReference).isEqual(
other as GeoFirestoreTypes.cloud.DocumentReference
);
const ref: any =
other instanceof GeoDocumentReference ? other['_document'] : other;
return this._document.isEqual(ref);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions src/GeoDocumentSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,8 @@ export class GeoDocumentSnapshot {
| GeoFirestoreTypes.cloud.DocumentSnapshot
| GeoFirestoreTypes.web.DocumentSnapshot
): boolean {
if (other instanceof GeoDocumentSnapshot) {
return (this
._snapshot as GeoFirestoreTypes.cloud.DocumentSnapshot).isEqual(
other['_snapshot'] as GeoFirestoreTypes.cloud.DocumentSnapshot
);
}
return (this._snapshot as GeoFirestoreTypes.cloud.DocumentSnapshot).isEqual(
other as GeoFirestoreTypes.cloud.DocumentSnapshot
);
const ref: any =
other instanceof GeoDocumentSnapshot ? other['_snapshot'] : other;
return this._snapshot.isEqual(ref);
}
}
2 changes: 1 addition & 1 deletion src/GeoFirestoreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '@types/node';
export namespace GeoFirestoreTypes {
export interface GeoDocumentData extends DocumentData {
g?: {
coordinates: web.GeoPoint | cloud.GeoPoint;
geopoint: web.GeoPoint | cloud.GeoPoint;
geohash: string;
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/GeoJoinerGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class GeoJoinerGet {
snapshot.docs.forEach(doc => {
const distance = calculateDistance(
this._queryCriteria.center,
doc.data().l
(doc.data() as GeoFirestoreTypes.GeoDocumentData).g.geopoint
);
if (this._queryCriteria.radius >= distance) {
this._docs.set(doc.id, doc);
Expand All @@ -42,7 +42,7 @@ export class GeoJoinerGet {
return {
distance: calculateDistance(
this._queryCriteria.center,
doc.data().l
(doc.data() as GeoFirestoreTypes.GeoDocumentData).g.geopoint
),
id: doc.id,
};
Expand Down
6 changes: 3 additions & 3 deletions src/GeoJoinerOnSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export class GeoJoinerOnSnapshot {
private _queries: GeoFirestoreTypes.web.Query[],
private _queryCriteria: GeoFirestoreTypes.QueryCriteria,
private _onNext: (snapshot: GeoQuerySnapshot) => void,
private _onError?: (error: Error) => void
private _onError: (error: Error) => void = () => {}
) {
validateQueryCriteria(_queryCriteria);
this._queriesResolved = new Array(_queries.length).fill(0);
_queries.forEach((value: GeoFirestoreTypes.web.Query, index: number) => {
_queries.forEach((value: GeoFirestoreTypes.web.Query, index) => {
const subscription = value.onSnapshot(
snapshot => this._processSnapshot(snapshot, index),
error => (this._error = error)
Expand Down Expand Up @@ -145,7 +145,7 @@ export class GeoJoinerOnSnapshot {
*/
private _emit(): void {
if (this._error) {
if (this._onError) this._onError(this._error);
this._onError(this._error);
this.unsubscribe()();
} else if (this._newValues && this._firstRoundResolved) {
this._newValues = false;
Expand Down
2 changes: 1 addition & 1 deletion src/GeoQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class GeoQuery {
) => () => void {
return (
onNext: (snapshot: GeoQuerySnapshot) => void,
onError?: (error: Error) => void
onError: (error: Error) => void = () => {}
): (() => void) => {
if (this._center && typeof this._radius === 'number') {
return new GeoJoinerOnSnapshot(
Expand Down
2 changes: 1 addition & 1 deletion src/GeoQuerySnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class GeoQuerySnapshot {

/** True if there are no documents in the GeoQuerySnapshot. */
get empty(): boolean {
return this._docs.length ? false : true;
return !this._docs.length;
}

/**
Expand Down
32 changes: 18 additions & 14 deletions src/GeoTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ export class GeoTransaction {
| GeoFirestoreTypes.cloud.DocumentReference
| GeoFirestoreTypes.web.DocumentReference
): GeoTransaction {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
(this._transaction as GeoFirestoreTypes.cloud.Transaction).delete(ref);
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
this._transaction.delete(ref);
return this;
}

Expand All @@ -65,12 +66,13 @@ export class GeoTransaction {
| GeoFirestoreTypes.cloud.DocumentReference
| GeoFirestoreTypes.web.DocumentReference
): Promise<GeoDocumentSnapshot> {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
return (this._transaction as GeoFirestoreTypes.cloud.Transaction)
.get(ref)
.then(snpashot => new GeoDocumentSnapshot(snpashot));
.then((snpashot: any) => new GeoDocumentSnapshot(snpashot));
}

/**
Expand All @@ -91,9 +93,10 @@ export class GeoTransaction {
documentData: GeoFirestoreTypes.DocumentData,
options?: GeoFirestoreTypes.SetOptions
): GeoTransaction {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
(this._transaction as GeoFirestoreTypes.cloud.Transaction).set(
ref,
encodeSetDocument(documentData, options),
Expand All @@ -120,9 +123,10 @@ export class GeoTransaction {
data: GeoFirestoreTypes.UpdateData,
customKey?: string
): GeoTransaction {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
(this._transaction as GeoFirestoreTypes.cloud.Transaction).update(
ref,
encodeUpdateDocument(data, customKey)
Expand Down
21 changes: 12 additions & 9 deletions src/GeoWriteBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ export class GeoWriteBatch {
documentData: GeoFirestoreTypes.DocumentData,
options?: GeoFirestoreTypes.SetOptions
): GeoWriteBatch {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
(this._writeBatch as GeoFirestoreTypes.cloud.WriteBatch).set(
ref,
encodeSetDocument(documentData, options),
Expand All @@ -84,9 +85,10 @@ export class GeoWriteBatch {
data: GeoFirestoreTypes.UpdateData,
customKey?: string
): GeoWriteBatch {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
(this._writeBatch as GeoFirestoreTypes.cloud.WriteBatch).update(
ref,
encodeUpdateDocument(data, customKey)
Expand All @@ -106,9 +108,10 @@ export class GeoWriteBatch {
| GeoFirestoreTypes.cloud.DocumentReference
| GeoFirestoreTypes.web.DocumentReference
): GeoWriteBatch {
const ref = (documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef) as GeoFirestoreTypes.cloud.DocumentReference;
const ref: any =
documentRef instanceof GeoDocumentReference
? documentRef['_document']
: documentRef;
(this._writeBatch as GeoFirestoreTypes.cloud.WriteBatch).delete(ref);
return this;
}
Expand Down
Loading

0 comments on commit 3e0cc7b

Please sign in to comment.