@@ -33,9 +33,39 @@ import {
33
33
validateWritablePath
34
34
} from '../core/util/validation' ;
35
35
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
+ */
36
56
export class OnDisconnect {
37
57
constructor ( private _repo : Repo , private _path : Path ) { }
38
58
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
+ */
39
69
cancel ( ) : Promise < void > {
40
70
const deferred = new Deferred < void > ( ) ;
41
71
repoOnDisconnectCancel (
@@ -46,6 +76,12 @@ export class OnDisconnect {
46
76
return deferred . promise ;
47
77
}
48
78
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
+ */
49
85
remove ( ) : Promise < void > {
50
86
validateWritablePath ( 'OnDisconnect.remove' , this . _path ) ;
51
87
const deferred = new Deferred < void > ( ) ;
@@ -58,6 +94,25 @@ export class OnDisconnect {
58
94
return deferred . promise ;
59
95
}
60
96
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
+ */
61
116
set ( value : unknown ) : Promise < void > {
62
117
validateWritablePath ( 'OnDisconnect.set' , this . _path ) ;
63
118
validateFirebaseDataArg ( 'OnDisconnect.set' , value , this . _path , false ) ;
@@ -71,6 +126,16 @@ export class OnDisconnect {
71
126
return deferred . promise ;
72
127
}
73
128
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
+ */
74
139
setWithPriority (
75
140
value : unknown ,
76
141
priority : number | string | null
@@ -95,6 +160,22 @@ export class OnDisconnect {
95
160
return deferred . promise ;
96
161
}
97
162
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
+ */
98
179
update ( values : Indexable ) : Promise < void > {
99
180
validateWritablePath ( 'OnDisconnect.update' , this . _path ) ;
100
181
validateFirebaseMergeDataArg (
0 commit comments