NativeScript / NativeScript/firebase

Database listeners may fail to remove, doc update needed

未关闭
#267 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
TypeScript
星标
62
派生
53
平均合并
8 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 packages/firebase-database/README.md 中的“Remove-a-reference-event-listener”开始,将其中的移除 listener 示例与此处描述的两种场景进行比较。更新示例,并添加 issue 要求的关于 callback 和 reference 生命周期的说明。完成的标准是 README 准确记录这两种情况,并展示安全的使用模式。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
documentation
Issue 类型
文档
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。