FloatChatStoreProvider.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // ConferenceRoomStore.swift
  3. // TUIRoomKit
  4. //
  5. // Created by CY zhao on 2024/5/10.
  6. //
  7. import Foundation
  8. #if USE_OPENCOMBINE
  9. import OpenCombine
  10. #else
  11. import Combine
  12. #endif
  13. class FloatChatStore {
  14. private(set) lazy var store: Store<FloatChatState, FloatChatService> = Store(initialState: FloatChatState(), environment: FloatChatService())
  15. init() {
  16. initStore()
  17. }
  18. deinit {
  19. store.unregister(reducer: floatChatReducer)
  20. store.unregisterEffects(withId: FloatChatEffect.id)
  21. }
  22. private func initStore() {
  23. store.register(reducer: floatChatReducer)
  24. store.register(effects: FloatChatEffect())
  25. }
  26. }
  27. extension FloatChatStore: FloatChatStoreProvider {
  28. func dispatch(action: Action) {
  29. store.dispatch(action: action)
  30. }
  31. func select<Value>(_ selector: Selector<FloatChatState, Value>) -> AnyPublisher<Value, Never> where Value : Equatable {
  32. return store.select(selector)
  33. .removeDuplicates()
  34. .eraseToAnyPublisher()
  35. }
  36. func selectCurrent<Value>(_ selector: Selector<FloatChatState, Value>) -> Value {
  37. return store.selectCurrent(selector)
  38. }
  39. }