Database listeners may fail to remove, doc update needed
還沒有人認領這個 Issue。
評估
- 難度
- 1/5
- 預估耗時
- 1 小時以內
- 新手友好度
- 45/100
- Issue 類型
- 文件
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- typescript
研究方向
從 packages/firebase-database/README.md 中的「Remove-a-reference-event-listener」開始,將其中的移除 listener 範例與此處描述的兩種情境進行比較。更新範例,並加入 issue 要求的關於 callback 和 reference 生命週期的說明。完成的標準是 README 準確記錄這兩種情況,並展示安全的使用模式。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- TypeScript
- 星號
- 62
- 分支
- 53
- 平均合併
- 8 天 4 小時
- 30 天內合併 PR
- 2
環境準備
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
NativeScript/firebase 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 48/100
NativeScript/firebase#292 ·
-
難度 3/5 1-2 天 新手友好度 35/100
NativeScript/firebase#289 ·
-
難度 4/5 3-5 天 新手友好度 35/100
NativeScript/firebase#288 · 4 個 reaction ·
-
[messaging-core][ios] getCurrentToken stalls可能重新可做 @triniwiz 於 182 天前認領,目前沒有進行中的 PR。 未關閉bug ios
NativeScript/firebase#287 · 已指派 1 人 ·
-
Uh-Oh Shazam未關閉
難度 4/5 3-5 天 新手友好度 20/100
NativeScript/firebase#286 ·
查看 NativeScript/firebase 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
microsoft/vscode-livepreview#876 ·
維護者通常 1 天內回覆
-
needs-triage
難度 1/5 1 小時以內 新手友好度 90/100
JustJarethB/invoicer#54 ·
-
ICP 1.2.0 shows a scheduled task's interval in milliseconds under the label "Interval (In seconds)"未關閉Needs Triage Type/Bug
難度 2/5 1-3 小時 新手友好度 68/100
wso2/product-integrator#2585 ·
維護者通常 1 天內回覆
-
check:passed streams:add
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
design
難度 2/5 1-3 小時 新手友好度 72/100
MTES-MCT/monitor-field#119 ·
維護者通常 1 天內回覆