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

Expose Firebase App and Firestore database to public #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Reads the document referred to by this <a href="#documnentquery">DocumnentQuery<
updateDocument<T>(options: UpdateDocument<T>) => Promise<void>
```

Updates fields in the document referred to by the specified <a href="#documnentquery">DocumnentQuery</a>.
Updates fields in the document referred to by the specified <a href="#documnentquery">DocumnentQuery</a>.
The update will fail if applied to a document that does not exist.

| Param | Type |
Expand All @@ -130,8 +130,8 @@ The update will fail if applied to a document that does not exist.
setDocument<T>(options: SetDocument<T>) => Promise<void>
```

Writes to the document referred to by the specified <a href="#documnentquery">DocumnentQuery</a>.
If the document does not yet exist, it will be created.
Writes to the document referred to by the specified <a href="#documnentquery">DocumnentQuery</a>.
If the document does not yet exist, it will be created.
If you provide merge or mergeFields, the provided data can be merged into an existing document.

| Param | Type |
Expand Down Expand Up @@ -162,7 +162,7 @@ Deletes the document referred to by the specified <a href="#documnentquery">Docu
addDocument<T>(options: AddDocument<T>) => Promise<DocumentReference>
```

Add a new document to specified <a href="#collectionquery">`CollectionQuery`</a> with the given data,
Add a new document to specified <a href="#collectionquery">`CollectionQuery`</a> with the given data,
assigning it a document ID automatically.

| Param | Type |
Expand Down Expand Up @@ -259,7 +259,7 @@ Remove all active snapshot listners
enableNetwork() => Promise<void>
```

Re-enables use of the network for this Firestore instance after a prior
Re-enables use of the network for this Firestore instance after a prior
call to {@link disableNetwork}.

--------------------
Expand All @@ -271,9 +271,9 @@ call to {@link disableNetwork}.
disableNetwork() => Promise<void>
```

Disables network usage for this instance. It can be re-enabled via {@link enableNetwork}.
While the network is disabled, any snapshot listeners, {@link getDocument}
or {@link getCollection} calls will return results from cache, and any write
Disables network usage for this instance. It can be re-enabled via {@link enableNetwork}.
While the network is disabled, any snapshot listeners, {@link getDocument}
or {@link getCollection} calls will return results from cache, and any write
operations will be queued until the network is restored.

--------------------
Expand Down Expand Up @@ -365,7 +365,7 @@ operations will be queued until the network is restored.

#### QueryConstraint

A <a href="#queryconstraint">`QueryConstraint`</a> is used to narrow the set of documents returned by a
A <a href="#queryconstraint">`QueryConstraint`</a> is used to narrow the set of documents returned by a
Firestore query.

| Prop | Type | Description |
Expand Down Expand Up @@ -404,7 +404,7 @@ Make all properties in T optional

#### QueryOperators

Filter conditions in a {@link <a href="#queryconstraint">QueryConstraint</a>} clause are specified using the
Filter conditions in a {@link <a href="#queryconstraint">QueryConstraint</a>} clause are specified using the
strings '&lt;', '&lt;=', '==', '&gt;=', '&gt;', 'array-contains'

<code>"==" | "&gt;=" | "&lt;=" | "&lt;" | "&gt;" | "array-contains"</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,12 @@ public Task<Void> enableNetwork() {
public Task<Void> disableNetwork() {
return this.db.enableNetwork();
}

public FirebaseApp getApp() {
return this.app;
}

public FirebaseFirestore getFirestore() {
return this.db;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CapacitorFirestorePlugin extends Plugin {

private CapacitorFirestore implementation = new CapacitorFirestore();
private Map<String, ListenerRegistration> listeners = new HashMap<>();
private ArrayList<String> resolvedListeners = new ArrayList<>();
private int pendingActions = 0;

@Override
Expand Down Expand Up @@ -91,6 +92,7 @@ public void clearAllSnapshotListeners(PluginCall call) {
listener.remove();
}

resolvedListeners.addAll(listeners.keySet());
listeners = new HashMap<>();

call.resolve();
Expand All @@ -105,6 +107,11 @@ public void removeSnapshotListener(PluginCall call) {
return;
}

if (resolvedListeners.contains(callbackId)) {
call.resolve();
return;
}

ListenerRegistration listener = listeners.get(callbackId);

if (listener == null) {
Expand All @@ -114,6 +121,7 @@ public void removeSnapshotListener(PluginCall call) {

listener.remove();
listeners.remove(callbackId);
resolvedListeners.add(callbackId);
call.resolve();
}

Expand Down Expand Up @@ -412,6 +420,21 @@ public void disableNetwork(PluginCall call) {
);
}

@PluginMethod
public void getApp(PluginCall call) {
JSObject object = new JSObject();
object.put("app", implementation.getApp());
call.resolve(object);
}

@PluginMethod
public void getApp(PluginCall call) {
implementation.getApp();
JSObject object = new JSObject();
object.put("firestore", implementation.getFirestore());
call.resolve(object);
}

private HashMap<String, Object> mapJSObject(JSONObject jsObject) throws JSONException {
HashMap<String, Object> mapData = new HashMap<>();
Iterator<String> keys = jsObject.keys();
Expand Down
14 changes: 14 additions & 0 deletions ios/Plugin/CapacitorFirestore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ enum CapacitorFirestoreError: Error {
self.db?.settings = settings
}
}


@objc public func getApp() -> JSObject {
var result = JSObject()
result["app"] = self.app
return result
}


@objc public func getFirestore() -> JSObject {
var result = JSObject()
result["firestore"] = self.db
return result
}

private func configure() throws {
if self.app != nil {
Expand Down
2 changes: 2 additions & 0 deletions ios/Plugin/CapacitorFirestorePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
CAP_PLUGIN_METHOD(getCollection, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(enableNetwork, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(disableNetwork, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getApp, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getFirestore, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addDocumentSnapshotListener, CAPPluginReturnCallback);
CAP_PLUGIN_METHOD(addCollectionSnapshotListener, CAPPluginReturnCallback);
)
16 changes: 12 additions & 4 deletions ios/Plugin/CapacitorFirestorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import FirebaseFirestore
public class CapacitorFirestorePlugin: CAPPlugin {
private let implementation = CapacitorFirestore()
private var listeners: [ String: ListenerRegistration ] = [:]
private var resolvedListenders: [String] = []
private var pendingActions: Int = 0

override public func load() {
Expand Down Expand Up @@ -74,14 +75,15 @@ public class CapacitorFirestorePlugin: CAPPlugin {
call.reject("Unknown error", nil, error, [:])
}
}

@objc func clearAllSnapshotListeners(_ call: CAPPluginCall) {
listeners.values.forEach { ListenerRegistration in
ListenerRegistration.remove();
ListenerRegistration.remove()
}


resolvedListenders.append(contentsOf: listeners.keys)
listeners = [:]

call.resolve()
}

Expand All @@ -93,6 +95,11 @@ public class CapacitorFirestorePlugin: CAPPlugin {
return
}

if resolvedListenders.contains(callbackId!) {
call.resolve()
return
}

let listener = listeners[callbackId!]

if listener == nil {
Expand All @@ -101,6 +108,7 @@ public class CapacitorFirestorePlugin: CAPPlugin {
}

listener?.remove()
resolvedListenders.append(callbackId!)
call.resolve()
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@proteansoftware/capacitor-firestore",
"version": "4.0.0",
"version": "4.1.0",
"description": "Capacitor Plugin for Native Firestore",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down
37 changes: 18 additions & 19 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable no-prototype-builtins */
/// <reference types="@capacitor/cli" />

import type { FirebaseApp } from "@firebase/app";
import type { Firestore } from "@firebase/firestore";
import { Timestamp } from "firebase/firestore";

declare module "@capacitor/cli" {
Expand Down Expand Up @@ -51,11 +53,7 @@ export type QueryOperators = "==" | ">=" | "<=" | "<" | ">" | "array-contains";
* @param value The value for comparison
* @returns The created {@link QueryConstraint}.
*/
export function createQueryConstraint(
field: string,
operator: QueryOperators,
value: any,
): QueryConstraint {
export function createQueryConstraint(field: string, operator: QueryOperators, value: any): QueryConstraint {
return {
fieldPath: field,
opStr: operator,
Expand All @@ -66,7 +64,7 @@ export function createQueryConstraint(
export function prepDataForFirestore<T>(data: T): T {
for (const prop in data) {
if (data[prop] instanceof Timestamp) {
const timestamp = (data[prop] as unknown) as Timestamp;
const timestamp = data[prop] as unknown as Timestamp;
data[prop] = {
specialType: "Timestamp",
seconds: timestamp.seconds,
Expand Down Expand Up @@ -204,15 +202,9 @@ export interface PendingActions {
count: number;
}

export type DocumentSnapshotCallback<T> = (
data: DocumentSnapshot<T> | null,
err?: any,
) => void;
export type DocumentSnapshotCallback<T> = (data: DocumentSnapshot<T> | null, err?: any) => void;

export type CollectionSnapshotCallback<T> = (
data: CollectionSnapshot<T> | null,
err?: any,
) => void;
export type CollectionSnapshotCallback<T> = (data: CollectionSnapshot<T> | null, err?: any) => void;

export interface CapacitorFirestorePlugin {
/**
Expand Down Expand Up @@ -287,10 +279,7 @@ export interface CapacitorFirestorePlugin {
* @param callback
* @returns The callback id which can be used to remove the listener.
*/
addDocumentSnapshotListener<T>(
options: DocumnentQuery,
callback: DocumentSnapshotCallback<T>,
): Promise<CallbackId>;
addDocumentSnapshotListener<T>(options: DocumnentQuery, callback: DocumentSnapshotCallback<T>): Promise<CallbackId>;

/**
* Executes the query and returns the results as a CollectionSnapshot
Expand All @@ -307,7 +296,7 @@ export interface CapacitorFirestorePlugin {
*/
addCollectionSnapshotListener<T>(
options: CollectionQuery,
callback: CollectionSnapshotCallback<T>,
callback: CollectionSnapshotCallback<T>
): Promise<CallbackId>;

/**
Expand Down Expand Up @@ -338,4 +327,14 @@ export interface CapacitorFirestorePlugin {
* @returns A `Promise` that is resolved once the network has been disabled.
*/
disableNetwork(): Promise<void>;

/**
* Gets the current Firebase App
*/
getApp(): { app: FirebaseApp | null; };

/**
* Gets the current Firestore database
*/
getFirestore(): { firestore: Firestore | null; };
}
22 changes: 5 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { registerPlugin } from "@capacitor/core";

import {
createQueryConstraint,
prepDataForFirestore,
processDocumentData,
} from "./definitions";
import { createQueryConstraint, prepDataForFirestore, processDocumentData } from "./definitions";
import type { CapacitorFirestorePlugin } from "./definitions";

const CapacitorFirestore = registerPlugin<CapacitorFirestorePlugin>(
"CapacitorFirestore",
{
web: () => import("./web").then(m => new m.CapacitorFirestoreWeb()),
},
);
const CapacitorFirestore = registerPlugin<CapacitorFirestorePlugin>("CapacitorFirestore", {
web: () => import("./web").then((m) => new m.CapacitorFirestoreWeb()),
});

export * from "./definitions";
export {
CapacitorFirestore,
createQueryConstraint,
prepDataForFirestore,
processDocumentData,
};
export { CapacitorFirestore, createQueryConstraint, prepDataForFirestore, processDocumentData };
Loading