Skip to content

Commit 23e59ca

Browse files
Merge e637c5f into dd562b0
2 parents dd562b0 + e637c5f commit 23e59ca

File tree

7 files changed

+1247
-52
lines changed

7 files changed

+1247
-52
lines changed

packages/database/src/core/view/Event.ts

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export interface Event {
3636
toString(): string;
3737
}
3838

39+
/**
40+
* One of the following strings: "value", "child_added", "child_changed",
41+
* "child_removed", or "child_moved."
42+
*/
3943
export type EventType =
4044
| 'value'
4145
| 'child_added'

packages/database/src/exp/Database.ts

+64-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ export function getDatabase(app: FirebaseApp, url?: string): FirebaseDatabase {
262262
}) as FirebaseDatabase;
263263
}
264264

265+
/**
266+
* Modify the provided instance to communicate with the Realtime Database
267+
* emulator.
268+
*
269+
* <p>Note: This method must be called before performing any other operation.
270+
*
271+
* @param db - The instance to modify.
272+
* @param host - The emulator host (ex: localhost)
273+
* @param port - The emulator port (ex: 8080)
274+
*/
265275
export function useDatabaseEmulator(
266276
db: FirebaseDatabase,
267277
host: string,
@@ -278,20 +288,73 @@ export function useDatabaseEmulator(
278288
repoManagerApplyEmulatorSettings(db._repo, host, port);
279289
}
280290

291+
/**
292+
* Disconnects from the server (all Database operations will be completed
293+
* offline).
294+
*
295+
* The client automatically maintains a persistent connection to the Database
296+
* server, which will remain active indefinitely and reconnect when
297+
* disconnected. However, the `goOffline()` and `goOnline()` methods may be used
298+
* to control the client connection in cases where a persistent connection is
299+
* undesirable.
300+
*
301+
* While offline, the client will no longer receive data updates from the
302+
* Database. However, all Database operations performed locally will continue to
303+
* immediately fire events, allowing your application to continue behaving
304+
* normally. Additionally, each operation performed locally will automatically
305+
* be queued and retried upon reconnection to the Database server.
306+
*
307+
* To reconnect to the Database and begin receiving remote events, see
308+
* `goOnline()`.
309+
*
310+
* @param db - The instance to disconnect.
311+
*/
281312
export function goOffline(db: FirebaseDatabase): void {
282313
db = getModularInstance(db);
283314
db._checkNotDeleted('goOffline');
284315
repoInterrupt(db._repo);
285316
}
286317

318+
/**
319+
* Reconnects to the server and synchronizes the offline Database state
320+
* with the server state.
321+
*
322+
* This method should be used after disabling the active connection with
323+
* `goOffline()`. Once reconnected, the client will transmit the proper data
324+
* and fire the appropriate events so that your client "catches up"
325+
* automatically.
326+
*
327+
* @param db - The instance to reconnect.
328+
*/
287329
export function goOnline(db: FirebaseDatabase): void {
288330
db = getModularInstance(db);
289331
db._checkNotDeleted('goOnline');
290332
repoResume(db._repo);
291333
}
292334

335+
/**
336+
* Logs debugging information to the console.
337+
*
338+
* @param enabled Enables logging if `true`, disables logging if `false`.
339+
* @param persistent Remembers the logging state between page refreshes if
340+
* `true`.
341+
*/
342+
export function enableLogging(enabled: boolean, persistent?: boolean);
343+
344+
/**
345+
* Logs debugging information to the console.
346+
*
347+
* @param logger A custom logger function to control how things get logged.
348+
* @param persistent Remembers the logging state between page refreshes if
349+
* `true`.
350+
*/
351+
export function enableLogging(
352+
logger?: (message: string) => unknown,
353+
persistent?: boolean
354+
);
355+
293356
export function enableLogging(
294-
logger?: boolean | ((message: string) => unknown),
357+
logger: boolean | ((message: string) => unknown),
295358
persistent?: boolean
296359
): void {
297360
enableLoggingImpl(logger, persistent);

packages/database/src/exp/OnDisconnect.ts

+81
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,39 @@ import {
3333
validateWritablePath
3434
} from '../core/util/validation';
3535

36+
/**
37+
* The `onDisconnect` class allows you to write or clear data when your client
38+
* disconnects from the Database server. These updates occur whether your
39+
* client disconnects cleanly or not, so you can rely on them to clean up data
40+
* even if a connection is dropped or a client crashes.
41+
*
42+
* The `onDisconnect` class is most commonly used to manage presence in
43+
* applications where it is useful to detect how many clients are connected and
44+
* when other clients disconnect. See {@link
45+
* https://firebase.google.com/docs/database/web/offline-capabilities Enabling
46+
* Offline Capabilities in JavaScript} for more information.
47+
*
48+
* To avoid problems when a connection is dropped before the requests can be
49+
* transferred to the Database server, these functions should be called before
50+
* writing any data.
51+
*
52+
* Note that `onDisconnect` operations are only triggered once. If you want an
53+
* operation to occur each time a disconnect occurs, you'll need to re-establish
54+
* the `onDisconnect` operations each time you reconnect.
55+
*/
3656
export class OnDisconnect {
3757
constructor(private _repo: Repo, private _path: Path) {}
3858

59+
/**
60+
* Cancels all previously queued `onDisconnect()` set or update events for this
61+
* location and all children.
62+
*
63+
* If a write has been queued for this location via a `set()` or `update()` at a
64+
* parent location, the write at this location will be canceled, though writes
65+
* to sibling locations will still occur.
66+
*
67+
* @return Resolves when synchronization to the server is complete.
68+
*/
3969
cancel(): Promise<void> {
4070
const deferred = new Deferred<void>();
4171
repoOnDisconnectCancel(
@@ -46,6 +76,12 @@ export class OnDisconnect {
4676
return deferred.promise;
4777
}
4878

79+
/**
80+
* Ensures the data at this location is deleted when the client is disconnected
81+
* (due to closing the browser, navigating to a new page, or network issues).
82+
*
83+
* @return Resolves when synchronization to the server is complete.
84+
*/
4985
remove(): Promise<void> {
5086
validateWritablePath('OnDisconnect.remove', this._path);
5187
const deferred = new Deferred<void>();
@@ -58,6 +94,25 @@ export class OnDisconnect {
5894
return deferred.promise;
5995
}
6096

97+
/**
98+
* Ensures the data at this location is set to the specified value when the
99+
* client is disconnected (due to closing the browser, navigating to a new page,
100+
* or network issues).
101+
*
102+
* `set()` is especially useful for implementing "presence" systems, where a
103+
* value should be changed or cleared when a user disconnects so that they
104+
* appear "offline" to other users. See {@link
105+
* https://firebase.google.com/docs/database/web/offline-capabilities Enabling
106+
* Offline Capabilities in JavaScript} for more information.
107+
*
108+
* Note that `onDisconnect` operations are only triggered once. If you want an
109+
* operation to occur each time a disconnect occurs, you'll need to re-establish
110+
* the `onDisconnect` operations each time.
111+
*
112+
* @param value - The value to be written to this location on disconnect (can
113+
* be an object, array, string, number, boolean, or null).
114+
* @return Resolves when synchronization to the Database is complete.
115+
*/
61116
set(value: unknown): Promise<void> {
62117
validateWritablePath('OnDisconnect.set', this._path);
63118
validateFirebaseDataArg('OnDisconnect.set', value, this._path, false);
@@ -71,6 +126,16 @@ export class OnDisconnect {
71126
return deferred.promise;
72127
}
73128

129+
/**
130+
* Ensures the data at this location is set to the specified value and priority
131+
* when the client is disconnected (due to closing the browser, navigating to a
132+
* new page, or network issues).
133+
*
134+
* @param value - The value to be written to this location on disconnect (can
135+
* be an object, array, string, number, boolean, or null).
136+
* @param priority - The priority to be written (string, number, or null).
137+
* @return Resolves when synchronization to the Database is complete.
138+
*/
74139
setWithPriority(
75140
value: unknown,
76141
priority: number | string | null
@@ -95,6 +160,22 @@ export class OnDisconnect {
95160
return deferred.promise;
96161
}
97162

163+
/**
164+
* Writes multiple values at this location when the client is disconnected (due
165+
* to closing the browser, navigating to a new page, or network issues).
166+
*
167+
* The `values` argument contains multiple property-value pairs that will be
168+
* written to the Database together. Each child property can either be a simple
169+
* property (for example, "name") or a relative path (for example, "name/first")
170+
* from the current location to the data to update.
171+
*
172+
* As opposed to the `set()` method, `update()` can be use to selectively update
173+
* only the referenced properties at the current location (instead of replacing
174+
* all the child properties at the current location).
175+
*
176+
* @param values Object containing multiple values.
177+
* @return Resolves when synchronization to the Database is complete.
178+
*/
98179
update(values: Indexable): Promise<void> {
99180
validateWritablePath('OnDisconnect.update', this._path);
100181
validateFirebaseMergeDataArg(

packages/database/src/exp/Reference.ts

+91
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,116 @@ import { QueryContext } from '../core/view/EventRegistration';
1919
* limitations under the License.
2020
*/
2121

22+
/**
23+
* A `Query` sorts and filters the data at a Database location so only a subset
24+
* of the child data is included. This can be used to order a collection of
25+
* data by some attribute (for example, height of dinosaurs) as well as to
26+
* restrict a large list of items (for example, chat messages) down to a number
27+
* suitable for synchronizing to the client. Queries are created by chaining
28+
* together one or more of the filter methods defined here.
29+
*
30+
* Just as with a `Reference`, you can receive data from a `Query` by using the
31+
* `on*()` methods. You will only receive events and `DataSnapshot`s for the
32+
* subset of the data that matches your query.
33+
*
34+
* Read our documentation on {@link
35+
* https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data
36+
* Sorting and filtering data} for more information.
37+
*/
2238
export interface Query extends QueryContext {
39+
/** The `Reference` for the `Query`'s location. */
2340
readonly ref: Reference;
41+
42+
/**
43+
* Returns whether or not the current and provided queries represent the same
44+
* location, have the same query parameters, and are from the same instance of
45+
* `FirebaseApp`.
46+
*
47+
* Two `Reference` objects are equivalent if they represent the same location
48+
* and are from the same instance of `FirebaseApp`.
49+
*
50+
* Two `Query` objects are equivalent if they represent the same location,
51+
* have the same query parameters, and are from the same instance of
52+
* `FirebaseApp`. Equivalent queries share the same sort order, limits, and
53+
* starting and ending points.
54+
*
55+
* @param other - The query to compare against.
56+
* @return Whether or not the current and provided queries are equivalent.
57+
*/
2458
isEqual(other: Query | null): boolean;
59+
60+
/**
61+
* Returns a JSON-serializable representation of this object.
62+
*
63+
* @return A JSON-serializable representation of this object.
64+
*/
2565
toJSON(): string;
66+
67+
/**
68+
* Gets the absolute URL for this location.
69+
*
70+
* The `toString()` method returns a URL that is ready to be put into a
71+
* browser, curl command, or a `refFromURL()` call. Since all of those expect
72+
* the URL to be url-encoded, `toString()` returns an encoded URL.
73+
*
74+
* Append '.json' to the returned URL when typed into a browser to download
75+
* JSON-formatted data. If the location is secured (that is, not publicly
76+
* readable), you will get a permission-denied error.
77+
*
78+
* @return The absolute URL for this location.
79+
*/
2680
toString(): string;
2781
}
2882

83+
/**
84+
* A `Reference` represents a specific location in your Database and can be used
85+
* for reading or writing data to that Database location.
86+
*
87+
* You can reference the root or child location in your Database by calling
88+
* `ref()` or `ref("child/path")`.
89+
*
90+
* Writing is done with the `set()` method and reading can be done with the
91+
* `on*()` method. See {@link
92+
* https://firebase.google.com/docs/database/web/read-and-write Read and Write
93+
* Data on the Web}
94+
*/
2995
export interface Reference extends Query {
96+
/**
97+
* The last part of the `Reference`'s path.
98+
*
99+
* For example, `"ada"` is the key for
100+
* `https://<DATABASE_NAME>.firebaseio.com/users/ada`.
101+
*
102+
* The key of a root `Reference` is `null`.
103+
*/
30104
readonly key: string | null;
105+
106+
/**
107+
* The parent location of a `Reference`.
108+
*
109+
* The parent of a root `Reference` is `null`.
110+
*/
31111
readonly parent: Reference | null;
112+
113+
/** The root `Reference` of the Database. */
114+
readonly root: Reference;
32115
}
33116

117+
/**
118+
* A `Promise` that can also act as a `Reference` when returned by
119+
* {@link push}. The reference is available immediately and the Promise resolves
120+
* as the write to the backend completes.
121+
*/
34122
export interface ThenableReference
35123
extends Reference,
36124
Pick<Promise<Reference>, 'then' | 'catch'> {}
37125

126+
/** A callback that can invoked to remove a listener. */
38127
export type Unsubscribe = () => void;
39128

129+
/** An options objects that can be used to customize a listener. */
40130
export interface ListenOptions {
131+
/** Whether to remove the listener after its first invocation. */
41132
readonly onlyOnce?: boolean;
42133
}
43134

0 commit comments

Comments
 (0)