Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Mar 4, 2024
1 parent 2455b28 commit da58b01
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
25 changes: 20 additions & 5 deletions Sources/Verge/Library/StoreSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public final class StoreStateSubscription: Hashable, Cancellable, @unchecked Sen

private let wasCancelled = ManagedAtomic(false)

private let isSuspending: ManagedAtomic<Bool> = .init(false)

private var entranceForSuspension: ManagedAtomic<Int8> = .init(0)

private var source: AtomicReferenceStorage<EventEmitterCancellable>
Expand All @@ -129,7 +131,7 @@ public final class StoreStateSubscription: Hashable, Cancellable, @unchecked Sen
return
}

source.dispose().cancel()
AtomicReferenceStorage.atomicLoad(at: &source, ordering: .relaxed).cancel()

associatedStore = nil
}
Expand All @@ -140,7 +142,7 @@ public final class StoreStateSubscription: Hashable, Cancellable, @unchecked Sen
return
}

source.dispose().cancel()
AtomicReferenceStorage.atomicLoad(at: &source, ordering: .relaxed).cancel()

}

Expand All @@ -164,9 +166,16 @@ public final class StoreStateSubscription: Hashable, Cancellable, @unchecked Sen
return
}

defer {
entranceForSuspension.wrappingDecrement(ordering: .sequentiallyConsistent)
}

guard isSuspending.compareExchange(expected: false, desired: true, ordering: .sequentiallyConsistent).exchanged else {
return
}

onAction(self, .suspend)

entranceForSuspension.wrappingDecrement(ordering: .sequentiallyConsistent)
}

public func resume() {
Expand All @@ -180,9 +189,15 @@ public final class StoreStateSubscription: Hashable, Cancellable, @unchecked Sen
return
}

onAction(self, .resume)
defer {
entranceForSuspension.wrappingDecrement(ordering: .sequentiallyConsistent)
}

guard isSuspending.compareExchange(expected: true, desired: false, ordering: .sequentiallyConsistent).exchanged else {
return
}

entranceForSuspension.wrappingDecrement(ordering: .sequentiallyConsistent)
onAction(self, .resume)
}

func associate(store: some StoreType) -> StoreStateSubscription {
Expand Down
56 changes: 51 additions & 5 deletions Sources/Verge/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -895,24 +895,70 @@ private final class StoreSubscriptionView: UIView {
let store: Store<State, Never> = .init(initialState: .init())

private let label = UILabel()

Check failure on line 897 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

cannot find 'UILabel' in scope

Check failure on line 897 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

cannot find 'UILabel' in scope
private var subscription: StoreStateSubscription?

override init(frame: CGRect) {

Check failure on line 900 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

initializer does not override a designated initializer from its superclass

Check failure on line 900 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

initializer does not override a designated initializer from its superclass
super.init(frame: frame)

Check failure on line 901 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

'super' members cannot be referenced in a root class

backgroundColor = .red
backgroundColor = .systemBackground

Check failure on line 903 in Sources/Verge/Store/Store.swift

View workflow job for this annotation

GitHub Actions / test

cannot find 'backgroundColor' in scope

let button = UIButton.init(configuration: .bordered())
button.addAction(.init(handler: { action in
let upButton = UIButton.init(configuration: .bordered())
upButton.setTitle("Up", for: .normal)
upButton.addAction(.init(handler: { [weak self] action in

self?.store.commit {
$0.count += 1
}

}), for: .touchUpInside)

let suspendButton = UIButton.init(configuration: .bordered())
suspendButton.setTitle("Suspend", for: .normal)
suspendButton.addAction(.init(handler: { [weak self] action in

self?.subscription?.suspend()

}), for: .touchUpInside)

let resumeButton = UIButton.init(configuration: .bordered())
resumeButton.setTitle("Resume", for: .normal)
resumeButton.addAction(.init(handler: { [weak self] action in

self?.subscription?.resume()

}), for: .touchUpInside)


let stack = UIStackView()

stack.addArrangedSubview(label)
stack.addArrangedSubview(button)
stack.addArrangedSubview(upButton)
stack.addArrangedSubview(suspendButton)
stack.addArrangedSubview(resumeButton)

stack.axis = .vertical
stack.distribution = .equalCentering

addSubview(stack)
stack.autoresizingMask =
stack.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(
[
stack.topAnchor.constraint(equalTo: topAnchor),
stack.leadingAnchor.constraint(equalTo: leadingAnchor),
stack.trailingAnchor.constraint(equalTo: trailingAnchor),
stack.bottomAnchor.constraint(equalTo: bottomAnchor)
]
)

subscription = store.sinkState { [weak self] state in
guard let self else { return }

state.ifChanged(\.count).do { value in
self.label.text = value.description
}

}

}

required init?(coder: NSCoder) {
Expand Down

0 comments on commit da58b01

Please sign in to comment.