Sources/OrgDocument/Buffer.swift

2cb9910daf496bc213dbf10762f4e94f28afce82
orgstar/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}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form