Skip to content

Commit

Permalink
Support onReceive
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed May 11, 2024
1 parent 6479157 commit 9263ec8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://source.skip.tools/skip.git", from: "0.8.32"),
.package(url: "https://source.skip.tools/skip-model.git", from: "0.6.0"),
.package(url: "https://source.skip.tools/skip-model.git", from: "0.6.1"),
],
targets: [
.target(name: "SkipUI", dependencies: [.product(name: "SkipModel", package: "skip-model")], plugins: [.plugin(name: "skipstone", package: "skip")]),
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,10 @@ Support levels:
<td>✅</td>
<td><code>.onMove</code></td>
</tr>
<tr>
<td>✅</td>
<td><code>.onReceive</code></td>
</tr>
<tr>
<td>✅</td>
<td><code>.onSubmit</code></td>
Expand Down
28 changes: 26 additions & 2 deletions Sources/SkipUI/SkipUI/View/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

import Combine
#if SKIP
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
#endif

extension View {
@available(*, unavailable)
public func onReceive<P>(_ publisher: P, perform action: @escaping (Any /* P.Output */) -> Void) -> some View /* where P : Publisher, P.Failure == Never */ {
// SKIP DECLARE: fun <P, Output> onReceive(publisher: P, perform: (Output) -> Unit): View where P: Publisher<Output, *>
public func onReceive<P>(_ publisher: P, perform action: @escaping (P.Output) -> Void) -> some View where P : Publisher {
#if SKIP
return ComposeModifierView(targetView: self) { _ in
let latestAction = rememberUpdatedState(action)
let subscription = remember {
publisher.sink { output in
latestAction.value(output)
}
}
DisposableEffect(subscription) {
onDispose {
subscription.cancel()
}
}
return ComposeResult.ok
}
#else
return self
#endif
}
}

Expand Down

0 comments on commit 9263ec8

Please sign in to comment.