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

Deprecate .observe() #71

Open
sindresorhus opened this issue Jun 1, 2021 · 3 comments
Open

Deprecate .observe() #71

sindresorhus opened this issue Jun 1, 2021 · 3 comments

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Jun 1, 2021

.updates() is better in every way. Having two ways to do the same is also confusing. We cannot remove .observe() for a long time, but maybe we could add availability annotations so that apps that target macOS 10.15+ (and equivalent iOS/tvOS/watchOS) gets a deprecation message.

@sindresorhus sindresorhus mentioned this issue Nov 19, 2022
1 task
@Maschina
Copy link
Contributor

Since I see the point to get rid of dead wood and streamline the API, I see a strong benefit of using .observe() and that is the KeyChange object, which comprises KeyChange.oldValue. AsyncStream would loose this functionality.

@Maschina
Copy link
Contributor

Would it make sense to extend the AsyncStream implementation with a KeyChange next to Value?

@sindresorhus
Copy link
Owner Author

sindresorhus commented May 29, 2024

I wanted the main method to be simple, because from extensive use, I almost never needed the old value. However, I do agree we should support providing the old value somehow. Maybe a updatesWithPrevious() method or something. It would return a tuple of old/new values though. I considered overloading .updates(), but it may create some ambiguities. I will need to think about it.


For now, you could use these utilities:

extension AsyncStream {
	func withPrevious() -> AsyncStream<(previous: Element?, current: Element)> {
		.init { continuation in
			Task {
				var previous: Element?
				for await element in self {
					continuation.yield((previous, element))
					previous = element
				}

				continuation.finish()
			}
		}
	}

	func withPrevious(initialPreviousValue: Element) -> AsyncStream<(previous: Element, current: Element)> {
		.init { continuation in
			Task {
				var previous = initialPreviousValue
				for await element in self {
					continuation.yield((previous, element))
					previous = element
				}

				continuation.finish()
			}
		}
	}
}

The obvious benefit of having support built-in is that the previous value would be non-optional.

@sindresorhus sindresorhus changed the title Deprecate .observe() when target is macOS 10.15+? Deprecate .observe() May 29, 2024
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

No branches or pull requests

2 participants