@@ -251,37 +251,64 @@ class Query<T extends database_interop.QueryJsImpl> extends JsObjectWrapper<T> {
251251 /// DatabaseReference to the Query's location.
252252 DatabaseReference get ref => DatabaseReference .getInstance (jsObject.ref);
253253
254- late final Stream <QueryEvent > _onValue = _createStream ('value' );
254+ Stream <QueryEvent > _onValue (String appName, int hashCode) => _createStream (
255+ 'value' ,
256+ appName,
257+ hashCode,
258+ );
255259
256260 /// Stream for a value event. Event is triggered once with the initial
257261 /// data stored at location, and then again each time the data changes.
258- Stream <QueryEvent > get onValue => _onValue;
259-
260- late final Stream <QueryEvent > _onChildAdded = _createStream ('child_added' );
262+ Stream <QueryEvent > onValue (String appName, int hashCode) =>
263+ _onValue (appName, hashCode);
264+
265+ Stream <QueryEvent > _onChildAdded (String appName, int hashCode) =>
266+ _createStream (
267+ 'child_added' ,
268+ appName,
269+ hashCode,
270+ );
261271
262272 /// Stream for a child_added event. Event is triggered once for each
263273 /// initial child at location, and then again every time a new child is added.
264- Stream <QueryEvent > get onChildAdded => _onChildAdded;
265-
266- late final Stream <QueryEvent > _onChildRemoved =
267- _createStream ('child_removed' );
274+ Stream <QueryEvent > onChildAdded (String appName, int hashCode) =>
275+ _onChildAdded (appName, hashCode);
276+
277+ Stream <QueryEvent > _onChildRemoved (String appName, int hashCode) =>
278+ _createStream (
279+ 'child_removed' ,
280+ appName,
281+ hashCode,
282+ );
268283
269284 /// Stream for a child_removed event. Event is triggered once every time
270285 /// a child is removed.
271- Stream <QueryEvent > get onChildRemoved => _onChildRemoved;
272-
273- late final Stream <QueryEvent > _onChildChanged =
274- _createStream ('child_changed' );
286+ Stream <QueryEvent > onChildRemoved (String appName, int hashCode) =>
287+ _onChildRemoved (appName, hashCode);
288+
289+ Stream <QueryEvent > _onChildChanged (String appName, int hashCode) =>
290+ _createStream (
291+ 'child_changed' ,
292+ appName,
293+ hashCode,
294+ );
275295
276296 /// Stream for a child_changed event. Event is triggered when the data
277297 /// stored in a child (or any of its descendants) changes.
278298 /// Single child_changed event may represent multiple changes to the child.
279- Stream <QueryEvent > get onChildChanged => _onChildChanged;
280- late final Stream <QueryEvent > _onChildMoved = _createStream ('child_moved' );
299+ Stream <QueryEvent > onChildChanged (String appName, int hashCode) =>
300+ _onChildChanged (appName, hashCode);
301+ Stream <QueryEvent > _onChildMoved (String appName, int hashCode) =>
302+ _createStream (
303+ 'child_moved' ,
304+ appName,
305+ hashCode,
306+ );
281307
282308 /// Stream for a child_moved event. Event is triggered when a child's priority
283309 /// changes such that its position relative to its siblings changes.
284- Stream <QueryEvent > get onChildMoved => _onChildMoved;
310+ Stream <QueryEvent > onChildMoved (String appName, int hashCode) =>
311+ _onChildMoved (appName, hashCode);
285312
286313 /// Creates a new Query from a [jsObject] .
287314 Query .fromJsObject (T jsObject) : super .fromJsObject (jsObject);
@@ -377,66 +404,86 @@ class Query<T extends database_interop.QueryJsImpl> extends JsObjectWrapper<T> {
377404 );
378405 }
379406
380- Stream < QueryEvent > _createStream (String eventType) {
381- late StreamController < QueryEvent > streamController ;
407+ String _streamWindowsKey (String appName, String eventType, int hashCode) =>
408+ 'flutterfire-${ appName }_${ eventType }_${ hashCode }_snapshot' ;
382409
410+ Stream <QueryEvent > _createStream (
411+ String eventType,
412+ String appName,
413+ int hashCode,
414+ ) {
415+ late StreamController <QueryEvent > streamController;
416+ unsubscribeWindowsListener (_streamWindowsKey (appName, eventType, hashCode));
383417 final callbackWrap = ((
384418 database_interop.DataSnapshotJsImpl data, [
385- String ? string ,
419+ String ? prevChild ,
386420 ]) {
387- streamController.add (QueryEvent (DataSnapshot .getInstance (data), string));
421+ streamController
422+ .add (QueryEvent (DataSnapshot .getInstance (data), prevChild));
388423 });
389424
390425 final void Function (JSObject ) cancelCallbackWrap = ((JSObject error) {
391426 streamController.addError (convertFirebaseDatabaseException (error));
392- streamController.close ();
393427 });
394428
429+ late JSFunction onUnsubscribe;
430+
395431 void startListen () {
396432 if (eventType == 'child_added' ) {
397- database_interop.onChildAdded (
433+ onUnsubscribe = database_interop.onChildAdded (
398434 jsObject,
399435 callbackWrap.toJS,
400436 cancelCallbackWrap.toJS,
401437 );
402438 }
403439 if (eventType == 'value' ) {
404- database_interop.onValue (
440+ onUnsubscribe = database_interop.onValue (
405441 jsObject,
406442 callbackWrap.toJS,
407443 cancelCallbackWrap.toJS,
408444 );
409445 }
410446 if (eventType == 'child_removed' ) {
411- database_interop.onChildRemoved (
447+ onUnsubscribe = database_interop.onChildRemoved (
412448 jsObject,
413449 callbackWrap.toJS,
414450 cancelCallbackWrap.toJS,
415451 );
416452 }
417453 if (eventType == 'child_changed' ) {
418- database_interop.onChildChanged (
454+ onUnsubscribe = database_interop.onChildChanged (
419455 jsObject,
420456 callbackWrap.toJS,
421457 cancelCallbackWrap.toJS,
422458 );
423459 }
424460 if (eventType == 'child_moved' ) {
425- database_interop.onChildMoved (
461+ onUnsubscribe = database_interop.onChildMoved (
426462 jsObject,
427463 callbackWrap.toJS,
428464 cancelCallbackWrap.toJS,
429465 );
430466 }
467+ setWindowsListener (
468+ _streamWindowsKey (appName, eventType, hashCode),
469+ onUnsubscribe,
470+ );
431471 }
432472
433473 void stopListen () {
434- database_interop.off (jsObject, eventType.toJS, callbackWrap.toJS);
474+ onUnsubscribe.callAsFunction ();
475+ streamController.close ();
476+ removeWindowsListener (_streamWindowsKey (
477+ appName,
478+ eventType,
479+ hashCode,
480+ ));
435481 }
436482
437483 streamController = StreamController <QueryEvent >.broadcast (
438484 onListen: startListen,
439485 onCancel: stopListen,
486+ sync : true ,
440487 );
441488 return streamController.stream;
442489 }
@@ -447,8 +494,8 @@ class Query<T extends database_interop.QueryJsImpl> extends JsObjectWrapper<T> {
447494
448495 database_interop.onValue (
449496 jsObject,
450- ((database_interop.DataSnapshotJsImpl snapshot, [String ? string ]) {
451- c.complete (QueryEvent (DataSnapshot .getInstance (snapshot), string ));
497+ ((database_interop.DataSnapshotJsImpl snapshot, [String ? prevChild ]) {
498+ c.complete (QueryEvent (DataSnapshot .getInstance (snapshot), prevChild ));
452499 }).toJS,
453500 ((JSAny error) {
454501 c.completeError (convertFirebaseDatabaseException (error));
0 commit comments