NativeScript / NativeScript/firebase

Database listeners may fail to remove, doc update needed

オープン
#267 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
TypeScript
スター
62
フォーク
53
平均マージ
8日 4時間
マージ済み PR(30日)
2

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

packages/firebase-database/README.md の「Remove-a-reference-event-listener」から始め、リスナーを削除する例を、ここで説明されている 2 つのシナリオと比較します。例を更新し、issue で要求されている callback と reference のライフタイムに関する注記を追加します。README が両方のケースを正確にドキュメント化し、安全な使用パターンを示していれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
documentation
issue の種類
ドキュメント
難易度
1/5
見積もり時間
1時間未満
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。