Skip to content

Commit f930ce4

Browse files
committed
(!) added reentrant updates
1 parent 1e29bad commit f930ce4

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

Sources/DiffableTextKitXUIKit/DiffableTextField.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
9393
// MARK: State
9494
//=--------------------------------------------------------------------=
9595

96-
@usableFromInline let lock = Lock()
96+
@usableFromInline let lock = Lock()
97+
@usableFromInline var update = Update()
9798

9899
@usableFromInline var cache: Cache!
99100
@usableFromInline var context: Context!
@@ -265,32 +266,40 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
265266
//=----------------------------------=
266267
// Upstream, Downstream
267268
//=----------------------------------=
268-
Status(upstream.style, upstream.value.wrappedValue, downstream.focus)
269+
Status(upstream.style, upstream.value, downstream.focus)
269270
}
270271

271272
@inlinable func push(_ update: Update) {
273+
//=----------------------------------=
274+
// State
275+
//=----------------------------------=
276+
self.update += update
277+
//=----------------------------------=
278+
// Value
279+
//=----------------------------------=
280+
if let _ = self.update.remove(.value) {
281+
self.upstream.value = self.context.value
282+
//=------------------------------=
283+
// Reentrance
284+
//=------------------------------=
285+
return
286+
}
272287
//=----------------------------------=
273288
// Lock
274289
//=----------------------------------=
275290
self.lock.perform {
276291
//=------------------------------=
277292
// Text
278293
//=------------------------------=
279-
if update.contains(.text) {
294+
if let _ = self.update.remove(.text) {
280295
self.downstream.text = context.text
281296
}
282297
//=------------------------------=
283298
// Selection
284299
//=------------------------------=
285-
if update.contains(.selection) {
300+
if let _ = self.update.remove(.selection) {
286301
self.downstream.selection = context.selection()
287302
}
288-
//=------------------------------=
289-
// Value
290-
//=------------------------------=
291-
if update.contains(.value) {
292-
self.upstream.value.wrappedValue = context.value
293-
}
294303
}
295304
}
296305
}

Sources/DiffableTextKitXUIKit/Streams/Upstream.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,33 @@ import DiffableTextKit
1313
import SwiftUI
1414

1515
//*============================================================================*
16-
// MARK: * Upstream [...]
16+
// MARK: * Upstream
1717
//*============================================================================*
1818

1919
@usableFromInline struct Upstream<Style: DiffableTextStyle> {
2020

2121
//=------------------------------------------------------------------------=
22+
// MARK: State
23+
//=------------------------------------------------------------------------=
2224

2325
@usableFromInline let style: Style
24-
@usableFromInline let value: Binding<Style.Value>
26+
@usableFromInline let proxy: Binding<Style.Value>
2527

28+
//=------------------------------------------------------------------------=
29+
// MARK: Initializers
2630
//=------------------------------------------------------------------------=
2731

2832
@inlinable init(_ view: DiffableTextField<Style>, _ environment: EnvironmentValues) {
29-
self.style = view.style.locale(environment.locale); self.value = view.value
33+
self.style = view.style.locale(environment.locale); self.proxy = view.value
34+
}
35+
36+
//=------------------------------------------------------------------------=
37+
// MARK: Accessors
38+
//=------------------------------------------------------------------------=
39+
40+
@inlinable var value: Style.Value {
41+
get { proxy.wrappedValue }
42+
set { proxy.wrappedValue = newValue }
3043
}
3144
}
3245

0 commit comments

Comments
 (0)