-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45d3d62
commit 1d48b53
Showing
13 changed files
with
539 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Foundation | ||
|
||
import RangeState | ||
|
||
extension TextSystemInterface { | ||
typealias Provider = ThreePhaseRangeValidator<Content>.Provider | ||
typealias ContentRange = ThreePhaseRangeValidator<Content>.ContentRange | ||
|
||
@MainActor | ||
func validation(for application: TokenApplication, in contentRange: ContentRange) -> Validation { | ||
let effectiveRange = application.range ?? contentRange.value | ||
|
||
applyStyles(for: application) | ||
|
||
return .success(effectiveRange) | ||
} | ||
|
||
@MainActor | ||
func asyncValidate( | ||
_ contentRange: ContentRange, | ||
provider: @MainActor (NSRange) async -> TokenApplication | ||
) async -> Validation { | ||
guard contentRange.version == content.currentVersion else { return .stale } | ||
|
||
// https://github.com/apple/swift/pull/71143 | ||
let application = await provider(contentRange.value) | ||
|
||
// second check after the awit | ||
guard contentRange.version == content.currentVersion else { return .stale } | ||
|
||
return validation(for: application, in: contentRange) | ||
} | ||
|
||
@MainActor | ||
func validationProvider(with provider: TokenProvider) -> Provider { | ||
.init( | ||
syncValue: { contentRange in | ||
guard contentRange.version == self.content.currentVersion else { return .stale } | ||
|
||
guard let application = provider.sync(contentRange.value) else { | ||
return nil | ||
} | ||
|
||
return validation(for: application, in: contentRange) | ||
}, | ||
mainActorAsyncValue: { contentRange in | ||
return await asyncValidate(contentRange, provider: provider.mainActorAsync) | ||
} | ||
) | ||
} | ||
} | ||
extension TextSystemInterface { | ||
typealias Styler = ThreePhaseTextSystemStyler<Self> | ||
typealias FallbackHandler = ThreePhaseRangeValidator<Self.Content>.FallbackHandler | ||
typealias SecondaryValidationProvider = ThreePhaseRangeValidator<Self.Content>.SecondaryValidationProvider | ||
|
||
@MainActor | ||
func validatorFallbackHandler( | ||
with provider: @escaping Styler.FallbackTokenProvider | ||
) -> FallbackHandler { | ||
{ range in | ||
let application = provider(range) | ||
|
||
applyStyles(for: application) | ||
} | ||
} | ||
|
||
@MainActor | ||
func validatorSecondaryHandler( | ||
with provider: @escaping Styler.SecondaryValidationProvider | ||
) -> SecondaryValidationProvider { | ||
{ await asyncValidate($0, provider: provider) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Foundation | ||
|
||
import RangeState | ||
|
||
@MainActor | ||
public final class ThreePhaseTextSystemStyler<Interface: TextSystemInterface> { | ||
public typealias FallbackTokenProvider = (NSRange) -> TokenApplication | ||
public typealias SecondaryValidationProvider = (NSRange) async -> TokenApplication | ||
|
||
private let textSystem: Interface | ||
private let validator: ThreePhaseRangeValidator<Interface.Content> | ||
|
||
public init( | ||
textSystem: Interface, | ||
tokenProvider: TokenProvider, | ||
fallbackHandler: @escaping FallbackTokenProvider, | ||
secondaryValidationProvider: @escaping SecondaryValidationProvider | ||
) { | ||
self.textSystem = textSystem | ||
|
||
let tokenValidator = TokenSystemValidator( | ||
textSystem: textSystem, | ||
tokenProvider: tokenProvider | ||
) | ||
|
||
self.validator = ThreePhaseRangeValidator( | ||
configuration: .init( | ||
versionedContent: textSystem.content, | ||
provider: tokenValidator.validationProvider, | ||
fallbackHandler: textSystem.validatorFallbackHandler(with: fallbackHandler), | ||
secondaryProvider: textSystem.validatorSecondaryHandler(with: secondaryValidationProvider), | ||
secondaryValidationDelay: 3.0, | ||
priorityRangeProvider: { textSystem.visibleRange } | ||
) | ||
) | ||
} | ||
|
||
public func didChangeContent(in range: NSRange, delta: Int) { | ||
validator.contentChanged(in: range, delta: delta) | ||
} | ||
|
||
public func invalidate(_ target: RangeTarget) { | ||
validator.invalidate(target) | ||
} | ||
|
||
public func validate(_ target: RangeTarget) { | ||
let priorityRange = textSystem.visibleRange | ||
|
||
validator.validate(target, prioritizing: priorityRange) | ||
} | ||
|
||
public func validate() { | ||
let priorityRange = textSystem.visibleRange | ||
|
||
validator.validate(.range(priorityRange), prioritizing: priorityRange) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.