Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCRUM-51] StreamListener 개선 #66

Merged
merged 3 commits into from
Dec 27, 2024
Merged

[SCRUM-51] StreamListener 개선 #66

merged 3 commits into from
Dec 27, 2024

Conversation

Jihyun247
Copy link
Collaborator

[SCRUM-51] StreamListener 개선

무엇에 관한 PR 인가요? 🙋

public typealias StreamKey = String

public protocol StreamType: Hashable {
  static var key: StreamKey { get }
}
  • 스트림 리스너 모듈은 오직 이벤트를 send, receive 할 수 있도록 정의해둠 (스트림 삭제도 추가할 예정)
  • 스트림 리스너 객체는 필요한 Domain 모듈에 정의해놓도록 ! (ex. ServerStateStream은 AppService에 있습니다)
  • 스트림 객체는 StreamType을 따르고, 스트림 리스너 딕셔너리에 저장할 수 있도록 Key값이 있습니다.
[StreamKey: StreamContinuation] = [:]

남은 작업

재시도 스트림 객체 생성 및 로직 연결 (어려우ㅜ ㅓ ..)

@Jihyun247 Jihyun247 requested a review from devMinseok December 27, 2024 14:09
@Jihyun247 Jihyun247 self-assigned this Dec 27, 2024
devMinseok
devMinseok previously approved these changes Dec 27, 2024
Copy link
Member

@devMinseok devMinseok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생했어! 다 깔끔하게 잘 만들었고만 👍👍

이건 추가적인거긴 한데.. 단일 stream을 여러곳에서 receive하고자 할때는 문제가 있을거 같아서 배열로 관리를 해야할거 같넴

private actor StreamActor {
  private var streams: [StreamKey: [StreamContinuation]] = [:]

  func register<T: StreamType>(key: StreamKey, continuation: AsyncStream<T>.Continuation) {
    let newContinuation = StreamContinuation(continuation)
    if streams[key] == nil {
      streams[key] = [newContinuation]
    } else {
      streams[key]?.append(newContinuation)
    }
  }

  func yield<T: StreamType>(type: StreamKey, value: T) {
    guard let continuations = streams[type] else { return }
    continuations.forEach { $0.yield(value) }
  }

  func removeContinuation(key: StreamKey, continuation: StreamContinuation) {
    streams[key]?.removeAll { $0 === continuation }
    if streams[key]?.isEmpty == true {
      streams[key] = nil
    }
  }
}

devMinseok
devMinseok previously approved these changes Dec 27, 2024
@Jihyun247
Copy link
Collaborator Author

func removeContinuation(key: StreamKey, continuation: StreamContinuation) {
    streams[key]?.removeAll { $0 === continuation }
    if streams[key]?.isEmpty == true {
      streams[key] = nil
    }
  }

여기에서 매개변수로 있는 continuation은 , 특정해서 하나의 continuation을 삭제할 일은 없을 것 같아서

  func remove(key: StreamKey) {
    streams[key]?.forEach { $0.finish() }
    streams[key] = nil
  }

이렇게 수정했는데 의견 있으면 알려죠

@Jihyun247 Jihyun247 merged commit 052cc4c into develop Dec 27, 2024
1 check passed
@Jihyun247 Jihyun247 deleted the feature/SCRUM-51 branch December 27, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants