pointfreeco / swift-identified-collections

A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.
MIT License
531 stars 45 forks source link

update child view from parent does not work when state change #10

Closed saroar closed 3 years ago

saroar commented 3 years ago

public struct ConversationsState: Equatable {
  ..........
  public var conversations: IdentifiedArrayOf<ConversationResponse.Item> = []
}

public struct TabsState: Equatable {
  ........
  public var conversations: ConversationsState
}

public let tabsReducer = Reducer<TabsState, TabsAction, TabsEnvironment> { state, action, environment in
 ..............................
  case let .receivedSocketMessage(.success(.string(str))):

      guard let data = str.data(using: .utf8) else {
        return .none
      }

      let chatOutGoingEvent = ChatOutGoingEvent.decode(data: data)

      switch chatOutGoingEvent {
      case .conversation(let message):

          // before update working update child my view
          guard let index = state.conversations.conversations
            .firstIndex(where: { $0.id == message.conversationId }) else {
          return .none
          }
          state.conversations.conversations[index].lastMessage = message
          // old code crash becz
         // "use the id-based subscript, instead, for in-place modification"

          // after which not working not update my child view
          state.conversations.conversations[id: message.conversationId]?
          .lastMessage = message
        return .none
      case .message(let message):
        state.conversations.chatState?.messages.insert(message, at: 0)
        return .none
      }
}
mbrandonw commented 3 years ago

I am going to convert this to a discussion as it doesn’t appear to be an issue with the library.