Skip to content

Commit

Permalink
feat: update gate state when value changed
Browse files Browse the repository at this point in the history
  • Loading branch information
r13v committed Jul 5, 2023
1 parent dbe6db9 commit b5301f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Effector/Gate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

public struct Gate<T> {
public struct Gate<Value> {
// MARK: Lifecycle

public init() {
Expand All @@ -15,13 +15,15 @@ public struct Gate<T> {

// MARK: Public

public let state = Store<T?>(nil)
public let state = Store<Value?>(nil)
public let status = Store(false)
public let open = Event<T>()
public let open = Event<Value>()
public let close = Event<Void>()

public func view(_ value: T) -> some View {
Rectangle()
public func callAsFunction(_ value: Value) -> some View {
state.setState(value)

return Rectangle()
.hidden()
.onAppear {
open(value)
Expand All @@ -32,8 +34,8 @@ public struct Gate<T> {
}
}

public extension Gate where T == Void {
func view() -> some View {
view(())
public extension Gate where Value == Void {
func callAsFunction() -> some View {
callAsFunction(())
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum CounterFeature {
static let counter = Store(0)
static let inc = Event<Void>()
static let dec = Event<Void>()
static let CounterGate = Gate<Int>()

static let logFx = Effect<Int, Void, Error> { n in print("n: \(n)") }

Expand All @@ -28,6 +29,11 @@ enum CounterFeature {
trigger: counter.updates,
target: logFx
)

CounterGate.open.watch { print("Gate open: \($0)") }
CounterGate.close.watch { print("Gate close: \($0)") }
CounterGate.status.watch { print("Gate status: \($0)") }
CounterGate.state.watch { print("Gate state: \(String(describing: $0))") }
}
}

Expand All @@ -51,6 +57,7 @@ struct ContentView: View {
Button("inc", action: CounterFeature.inc.run)
Button("inc 10 async", action: { Task { try await CounterFeature.incAsync() }})
Button("set 100") { counter = 100 }
CounterFeature.CounterGate(counter)
}
}
}
Expand Down

0 comments on commit b5301f2

Please sign in to comment.