@@ -94,6 +94,8 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
9494 // MARK: State
9595 //=--------------------------------------------------------------------=
9696
97+ @usableFromInline let lock = Lock ( )
98+
9799 @usableFromInline var cache : Cache !
98100 @usableFromInline var context : Context !
99101 @usableFromInline var update = Update ( )
@@ -174,7 +176,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
174176 shouldChangeCharactersIn nsrange: NSRange ,
175177 replacementString text: String ) -> Bool {
176178 //=----------------------------------=
177- // Lock
179+ // Wait
178180 //=----------------------------------=
179181 guard !update else { return false }
180182 //=----------------------------------=
@@ -205,9 +207,21 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
205207
206208 @inlinable @inline ( never) public func textFieldDidChangeSelection( _ view: UITextField ) {
207209 //=----------------------------------=
210+ // Locked
211+ //=----------------------------------=
212+ guard !lock. isLocked else { return }
213+ //=----------------------------------=
214+ // Reentrance, Known Due To Push Lock
215+ //=----------------------------------=
216+ if !update. isEmpty {
217+ //=------------------------------=
218+ // Push
219+ //=------------------------------=
220+ self . push ( [ . text, . selection] )
221+ //=----------------------------------=
208222 // Marked
209223 //=----------------------------------=
210- if let _ = view. markedTextRange {
224+ } else if let _ = view. markedTextRange {
211225 //=------------------------------=
212226 // Push
213227 //=------------------------------=
@@ -216,10 +230,6 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
216230 // Normal
217231 //=----------------------------------=
218232 } else {
219- //=------------------------------=
220- // Lock
221- //=------------------------------=
222- guard !update else { return }
223233 //=------------------------------=
224234 // Pull
225235 //=------------------------------=
@@ -290,16 +300,21 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
290300 return
291301 }
292302 //=----------------------------------=
293- // Text
294- //=----------------------------------=
295- if let _ = self . update. remove ( . text) {
296- self . downstream. text = context. text
297- }
298- //=----------------------------------=
299- // Selection
303+ // Lock
300304 //=----------------------------------=
301- if let _ = self . update. remove ( . selection) {
302- self . downstream. selection = context. selection ( )
305+ self . lock. perform {
306+ //=------------------------------=
307+ // Text
308+ //=------------------------------=
309+ if let _ = self . update. remove ( . text) {
310+ self . downstream. text = context. text
311+ }
312+ //=------------------------------=
313+ // Selection
314+ //=------------------------------=
315+ if let _ = self . update. remove ( . selection) {
316+ self . downstream. selection = context. selection ( )
317+ }
303318 }
304319 }
305320 }
0 commit comments