//
//  SnapshotPublisher.swift
//  punchnet
//
//  Created by 安礼成 on 2026/2/5.
//
import Foundation

final class SnapshotPublisher<S: Snapshot>: @unchecked Sendable {
    private let lock = NSLock()
    private var snapshot: S
    
    init(initial snapshot: S) {
        self.snapshot = snapshot
    }

    func publish(_ snapshot: S) {
        self.lock.lock()
        self.snapshot = snapshot
        self.lock.unlock()
    }

    @inline(__always)
    func current() -> S {
        self.lock.lock()
        let snapshot = self.snapshot
        self.lock.unlock()
        return snapshot
    }
}
