-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispose viewConnectable synchronously #190
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,18 +94,13 @@ public final class MobiusController<Model, Event, Effect> { | |
) | ||
self.state = state | ||
|
||
// Maps an event consumer to a new event consumer that invokes the original one on the loop queue, | ||
// asynchronously. | ||
// Maps an event consumer to a new event consumer that asynchronously invokes the original on the loop queue. | ||
// | ||
// The input will be the core `MobiusLoop`’s event dispatcher, which asserts that it isn’t invoked after the | ||
// loop is disposed. This doesn’t play nicely with asynchrony, so here we assert when the transformed event | ||
// consumer is invoked, but fail silently if the controller is stopped before the asynchronous block executes. | ||
// loop is disposed. This doesn’t play nicely with asynchrony, so here we fail silently if the controller is | ||
// stopped before the asynchronous block executes. | ||
func flipEventsToLoopQueue(consumer: @escaping Consumer<Event>) -> Consumer<Event> { | ||
return { event in | ||
guard state.running else { | ||
MobiusHooks.errorHandler("\(Self.debugTag): cannot accept events when stopped", #file, #line) | ||
} | ||
Comment on lines
-105
to
-107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to this closure being created and owned by the loop—and the lifetime of the loop being tied to the controller's running state—this doesn't appear to be a valid scenario, unless the loop reference was somehow leaking. |
||
|
||
loopQueue.async { | ||
guard state.running else { | ||
// If we got here, the controller was stopped while this async block was queued. Callers can’t | ||
|
@@ -211,9 +206,14 @@ public final class MobiusController<Model, Event, Effect> { | |
var disposables: [Disposable] = [loop] | ||
|
||
if let viewConnectable = stoppedState.viewConnectable { | ||
let viewConnection = viewConnectable.connect { [unowned loop] event in | ||
// Note: loop.unguardedDispatchEvent will call our flipEventsToLoopQueue, which implements the | ||
// assertion “unguarded” refers to, and also (of course) flips to the loop queue. | ||
let viewConnection = viewConnectable.connect { [weak loop] event in | ||
guard let loop = loop else { | ||
// This failure should not be reached under normal circumstances because it is handled by | ||
// AsyncDispatchQueueConnectable. Stopping here means that the viewConnectable called its | ||
// consumer reference after stop() has disposed the connection and deallocated the loop. | ||
MobiusHooks.errorHandler("\(Self.debugTag): cannot use invalid consumer", #file, #line) | ||
kmcbride marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
loop.unguardedDispatchEvent(event) | ||
} | ||
loop.addObserver(viewConnection.accept) | ||
|
@@ -228,7 +228,7 @@ public final class MobiusController<Model, Event, Effect> { | |
} | ||
} catch { | ||
MobiusHooks.errorHandler( | ||
errorMessage(error, default: "\(Self.debugTag): cannot start a while already running"), | ||
errorMessage(error, default: "\(Self.debugTag): cannot start a controller while already running"), | ||
#file, | ||
#line | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2019-2022 Spotify AB. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
@testable import MobiusCore | ||
|
||
import Foundation | ||
import Nimble | ||
import Quick | ||
|
||
class AsyncDispatchQueueConnectableTests: QuickSpec { | ||
private let acceptQueue = DispatchQueue(label: "accept queue") | ||
|
||
override func spec() { | ||
describe("AsyncDispatchQueueConnectable") { | ||
var connectable: AsyncDispatchQueueConnectable<String, String>! | ||
var underlyingConnectable: RecordingTestConnectable! | ||
|
||
beforeEach { | ||
underlyingConnectable = RecordingTestConnectable(expectedQueue: self.acceptQueue) | ||
connectable = AsyncDispatchQueueConnectable(underlyingConnectable, acceptQueue: self.acceptQueue) | ||
} | ||
|
||
context("when connected") { | ||
var connection: Connection<String>! | ||
var dispatchedValue: String? | ||
|
||
beforeEach { | ||
connection = connectable.connect { dispatchedValue = $0 } | ||
} | ||
|
||
afterEach { | ||
dispatchedValue = nil | ||
} | ||
|
||
it("should forward inputs to the underlying connectable") { | ||
connection.accept("S") | ||
expect(underlyingConnectable.recorder.items).toEventually(equal(["S"])) | ||
} | ||
|
||
it("should forward outputs from the underlying connectable") { | ||
underlyingConnectable.dispatch("S") | ||
expect(dispatchedValue).to(equal("S")) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,8 +64,6 @@ class RecordingTestConnectable: Connectable { | |
} | ||
|
||
func dispose() { | ||
verifyQueue() | ||
|
||
disposed = true | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous code had an issue with retaining the consumer reference, which would lead to a somewhat nebulous crash when its unowned loop reference was accessed by emitting an event after disposal. This code is functionally equivalent, but the cause of the error should be clearer now.