Skip to content
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

adds AsyncChannel and AsyncThrowingChannel with tests #2086

Merged
merged 4 commits into from
Aug 10, 2022

Conversation

brennanMKE
Copy link
Contributor

Issue #, if available:

Description of changes:

adds AsyncChannel and AsyncThrowingChannel with tests

Check points: (check or cross out if not relevant)

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
  • Documentation update for the change if required
  • PR title conforms to conventional commit style
  • If breaking change, documentation/changelog update with migration instructions

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@brennanMKE brennanMKE requested review from a team as code owners August 4, 2022 21:40
@brennanMKE brennanMKE self-assigned this Aug 4, 2022
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 4, 2022 21:40 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 8, 2022 23:13 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 9, 2022 21:26 Inactive

import Foundation

public actor AsyncChannel<Element: Sendable>: AsyncSequence {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need public keyword for all these?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed lets make these internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, though the AsyncSequence is a protocol with associated type. There is no way to represent it in Swift for a return value without the implementation

}

mutating func next() async -> Element? {
await channel.next()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What can be canceled here?

init() {
}

nonisolated func makeAsyncIterator() -> Iterator {
Copy link
Contributor

@royjit royjit Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnonisolated okay? Will the channel work if we iterate in two different task?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function cannot be isolated in the actor. it also does not mutate any state. It creates the iterator which is used by the sequence and all those functions are isolated within the actor.

}

private var canTerminate: Bool {
terminated && elements.isEmpty && !continuations.isEmpty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be OR cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is correct


func finish() {
terminated = true
processNext()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we calling processNext after finishing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the processNext function


import Foundation

actor AsyncChannel<Element: Sendable>: AsyncSequence {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Call it AmplifyAsyncChannel ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is reason to specialize this name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if developer uses both Amplify and AsyncAlgorithms this names can conflict. We cannot use module name to distinguish between them because of the known issue in SPM where it cannot figure out the type Amplify vs the module Amplify. swiftlang/swift#43510

@brennanMKE brennanMKE temporarily deployed to UnitTest August 10, 2022 15:19 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 10, 2022 15:19 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 10, 2022 15:19 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 10, 2022 15:19 Inactive
@brennanMKE brennanMKE temporarily deployed to UnitTest August 10, 2022 15:20 Inactive
Copy link
Contributor

@royjit royjit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This api can change based on api review.

@brennanMKE brennanMKE merged commit e95bb69 into dev-preview Aug 10, 2022
@brennanMKE brennanMKE deleted the stehlib.async-channel branch August 10, 2022 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants