Skip to content

Commit

Permalink
Remove adapter concept and MainOffender dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Nov 22, 2023
1 parent 1e4abb8 commit 5cd0787
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 251 deletions.
33 changes: 11 additions & 22 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
{
"object": {
"pins": [
{
"package": "MainOffender",
"repositoryURL": "https://github.com/mattmassicotte/MainOffender",
"state": {
"branch": null,
"revision": "343cc3797618c29b48b037b4e2beea0664e75315",
"version": "0.1.0"
}
},
{
"package": "Rearrange",
"repositoryURL": "https://github.com/ChimeHQ/Rearrange",
"state": {
"branch": null,
"revision": "9faf1d7f80bb49b58bea2943e711d38fa8504060",
"version": "1.5.0"
}
"pins" : [
{
"identity" : "rearrange",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/Rearrange",
"state" : {
"revision" : "9faf1d7f80bb49b58bea2943e711d38fa8504060",
"version" : "1.5.0"
}
]
},
"version": 1
}
],
"version" : 2
}
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/Rearrange", from: "1.5.0"),
.package(url: "https://github.com/mattmassicotte/MainOffender", from: "0.1.0"),
],
targets: [
.target(name: "Internal", publicHeadersPath: "."),
.target(name: "TextStory", dependencies: ["Internal", "MainOffender", "Rearrange"]),
.target(name: "TextStory", dependencies: ["Internal", "Rearrange"]),
.target(name: "TextStoryTesting", dependencies: ["TextStory"]),
.testTarget(name: "TextStoryTests", dependencies: ["TextStory", "TextStoryTesting"]),
]
Expand Down
1 change: 0 additions & 1 deletion Sources/TextStory/LazyTextStoringMonitor.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation

@MainActor
public final class LazyTextStoringMonitor {
public let storingMonitor: TextStoringMonitor
public private(set) var maximumProcessedLocation: Int
Expand Down
39 changes: 14 additions & 25 deletions Sources/TextStory/TextMutationEventRouter.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import Foundation

import MainOffender

/// Routes `TSYTextStorageDelegate` calls to multiple `TextStoringMonitor` instances
///
/// This class can except all the `TSYTextStorageDelegate` calls and forward them
/// to multiple `TextStoringMonitor` instances. While it can be directly assigned
/// as the `storageDelegate` of `TSYTextStorageDelegate`, that could potentially be
/// inconvenient for your usage. In that case, just forward along the calls.
@MainActor
public final class TextMutationEventRouter: NSObject {
private var internalMonitor = CompositeTextStoringMonitor(monitors: [])
public var storingMonitorsCompletionBlock: ((TextStoring) -> Void)?
Expand All @@ -28,44 +25,36 @@ public final class TextMutationEventRouter: NSObject {
}

extension TextMutationEventRouter: TSYTextStorageDelegate {
public nonisolated func textStorage(_ textStorage: TSYTextStorage, willReplaceCharactersIn range: NSRange, with string: String) {
MainActor.runUnsafely {
precondition(processingTextChange == false, "Must not be processing a text change when another is begun")
public func textStorage(_ textStorage: TSYTextStorage, willReplaceCharactersIn range: NSRange, with string: String) {
precondition(processingTextChange == false, "Must not be processing a text change when another is begun")

let mutation = TextMutation(string: string, range: range, limit: textStorage.length)
let mutation = TextMutation(string: string, range: range, limit: textStorage.length)

self.pendingMutation = mutation
self.pendingMutation = mutation

internalMonitor.willApplyMutation(mutation, to: textStorage)
}
internalMonitor.willApplyMutation(mutation, to: textStorage)
}

public nonisolated func textStorage(_ textStorage: TSYTextStorage, didReplaceCharactersIn range: NSRange, with string: String) {
public func textStorage(_ textStorage: TSYTextStorage, didReplaceCharactersIn range: NSRange, with string: String) {
// it's necessary to recreate the limit, because at this point the storage has changed,
// and the length has now been modified
let delta = string.utf16.count - range.length
let preeditLimit = textStorage.length - delta

let mutation = TextMutation(string: string, range: range, limit: preeditLimit)

MainActor.runUnsafely {
precondition(mutation == pendingMutation, "Pre and post mutations must be the same")
precondition(mutation == pendingMutation, "Pre and post mutations must be the same")

internalMonitor.didApplyMutation(mutation, to: textStorage)
}
internalMonitor.didApplyMutation(mutation, to: textStorage)
}

public nonisolated func textStorageWillCompleteProcessingEdit(_ textStorage: TSYTextStorage) {
MainActor.runUnsafely {
internalMonitor.willCompleteChangeProcessing(of: pendingMutation, in: textStorage)
}
public func textStorageWillCompleteProcessingEdit(_ textStorage: TSYTextStorage) {
internalMonitor.willCompleteChangeProcessing(of: pendingMutation, in: textStorage)
}

public nonisolated func textStorageDidCompleteProcessingEdit(_ textStorage: TSYTextStorage) {
MainActor.runUnsafely {
internalMonitor.didCompleteChangeProcessing(of: pendingMutation, in: textStorage)

pendingMutation = nil
}
public func textStorageDidCompleteProcessingEdit(_ textStorage: TSYTextStorage) {
internalMonitor.didCompleteChangeProcessing(of: pendingMutation, in: textStorage)

pendingMutation = nil
}
}
121 changes: 0 additions & 121 deletions Sources/TextStory/TextStorageAdapter.swift

This file was deleted.

73 changes: 0 additions & 73 deletions Sources/TextStory/TextStorageEventRouter.swift

This file was deleted.

5 changes: 0 additions & 5 deletions Sources/TextStory/TextStoringMonitor.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import Foundation

public protocol TextStoringMonitor {
@MainActor
func willApplyMutation(_ mutation: TextMutation, to storage: TextStoring)
@MainActor
func didApplyMutation(_ mutation: TextMutation, to storage: TextStoring)
@MainActor
func willCompleteChangeProcessing(of mutation: TextMutation?, in storage: TextStoring)
@MainActor
func didCompleteChangeProcessing(of mutation: TextMutation?, in storage: TextStoring)
}

@MainActor
public extension TextStoringMonitor {
/// Invoke all the monitoring methods in order
func processMutation(_ mutation: TextMutation, in storage: TextStoring) {
Expand Down
Loading

0 comments on commit 5cd0787

Please sign in to comment.