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

[Feature/#78] 동시에 공유컨테이너로 진입하는 기능 구현 #79

Merged
merged 9 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Data/Data/EventRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// NoticedEventRepository.swift
// Data
//
// Created by Yune gim on 12/2/24.
//

import Combine
import Entity
import Foundation
import Interfaces
import P2PSocket

public final class NoticedEventRepository: NoticedEventRepositoryInterface {
private var cancellables: Set<AnyCancellable> = []
private let socketProvider: SocketProvidable

public var receivedEvent = PassthroughSubject<Event, Never>()

public init(socketProvider: SocketProvidable) {
self.socketProvider = socketProvider
bind()
}

public func notice(event: Event) {
guard let eventData = try? JSONEncoder().encode(event) else { return }
socketProvider.sendAll(data: eventData)
}
}

private extension NoticedEventRepository {
func bind() {
socketProvider.dataShared.sink { [weak self] (data, _) in
guard let event = try? JSONDecoder().decode(Event.self, from: data) else { return }
self?.receivedEvent.send(event)
}
.store(in: &cancellables)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

P2: 만약 이렇게 NoticedEventRepository를 따로 사용하게 된다면 JSONEncoder / JSONDecoder는 따로 빼서 재사용 가능하게 하면 어떨까요?

Copy link
Collaborator Author

@051198Hz 051198Hz Dec 2, 2024

Choose a reason for hiding this comment

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

좋습니다!
그리고 이렇지 않아도 Core에서 JSONEncoder/Decoder를 두고 다른데서 재사용해야 할 필요가 생긴것 같아요.
Data에서 인코딩/디코딩이 많이 일어나기 시작해 보여요

10 changes: 10 additions & 0 deletions Domain/Entity/Event.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Event.swift
// Entity
//
// Created by Yune gim on 12/2/24.
//

public enum Event: Codable {
case openSharedContainer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NoticedEventRepositoryInterface.swift
// Interfaces
//
// Created by Yune gim on 12/2/24.
//

import Combine
import Entity

public protocol NoticedEventRepositoryInterface {
var receivedEvent: PassthroughSubject<Event, Never> { get }

func notice(event: Event)
}