Skip to content

Commit

Permalink
Merge pull request #30 from 0x80/topic/0430-last-bits
Browse files Browse the repository at this point in the history
Wrap up 2.0 release
  • Loading branch information
0x80 authored May 9, 2022
2 parents 0a03b27 + 1126305 commit 649cc9a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firestore-mobx",
"version": "2.0.0-13",
"version": "2.0.0-14",
"description": "Observable Firestore documents and collections using MobX",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
12 changes: 3 additions & 9 deletions src/__test/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { autorun, configure } from "mobx";
import { autorun, configure, toJS } from "mobx";
import { ObservableDocument } from "../document";
import { first } from "../utils";
import {
Expand Down Expand Up @@ -41,7 +41,8 @@ describe("Document", () => {
expect(document.id).toBe("__no_id");
expect(document.isLoading).toBe(false);
expect(document.hasData).toBe(false);
expect(document.data).toBe(undefined);
expect(() => document.data).toThrow();
expect(() => document.document).toThrow();
});

it("Can observe a document by ref", async () => {
Expand All @@ -54,7 +55,6 @@ describe("Document", () => {

expect(document.isLoading).toBe(true);
expect(document.hasData).toBe(false);
expect(document.data).toBeUndefined();

await document.ready().then((doc) => console.log(doc));

Expand All @@ -78,7 +78,6 @@ describe("Document", () => {

expect(document.isLoading).toBe(true);
expect(document.hasData).toBe(false);
expect(document.data).toBeUndefined();

const disposeListeners = autorun(() => {
console.log("isLoading", document.isLoading);
Expand Down Expand Up @@ -106,7 +105,6 @@ describe("Document", () => {

expect(document.isLoading).toBe(false);
expect(document.hasData).toBe(false);
expect(document.data).toBeUndefined();

document.attachTo(first(snapshot.docs)?.id);

Expand All @@ -132,9 +130,6 @@ describe("Document", () => {

expect(document.isLoading).toBe(false);
expect(document.hasData).toBe(false);
expect(document.data).toBeUndefined();

// consoleInspect('snapshot', snapshot.docs.map(doc => doc.data))

document.attachTo(first(snapshot.docs)?.id);

Expand All @@ -154,7 +149,6 @@ describe("Document", () => {

const data = await document.ready();

// consoleInspect('data', doc?.data)
expect(data).toEqual(first(collectionData));

disposeListeners();
Expand Down
27 changes: 16 additions & 11 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class ObservableCollection<T> {
* if the current listeners belong to that combination or we need to update
* them
*/
public constructor(
constructor(
/**
* Ref is optional because for sub-collections you might not know the full
* path in advance. Pass undefined if you want to supply the other
Expand All @@ -75,6 +75,11 @@ export class ObservableCollection<T> {
isEmpty: computed,
hasDocuments: computed,
attachTo: action,
/**
* attachTo being an action doesn't seem to be sufficient to prevent
* strict mode errors
*/
_changeSource: action,
});

this.initializeReadyPromise();
Expand Down Expand Up @@ -109,35 +114,35 @@ export class ObservableCollection<T> {
}
}

public get isEmpty(): boolean {
get isEmpty(): boolean {
return this.documents.length === 0;
}

public get hasDocuments(): boolean {
get hasDocuments(): boolean {
return this.documents.length > 0;
}

private get isObserved(): boolean {
return this.observedCount > 0;
}

public get path(): string | undefined {
get path(): string | undefined {
return this.collectionRef ? this.collectionRef.path : undefined;
}

public get ref(): FirebaseFirestore.CollectionReference | undefined {
get ref(): FirebaseFirestore.CollectionReference | undefined {
return this.collectionRef;
}

public attachTo(newRef: FirebaseFirestore.CollectionReference | undefined) {
this.changeSource(newRef);
attachTo(newRef: FirebaseFirestore.CollectionReference | undefined) {
this._changeSource(newRef);
/**
* Return this so we can chain ready()
*/
return this;
}

private changeSource(newRef?: FirebaseFirestore.CollectionReference) {
_changeSource(newRef?: FirebaseFirestore.CollectionReference) {
if (!this.collectionRef && !newRef) {
// this.logDebug("Ignore change source");
return;
Expand Down Expand Up @@ -178,7 +183,7 @@ export class ObservableCollection<T> {
}
}

public async add(data: T) {
async add(data: T) {
if (!hasReference(this.collectionRef)) {
this.handleError(
new Error(`Can not add a document to a collection that has no ref`),
Expand All @@ -191,7 +196,7 @@ export class ObservableCollection<T> {
return this.collectionRef.add(data);
}

public ready() {
ready() {
const isListening = !!this.onSnapshotUnsubscribeFn;

if (!isListening) {
Expand Down Expand Up @@ -342,7 +347,7 @@ export class ObservableCollection<T> {
});
}

public set query(queryCreatorFn: QueryCreatorFn | undefined) {
set query(queryCreatorFn: QueryCreatorFn | undefined) {
this.logDebug("Set query");

this.queryCreatorFn = queryCreatorFn;
Expand Down
Loading

0 comments on commit 649cc9a

Please sign in to comment.