Database listeners may fail to remove, doc update needed
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- typescript
- Lĩnh vực
- documentation
Hướng nghiên cứu
Bắt đầu tại packages/firebase-database/README.md, bên dưới “Remove-a-reference-event-listener”, và so sánh ví dụ xóa listener của tài liệu với hai kịch bản được mô tả ở đây. Cập nhật ví dụ và thêm ghi chú về callback cũng như vòng đời của reference theo yêu cầu trong issue. Công việc được xem là hoàn tất khi README ghi lại chính xác cả hai trường hợp và thể hiện mẫu sử dụng an toàn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 62
- Fork
- 53
- Merge trung bình
- 8 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 2
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của NativeScript/firebase
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
NativeScript/firebase#292 ·
-
Can't install package Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
NativeScript/firebase#289 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
NativeScript/firebase#288 · 4 reaction ·
-
bug ios
NativeScript/firebase#287 · 1 người được giao ·
-
Uh-Oh Shazam Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
NativeScript/firebase#286 ·
Tất cả issue của NativeScript/firebase
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
safetrustcr/dApp-SafeTrust#426 ·
-
area:workflow bug ready-for-agent
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
fil-donadoni/tolaria#4409 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Fission-AI/OpenSpec#1960 ·
-
Add dependabot Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
corsairdev/corsair#1764 ·