NativeScript / NativeScript/firebase

Database listeners may fail to remove, doc update needed

Aperta
#267 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
TypeScript
Stelle
62
Fork
53
Merge medio
8g 4h
PR unite (30g)
2

Descrizione

I have encountered two scenarios that can cause database listeners (such as 'child_changed') to fail to remove.

Here's the code at the end of the on method :

    callback['__fbHandle'] = handle;
    callback['__fbEventType'] = eventType;
    callback['__fbContext'] = context;

    this._handles.set(callback, handle);

this in this case is the path reference, such as firebase().database().ref('user/data').

Consequently, this code will work correctly:

    const callback = function(snapshot) { console.log('callback: ' + snapshot.val()); }
    const ref = firebase().database().ref('user/data');
    const listener = ref.on('child_changed', callback);
    ref.off('child_changed', listener);

Whereas this code will fail to remove the listener:

    const callback = function (snapshot) { console.log('callback: ' + snapshot.val()); }
    const listener = firebase().database().ref('user/data').on('child_changed', callback);
    firebase().database().ref('user/data').off('child_changed', listener);

Because the off method references the handle saved by the on method, but above you have a different instance of the reference.

Here's the entirety of the off method:

    off(eventType?: EventType, callback?: (a: DataSnapshot, b: string) => void, context?: Record<string, any>): void {
        const handle = callback?.['__fbHandle'];
        const event = callback?.['__fbEventType'];
        if (handle && event === eventType) {
            if (this._handles.has(callback)) {
                this.native.removeEventListener(handle as any);
                callback['__fbHandle'] = undefined;
                callback['__fbEventType'] = undefined;
                callback['__fbContext'] = undefined;
                this._handles.delete(callback);
            }
        }
    } 

In the failing case, this references two different objects, and thus this._handles.has(callback) resolves to false and the listener is not removed.

This can be resolved by creating the reference first, then using that same reference for both the on and off invocations, as shown in the success example above.

The second scenario is when a common callback (event handler) is used. In the off method above, the handle, event type, and context all are deleted from the callback when the listener is removed. If you use that same callback function on a subsequent off call, the handle will resolve to undefined and the block that removes the listener will be skipped.

This can be resolved by invoking the common callback within a unique outer function, such as

    const commonCallback = function (snapshot) { console.log('commonCallback: ' + snapshot.val()); }

    const callback = function (snapshot) { commonCallback(snapshot); }
    const ref = firebase().database().ref('user/data');
    const listener = ref.on('child_changed', callback);
    ref.off('child_changed', listener);

I recommend revising the code example in the database readme topic Remove-a-reference-event-listener, and adding a note that a unique callback function must be used with each on invocation.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da packages/firebase-database/README.md, sotto “Remove-a-reference-event-listener”, e confronta il suo esempio di rimozione di un listener con i due scenari descritti qui. Aggiorna l’esempio e aggiungi la nota sul callback e sulla durata di vita del riferimento richiesta nell’issue. Il lavoro è completato quando la README documenta correttamente entrambi i casi e mostra il modello di utilizzo sicuro.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.