Sources/OrgDocument/Buffer.swift
24 lines · 753 bytes
1import Foundation
2
3/// Where an open document's live state is kept: the editor while one shows it, a
4/// `StateBuffer` before that.
5@MainActor
6public protocol DocumentBuffer: AnyObject {
7 var document: DocumentState { get }
8 /// Applies a change made outside typing (save, disk change, revert) and updates whatever
9 /// shows the document.
10 func update<T>(_ change: (inout DocumentState) throws -> T) rethrows -> T
11}
12
13@MainActor
14public final class StateBuffer: DocumentBuffer {
15 public private(set) var document: DocumentState
16
17 public init(_ document: DocumentState) {
18 self.document = document
19 }
20
21 public func update<T>(_ change: (inout DocumentState) throws -> T) rethrows -> T {
22 try change(&document)
23 }
24}