forked from ReactiveCocoa/ReactiveSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Property.swift
861 lines (760 loc) · 35.2 KB
/
Property.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
import Darwin.POSIX.pthread
#else
import Glibc
#endif
// FIXME: The `Error == Never` constraint is retained for Swift 4.0.x
// compatibility, since `BindingSource` did not impose such constraint
// due to the absence of conditional conformance.
/// Represents a property that allows observation of its changes.
///
/// Only classes can conform to this protocol, because having a signal
/// for changes over time implies the origin must have a unique identity.
public protocol PropertyProtocol: class, BindingSource {
/// The current value of the property.
var value: Value { get }
/// The values producer of the property.
///
/// It produces a signal that sends the property's current value,
/// followed by all changes over time. It completes when the property
/// has deinitialized, or has no further change.
///
/// - note: If `self` is a composed property, the producer would be
/// bound to the lifetime of its sources.
var producer: SignalProducer<Value, Never> { get }
/// A signal that will send the property's changes over time. It
/// completes when the property has deinitialized, or has no further
/// change.
///
/// - note: If `self` is a composed property, the signal would be
/// bound to the lifetime of its sources.
var signal: Signal<Value, Never> { get }
}
/// Represents an observable property that can be mutated directly.
public protocol MutablePropertyProtocol: PropertyProtocol, BindingTargetProvider {
/// The current value of the property.
var value: Value { get set }
/// The lifetime of the property.
var lifetime: Lifetime { get }
}
/// Default implementation of `BindingTargetProvider` for mutable properties.
extension MutablePropertyProtocol {
public var bindingTarget: BindingTarget<Value> {
return BindingTarget(lifetime: lifetime) { [weak self] in self?.value = $0 }
}
}
/// Represents a mutable property that can be safety composed by exposing its
/// synchronization mechanic through the defined closure-based interface.
public protocol ComposableMutablePropertyProtocol: MutablePropertyProtocol {
/// Atomically performs an arbitrary action using the current value of the
/// variable.
///
/// - parameters:
/// - action: A closure that accepts current property value.
///
/// - returns: the result of the action.
func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result
/// Atomically modifies the variable.
///
/// - parameters:
/// - action: A closure that accepts old property value and returns a new
/// property value.
///
/// - returns: The result of the action.
func modify<Result>(_ action: (inout Value) throws -> Result) rethrows -> Result
}
// Property operators.
//
// A composed property is a transformed view of its sources, and does not
// own its lifetime. Its producer and signal are bound to the lifetime of
// its sources.
extension PropertyProtocol {
/// Lifts a unary SignalProducer operator to operate upon PropertyProtocol instead.
fileprivate func lift<U>(_ transform: @escaping (SignalProducer<Value, Never>) -> SignalProducer<U, Never>) -> Property<U> {
return Property(unsafeProducer: transform(producer))
}
/// Lifts a binary SignalProducer operator to operate upon PropertyProtocol instead.
fileprivate func lift<P: PropertyProtocol, U>(_ transform: @escaping (SignalProducer<Value, Never>) -> (SignalProducer<P.Value, Never>) -> SignalProducer<U, Never>) -> (P) -> Property<U> {
return { other in
return Property(unsafeProducer: transform(self.producer)(other.producer))
}
}
}
extension PropertyProtocol {
/// Maps the current value and all subsequent values to a new property.
///
/// - parameters:
/// - transform: A closure that will map the current `value` of this
/// `Property` to a new value.
///
/// - returns: A property that holds a mapped value from `self`.
public func map<U>(_ transform: @escaping (Value) -> U) -> Property<U> {
return lift { $0.map(transform) }
}
/// Map the current value and all susequent values to a new constant property.
///
/// - parameters:
/// - value: A new value.
///
/// - returns: A property that holds a mapped value from `self`.
public func map<U>(value: U) -> Property<U> {
return lift { $0.map(value: value) }
}
/// Maps the current value and all subsequent values to a new property
/// by applying a key path.
///
/// - parameters:
/// - keyPath: A key path relative to the property's `Value` type.
///
/// - returns: A property that holds a mapped value from `self`.
public func map<U>(_ keyPath: KeyPath<Value, U>) -> Property<U> {
return lift { $0.map(keyPath) }
}
/// Passes only the values of the property that pass the given predicate
/// to a new property.
///
/// - parameters:
/// - initial: A `Property` always needs a `value`. The initial `value` is necessary in case the
/// predicate excludes the first (or all) `value`s of this `Property`
/// - predicate: A closure that accepts value and returns `Bool` denoting
/// whether current `value` of this `Property` has passed the test.
///
/// - returns: A property that holds only values from `self` passing the given predicate.
public func filter(initial: Value, _ predicate: @escaping (Value) -> Bool) -> Property<Value> {
return Property(initial: initial, then: self.producer.filter(predicate))
}
/// Combines the current value and the subsequent values of two `Property`s in
/// the manner described by `Signal.combineLatest(with:)`.
///
/// - parameters:
/// - other: A property to combine `self`'s value with.
///
/// - returns: A property that holds a tuple containing values of `self` and
/// the given property.
public func combineLatest<P: PropertyProtocol>(with other: P) -> Property<(Value, P.Value)> {
return Property.combineLatest(self, other)
}
/// Zips the current value and the subsequent values of two `Property`s in
/// the manner described by `Signal.zipWith`.
///
/// - parameters:
/// - other: A property to zip `self`'s value with.
///
/// - returns: A property that holds a tuple containing values of `self` and
/// the given property.
public func zip<P: PropertyProtocol>(with other: P) -> Property<(Value, P.Value)> {
return Property.zip(self, other)
}
/// Forward events from `self` with history: values of the returned property
/// are a tuple whose first member is the previous value and whose second
/// member is the current value. `initial` is supplied as the first member
/// when `self` sends its first value.
///
/// - parameters:
/// - initial: A value that will be combined with the first value sent by
/// `self`.
///
/// - returns: A property that holds tuples that contain previous and
/// current values of `self`.
public func combinePrevious(_ initial: Value) -> Property<(Value, Value)> {
return lift { $0.combinePrevious(initial) }
}
/// Forward only values from `self` that are not considered equivalent to its
/// consecutive predecessor.
///
/// - note: The first value is always forwarded.
///
/// - parameters:
/// - isEquivalent: A closure to determine whether two values are equivalent.
///
/// - returns: A property which conditionally forwards values from `self`.
public func skipRepeats(_ isEquivalent: @escaping (Value, Value) -> Bool) -> Property<Value> {
return lift { $0.skipRepeats(isEquivalent) }
}
}
extension PropertyProtocol where Value: Equatable {
/// Forward only values from `self` that are not equal to its consecutive predecessor.
///
/// - note: The first value is always forwarded.
///
/// - returns: A property which conditionally forwards values from `self`.
public func skipRepeats() -> Property<Value> {
return lift { $0.skipRepeats() }
}
}
extension PropertyProtocol where Value: PropertyProtocol {
/// Flattens the inner property held by `self` (into a single property of
/// values), according to the semantics of the given strategy.
///
/// - parameters:
/// - strategy: The preferred flatten strategy.
///
/// - returns: A property that sends the values of its inner properties.
public func flatten(_ strategy: FlattenStrategy) -> Property<Value.Value> {
return lift { $0.flatMap(strategy) { $0.producer } }
}
}
extension PropertyProtocol {
/// Maps each property from `self` to a new property, then flattens the
/// resulting properties (into a single property), according to the
/// semantics of the given strategy.
///
/// - parameters:
/// - strategy: The preferred flatten strategy.
/// - transform: The transform to be applied on `self` before flattening.
///
/// - returns: A property that sends the values of its inner properties.
public func flatMap<P: PropertyProtocol>(_ strategy: FlattenStrategy, _ transform: @escaping (Value) -> P) -> Property<P.Value> {
return lift { $0.flatMap(strategy) { transform($0).producer } }
}
/// Forward only those values from `self` that have unique identities across
/// the set of all values that have been held.
///
/// - note: This causes the identities to be retained to check for
/// uniqueness.
///
/// - parameters:
/// - transform: A closure that accepts a value and returns identity
/// value.
///
/// - returns: A property that sends unique values during its lifetime.
public func uniqueValues<Identity: Hashable>(_ transform: @escaping (Value) -> Identity) -> Property<Value> {
return lift { $0.uniqueValues(transform) }
}
}
extension PropertyProtocol where Value: Hashable {
/// Forwards only those values from `self` that are unique across the set of
/// all values that have been seen.
///
/// - note: This causes the identities to be retained to check for uniqueness.
/// Providing a function that returns a unique value for each sent
/// value can help you reduce the memory footprint.
///
/// - returns: A property that sends unique values during its lifetime.
public func uniqueValues() -> Property<Value> {
return lift { $0.uniqueValues() }
}
}
extension PropertyProtocol {
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol>(_ a: A, _ b: B) -> Property<(A.Value, B.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol>(_ a: A, _ b: B, _ c: C) -> Property<(Value, B.Value, C.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D) -> Property<(Value, B.Value, C.Value, D.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E) -> Property<(Value, B.Value, C.Value, D.Value, E.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e, f) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e, f, g) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e, f, g, h) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol, I: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H, _ i: I) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value, I.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e, f, g, h, i) }
}
/// Combines the values of all the given properties, in the manner described
/// by `combineLatest(with:)`.
public static func combineLatest<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol, I: PropertyProtocol, J: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H, _ i: I, _ j: J) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value, I.Value, J.Value)> where A.Value == Value {
return a.lift { SignalProducer.combineLatest($0, b, c, d, e, f, g, h, i, j) }
}
/// Combines the values of all the given producers, in the manner described by
/// `combineLatest(with:)`. Returns nil if the sequence is empty.
public static func combineLatest<S: Sequence>(_ properties: S) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
guard !producers.isEmpty else {
return nil
}
return Property(unsafeProducer: SignalProducer.combineLatest(producers))
}
/// Combines the values of all the given `Property`s, in the manner described by
/// `combineLatest(with:)`. If `properties` is empty, the resulting `Property` would have `emptySentinel` as its
/// constant value.
public static func combineLatest<S: Sequence>(
_ properties: S,
emptySentinel: [S.Iterator.Element.Value]
) -> Property<[S.Iterator.Element.Value]> where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
return Property(unsafeProducer: SignalProducer.combineLatest(producers, emptySentinel: emptySentinel))
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol>(_ a: A, _ b: B) -> Property<(Value, B.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol>(_ a: A, _ b: B, _ c: C) -> Property<(Value, B.Value, C.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D) -> Property<(Value, B.Value, C.Value, D.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E) -> Property<(Value, B.Value, C.Value, D.Value, E.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e, f) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e, f, g) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e, f, g, h) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol, I: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H, _ i: I) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value, I.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e, f, g, h, i) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`.
public static func zip<A: PropertyProtocol, B: PropertyProtocol, C: PropertyProtocol, D: PropertyProtocol, E: PropertyProtocol, F: PropertyProtocol, G: PropertyProtocol, H: PropertyProtocol, I: PropertyProtocol, J: PropertyProtocol>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E, _ f: F, _ g: G, _ h: H, _ i: I, _ j: J) -> Property<(Value, B.Value, C.Value, D.Value, E.Value, F.Value, G.Value, H.Value, I.Value, J.Value)> where A.Value == Value {
return a.lift { SignalProducer.zip($0, b, c, d, e, f, g, h, i, j) }
}
/// Zips the values of all the given properties, in the manner described by
/// `zip(with:)`. Returns nil if the sequence is empty.
public static func zip<S: Sequence>(_ properties: S) -> Property<[S.Iterator.Element.Value]>? where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
guard !producers.isEmpty else {
return nil
}
return Property(unsafeProducer: SignalProducer.zip(producers))
}
/// Combines the values of all the given `Property`s, in the manner described by
/// `zip(with:)`. If `properties` is empty, the resulting `Property` would have `emptySentinel` as its
/// constant value.
public static func zip<S: Sequence>(
_ properties: S,
emptySentinel: [S.Iterator.Element.Value]
) -> Property<[S.Iterator.Element.Value]> where S.Iterator.Element: PropertyProtocol {
let producers = properties.map { $0.producer }
return Property(unsafeProducer: SignalProducer.zip(producers, emptySentinel: emptySentinel))
}
}
extension PropertyProtocol where Value == Bool {
/// Create a property that computes a logical NOT in the latest values of `self`.
///
/// - returns: A property that contains the logical NOT results.
public func negate() -> Property<Value> {
return self.lift { $0.negate() }
}
/// Create a property that computes a logical AND between the latest values of `self`
/// and `property`.
///
/// - parameters:
/// - property: Property to be combined with `self`.
///
/// - returns: A property that contains the logical AND results.
public func and<P: PropertyProtocol>(_ property: P) -> Property<Value> where P.Value == Value {
return self.lift(SignalProducer.and)(property)
}
/// Create a property that computes a logical AND between the latest values of `properties`.
///
/// - parameters:
/// - property: Collection of properties to be combined.
///
/// - returns: A property that contains the logical AND results.
public static func all<P: PropertyProtocol, Properties: Collection>(_ properties: Properties) -> Property<Value> where P.Value == Value, Properties.Element == P {
return Property(initial: properties.map { $0.value }.reduce(true) { $0 && $1 }, then: SignalProducer.all(properties))
}
/// Create a property that computes a logical OR between the latest values of `self`
/// and `property`.
///
/// - parameters:
/// - property: Property to be combined with `self`.
///
/// - returns: A property that contains the logical OR results.
public func or<P: PropertyProtocol>(_ property: P) -> Property<Value> where P.Value == Value {
return self.lift(SignalProducer.or)(property)
}
/// Create a property that computes a logical OR between the latest values of `properties`.
///
/// - parameters:
/// - properties: Collection of properties to be combined.
///
/// - returns: A property that contains the logical OR results.
public static func any<P: PropertyProtocol, Properties: Collection>(_ properties: Properties) -> Property<Value> where P.Value == Value, Properties.Element == P {
return Property(initial: properties.map { $0.value }.reduce(false) { $0 || $1 }, then: SignalProducer.any(properties))
}
}
/// A read-only property that can be observed for its changes over time. There
/// are three categories of read-only properties:
///
/// # Constant property
/// Created by `Property(value:)`, the producer and signal of a constant
/// property would complete immediately when it is initialized.
///
/// # Existential property
/// Created by `Property(capturing:)`, it wraps any arbitrary `PropertyProtocol`
/// types, and passes through the behavior. Note that it would retain the
/// wrapped property.
///
/// Existential property would be deprecated when generalized existential
/// eventually lands in Swift.
///
/// # Composed property
/// A composed property presents a composed view of its sources, which can be
/// one or more properties, a producer, or a signal. It can be created using
/// property composition operators, `Property(_:)` or `Property(initial:then:)`.
///
/// It does not own its lifetime, and its producer and signal are bound to the
/// lifetime of its sources. It also does not have an influence on its sources,
/// so retaining a composed property would not prevent its sources from
/// deinitializing.
///
/// Note that composed properties do not retain any of its sources.
@propertyWrapper
public final class Property<Value>: PropertyProtocol {
private let _value: () -> Value
/// The current value of the property.
public var value: Value {
return _value()
}
@inlinable
public var wrappedValue: Value {
return value
}
@inlinable
public var projectedValue: Property<Value> {
return self
}
/// A producer for Signals that will send the property's current
/// value, followed by all changes over time, then complete when the
/// property has deinitialized or has no further changes.
///
/// - note: If `self` is a composed property, the producer would be
/// bound to the lifetime of its sources.
public let producer: SignalProducer<Value, Never>
/// A signal that will send the property's changes over time, then
/// complete when the property has deinitialized or has no further changes.
///
/// - note: If `self` is a composed property, the signal would be
/// bound to the lifetime of its sources.
public let signal: Signal<Value, Never>
/// Initializes a constant property.
///
/// - parameters:
/// - property: A value of the constant property.
public init(value: Value) {
_value = { value }
producer = SignalProducer(value: value)
signal = Signal<Value, Never>.empty
}
/// Initializes an existential property which wraps the given property.
///
/// - note: The resulting property retains the given property.
///
/// - parameters:
/// - property: A property to be wrapped.
public init<P: PropertyProtocol>(capturing property: P) where P.Value == Value {
_value = { property.value }
producer = property.producer
signal = property.signal
}
/// Initializes a composed property which reflects the given property.
///
/// - note: The resulting property does not retain the given property.
///
/// - parameters:
/// - property: A property to be wrapped.
public convenience init<P: PropertyProtocol>(_ property: P) where P.Value == Value {
self.init(unsafeProducer: property.producer)
}
/// Initializes a composed property that first takes on `initial`, then each
/// value sent on a signal created by `producer`.
///
/// - parameters:
/// - initial: Starting value for the property.
/// - values: A producer that will start immediately and send values to
/// the property.
public convenience init(initial: Value, then values: SignalProducer<Value, Never>) {
self.init(unsafeProducer: SignalProducer { observer, lifetime in
observer.send(value: initial)
lifetime += values.start(Signal.Observer(mappingInterruptedToCompleted: observer))
})
}
/// Initializes a composed property that first takes on `initial`, then each
/// value sent on a signal created by `producer`.
///
/// - parameters:
/// - initial: Starting value for the property.
/// - values: A producer that will start immediately and send values to
/// the property.
public convenience init<Values: SignalProducerConvertible>(initial: Value, then values: Values) where Values.Value == Value, Values.Error == Never {
self.init(initial: initial, then: values.producer)
}
/// Initialize a composed property from a producer that promises to send
/// at least one value synchronously in its start handler before sending any
/// subsequent event.
///
/// - important: The producer and the signal of the created property would
/// complete only when the `unsafeProducer` completes.
///
/// - warning: If the producer fails its promise, a fatal error would be
/// raised.
///
/// - warning: `unsafeProducer` should not emit any `interrupted` event unless it is
/// a result of being interrupted by the downstream.
///
/// - parameters:
/// - unsafeProducer: The composed producer for creating the property.
fileprivate init(unsafeProducer: SignalProducer<Value, Never>) {
// The ownership graph:
//
// ------------ weak ----------- strong ------------------
// | Upstream | ~~~~~~~~> | Box | <======== | SignalProducer | <=== strong
// ------------ ----------- // ------------------ \\
// \\ // \\
// \\ ------------ weak ----------- <== ------------
// ==> | Observer | ~~~~> | Relay | <=========================== | Property |
// strong ------------ ----------- strong ------------
let box = PropertyBox<Value?>(nil)
// A composed property tracks its active consumers through its relay signal, and
// interrupts `unsafeProducer` if the relay signal terminates.
let disposable = SerialDisposable()
let (relay, observer) = Signal<Value, Never>.pipe(disposable: disposable)
disposable.inner = unsafeProducer.start { [weak box] event in
// `observer` receives `interrupted` only as a result of the termination of
// `signal`, and would not be delivered anyway. So transforming
// `interrupted` to `completed` is unnecessary here.
guard let box = box else {
// Just forward the event, since no one owns the box or IOW no demand
// for a cached latest value.
return observer.send(event)
}
box.begin { storage in
storage.modify { value in
if let newValue = event.value {
value = newValue
}
}
observer.send(event)
}
}
// Verify that an initial is sent. This is friendlier than deadlocking
// in the event that one isn't.
guard box.value != nil else {
fatalError("The producer promised to send at least one value. Received none.")
}
_value = { box.value! }
signal = relay
producer = SignalProducer { [box, relay] observer, lifetime in
box.withValue { value in
observer.send(value: value!)
lifetime += relay.observe(Signal.Observer(mappingInterruptedToCompleted: observer))
}
}
}
}
extension Property where Value: OptionalProtocol {
/// Initializes a composed property that first takes on `initial`, then each
/// value sent on a signal created by `producer`.
///
/// - parameters:
/// - initial: Starting value for the property.
/// - values: A producer that will start immediately and send values to
/// the property.
public convenience init(initial: Value, then values: SignalProducer<Value.Wrapped, Never>) {
self.init(initial: initial, then: values.map(Value.init(reconstructing:)))
}
/// Initializes a composed property that first takes on `initial`, then each
/// value sent on a signal created by `producer`.
///
/// - parameters:
/// - initial: Starting value for the property.
/// - values: A producer that will start immediately and send values to
/// the property.
public convenience init<Values: SignalProducerConvertible>(initial: Value, then values: Values) where Values.Value == Value.Wrapped, Values.Error == Never {
self.init(initial: initial, then: values.producer)
}
}
/// A mutable property of type `Value` that allows observation of its changes.
///
/// Instances of this class are thread-safe.
@propertyWrapper
public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
private let token: Lifetime.Token
private let observer: Signal<Value, Never>.Observer
private let box: PropertyBox<Value>
/// The current value of the property.
///
/// Setting this to a new value will notify all observers of `signal`, or
/// signals created using `producer`.
public var value: Value {
get { return box.value }
set { modify { $0 = newValue } }
}
@inlinable
public var wrappedValue: Value {
get { value }
set { value = newValue }
}
@inlinable
public var projectedValue: MutableProperty<Value> {
return self
}
/// The lifetime of the property.
public let lifetime: Lifetime
/// A signal that will send the property's changes over time,
/// then complete when the property has deinitialized.
public let signal: Signal<Value, Never>
/// A producer for Signals that will send the property's current value,
/// followed by all changes over time, then complete when the property has
/// deinitialized.
public var producer: SignalProducer<Value, Never> {
return SignalProducer { [box, signal] observer, lifetime in
box.withValue { value in
observer.send(value: value)
lifetime += signal.observe(Signal.Observer(mappingInterruptedToCompleted: observer))
}
}
}
/// Initializes a mutable property that first takes on `initialValue`
///
/// - parameters:
/// - initialValue: Starting value for the mutable property.
public init(_ initialValue: Value) {
(signal, observer) = Signal.pipe()
(lifetime, token) = Lifetime.make()
/// Need a recursive lock around `value` to allow recursive access to
/// `value`. Note that recursive sets will still deadlock because the
/// underlying producer prevents sending recursive events.
box = PropertyBox(initialValue)
}
/// Initializes a mutable property that first takes on `initialValue`
///
/// - parameters:
/// - initialValue: Starting value for the mutable property.
public convenience init(wrappedValue: Value) {
self.init(wrappedValue)
}
/// Atomically replaces the contents of the variable.
///
/// - parameters:
/// - newValue: New property value.
///
/// - returns: The previous property value.
@discardableResult
public func swap(_ newValue: Value) -> Value {
return modify { value in
defer { value = newValue }
return value
}
}
/// Atomically modifies the variable.
///
/// - parameters:
/// - action: A closure that accepts an inout reference to the value.
///
/// - returns: The result of the action.
@discardableResult
public func modify<Result>(_ action: (inout Value) throws -> Result) rethrows -> Result {
return try box.begin { storage in
defer { observer.send(value: storage.value) }
return try storage.modify(action)
}
}
/// Atomically modifies the variable.
///
/// - warning: The reference should not be escaped.
///
/// - parameters:
/// - action: A closure that accepts a reference to the property storage.
///
/// - returns: The result of the action.
@discardableResult
internal func begin<Result>(_ action: (PropertyStorage<Value>) throws -> Result) rethrows -> Result {
return try box.begin(action)
}
/// Atomically performs an arbitrary action using the current value of the
/// variable.
///
/// - parameters:
/// - action: A closure that accepts current property value.
///
/// - returns: the result of the action.
@discardableResult
public func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result {
return try box.withValue { try action($0) }
}
deinit {
observer.sendCompleted()
}
}
internal struct PropertyStorage<Value> {
private unowned let box: PropertyBox<Value>
var value: Value {
return box._value
}
func modify<Result>(_ action: (inout Value) throws -> Result) rethrows -> Result {
guard !box.isModifying else { fatalError("Nested modifications violate exclusivity of access.") }
box.isModifying = true
defer { box.isModifying = false }
return try action(&box._value)
}
fileprivate init(_ box: PropertyBox<Value>) {
self.box = box
}
}
/// A reference counted box which holds a recursive lock and a value storage.
///
/// The requirement of a `Value?` storage from composed properties prevents further
/// implementation sharing with `MutableProperty`.
private final class PropertyBox<Value> {
private let lock: Lock.PthreadLock
fileprivate var _value: Value
fileprivate var isModifying = false
internal var value: Value {
lock.lock()
defer { lock.unlock() }
return _value
}
init(_ value: Value) {
_value = value
lock = Lock.PthreadLock(recursive: true)
}
func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result {
lock.lock()
defer { lock.unlock() }
return try action(_value)
}
func begin<Result>(_ action: (PropertyStorage<Value>) throws -> Result) rethrows -> Result {
lock.lock()
defer { lock.unlock() }
return try action(PropertyStorage(self))
}
}