Closed devpolant closed 4 years ago
In this pull request I'm going to fix the logic of registering new tap handlers.
Previous implementation of this method is completely wrong, because copy-on-write behaviour was not considered.
copy-on-write
New handler was appended to copy in this line - handlers.append(handler). tapHandlers dictionary was not changes, so as a user:
handlers.append(handler)
tapHandlers
This is old implementation:
@discardableResult public func onTap(tapCount: Int = 1, f: @escaping (TapEvent) -> Void) -> Disposable { let handler = ChangeHandler<TapEvent>(f) if var handlers = tapHandlers[tapCount] { // array copy is created handlers.append(handler) // copy is mutated here, so changes not applied to `self.tapHandlers` } else { tapHandlers[tapCount] = [handler] } return Disposable { [weak self, unowned handler] in guard let index = self?.tapHandlers[tapCount]?.firstIndex(of: handler) else { return } self?.tapHandlers[tapCount]?.remove(at: index) } }
Issue is fixed in new implementation.
Thank you for the fix! Merged it.
Updates
In this pull request I'm going to fix the logic of registering new tap handlers.
Previous implementation of this method is completely wrong, because
copy-on-write
behaviour was not considered.New handler was appended to copy in this line -
handlers.append(handler)
.tapHandlers
dictionary was not changes, so as a user:This is old implementation:
Issue is fixed in new implementation.