Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Remove the state channel.
Browse files Browse the repository at this point in the history
Reviewers: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, appsforartists

Reviewed By: O2 Material Motion, #material_motion, appsforartists

Subscribers: appsforartists

Tags: #material_motion

Differential Revision: http://codereview.cc/D2691
  • Loading branch information
Jeff Verkoeyen committed Feb 16, 2017
1 parent c3995f7 commit c2657b8
Show file tree
Hide file tree
Showing 24 changed files with 23 additions and 347 deletions.
4 changes: 0 additions & 4 deletions examples/apps/Catalog/Catalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
666FAA8B1D384A6B000363DA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 666FAA8A1D384A6B000363DA /* Assets.xcassets */; };
666FAA8E1D384A6B000363DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 666FAA8C1D384A6B000363DA /* LaunchScreen.storyboard */; };
669F80CA1E41A20E00E46B38 /* Stickers.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 669F80C91E41A20E00E46B38 /* Stickers.xcassets */; };
66AA65AC1E00C37F000052A8 /* toggleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66AA65AB1E00C37F000052A8 /* toggleTests.swift */; };
66E6F2371E3FC791003158D3 /* PhotoAlbum.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 66E6F2361E3FC791003158D3 /* PhotoAlbum.xcassets */; };
C1F5E1D37D0DA71224EFC15E /* Pods_Catalog.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E95CBDC1BABF9E6E385A124B /* Pods_Catalog.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -58,7 +57,6 @@
666FAA941D384A6B000363DA /* UnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
666FAA9A1D384A6B000363DA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = ../UnitTests/Info.plist; sourceTree = "<group>"; };
669F80C91E41A20E00E46B38 /* Stickers.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Stickers.xcassets; sourceTree = SOURCE_ROOT; };
66AA65AB1E00C37F000052A8 /* toggleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = toggleTests.swift; sourceTree = "<group>"; };
66E6F2361E3FC791003158D3 /* PhotoAlbum.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = PhotoAlbum.xcassets; sourceTree = SOURCE_ROOT; };
B05E63B8F92D94A8819B0ABB /* Pods_UnitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UnitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BC5B661FDF1C5375A0D3B88E /* Pods-UnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.release.xcconfig"; path = "../../../Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -123,7 +121,6 @@
6627FC171DF7BC7D003DB567 /* _mapTests.swift */,
6627FC181DF7BC7D003DB567 /* _operatorTests.swift */,
6627FC2E1DFFAEB7003DB567 /* CGPointTests.swift */,
66AA65AB1E00C37F000052A8 /* toggleTests.swift */,
);
path = operator;
sourceTree = "<group>";
Expand Down Expand Up @@ -432,7 +429,6 @@
6627FC1E1DF7BF59003DB567 /* ReactivePropertyTests.swift in Sources */,
6627FC2F1DFFAEB7003DB567 /* CGPointTests.swift in Sources */,
6627FC1B1DF7BC7D003DB567 /* _operatorTests.swift in Sources */,
66AA65AC1E00C37F000052A8 /* toggleTests.swift in Sources */,
66090B8A1E046CE600B1D598 /* Spring.swift in Sources */,
6627FC0E1DF7B1A2003DB567 /* MotionObservableTests.swift in Sources */,
66090B841E03715F00B1D598 /* PropertyObservation.swift in Sources */,
Expand Down
12 changes: 2 additions & 10 deletions src/MotionObservable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import IndefiniteObservable

// Channels are functions that pass values down the stream.
public typealias NextChannel<T> = (T) -> Void
public typealias StateChannel = (MotionState) -> Void

/** A Core Animation channel event. */
public enum CoreAnimationChannelEvent {
Expand All @@ -43,11 +42,8 @@ public typealias CoreAnimationChannel = (CoreAnimationChannelEvent) -> Void
public final class MotionObservable<T>: IndefiniteObservable<MotionObserver<T>> {
/** Sugar for subscribing a MotionObserver. */
public func subscribe(next: @escaping NextChannel<T>,
state: @escaping StateChannel,
coreAnimation: @escaping CoreAnimationChannel) -> Subscription {
return super.subscribe(observer: MotionObserver<T>(next: next,
state: state,
coreAnimation: coreAnimation))
return super.subscribe(observer: MotionObserver<T>(next: next, coreAnimation: coreAnimation))
}
}

Expand Down Expand Up @@ -78,16 +74,12 @@ public enum MotionState {
public final class MotionObserver<T>: Observer {
public typealias Value = T

public init(next: @escaping NextChannel<T>,
state: @escaping StateChannel,
coreAnimation: @escaping CoreAnimationChannel) {
public init(next: @escaping NextChannel<T>, coreAnimation: @escaping CoreAnimationChannel) {
self.next = next
self.state = state
self.coreAnimation = coreAnimation
}

public let next: NextChannel<T>
public let state: StateChannel
public let coreAnimation: CoreAnimationChannel
}

Expand Down
31 changes: 2 additions & 29 deletions src/MotionRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,7 @@ public class MotionRuntime {
/** Subscribes to the stream, writes its output to the given property, and observes its state. */
private func write<O: MotionObservableConvertible, T>(_ stream: O, to property: ReactiveProperty<T>) where O.T == T {
let token = NSUUID().uuidString
subscriptions.append(stream.asStream().subscribe(next: { property.value = $0 }, state: { [weak self] state in
property.state(state)

guard let strongSelf = self else { return }
strongSelf.stateDidChange(state, for: token)

}, coreAnimation: property.coreAnimation))
subscriptions.append(stream.asStream().subscribe(next: { property.value = $0 }, coreAnimation: property.coreAnimation))
}

/**
Expand Down Expand Up @@ -142,34 +136,13 @@ public class MotionRuntime {
body()
}
}
}, state: { _ in }, coreAnimation: { _ in }))
}, coreAnimation: { _ in }))
}
self.subscriptions.append(contentsOf: subscriptions)
}

private func stateDidChange(_ state: MotionState, for token: String) {
if state == .active {
activeSubscriptions.insert(token)
} else {
activeSubscriptions.remove(token)
}

let oldState = self.state.value
let newState: MotionState = activeSubscriptions.count > 0 ? .active : .atRest
if oldState != newState {
self.state.value = newState
}

if let parent = parent {
parent.stateDidChange(state, for: token)
}
}

private weak var parent: MotionRuntime?
private var children: [MotionRuntime] = []
private var subscriptions: [Subscription] = []
private var viewInteractions: [ViewInteraction] = []

private typealias Token = String
private var activeSubscriptions = Set<Token>()
}
15 changes: 0 additions & 15 deletions src/ReactiveProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ public final class ReactiveProperty<T> {
let stream = MotionObservable<T> { observer in
self.observers.append(observer)

if self.state == .active {
observer.state(.active)
}
observer.next(self.value)
if self.state == .atRest {
observer.state(.atRest)
}

return {
if let index = self.observers.index(where: { $0 === observer }) {
Expand Down Expand Up @@ -90,15 +84,6 @@ public final class ReactiveProperty<T> {
self._coreAnimation = coreAnimation
}

/** Informs all observers of the given state. */
public func state(_ state: MotionState) {
self.state = state

for observer in observers {
observer.state(state)
}
}

/**
Forwards the invocation to the channel if a core animation channel was provided to this property,
otherwise throws an assertion.
Expand Down
4 changes: 2 additions & 2 deletions src/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class Timeline {
for layer in strongSelf.layers {
strongSelf.updateTiming(for: layer)
}
}, state: { _ in }, coreAnimation: { _ in }))
}, coreAnimation: { _ in }))

subscriptions.append(timeOffset.asStream().dedupe().subscribe(next: { [weak self] offset in
guard let strongSelf = self else { return }
guard strongSelf.paused.value else { return }
for layer in strongSelf.layers {
strongSelf.updateTimeOffset(for: layer, timeOffset: offset)
}
}, state: { _ in }, coreAnimation: { _ in }))
}, coreAnimation: { _ in }))
}
private var subscriptions: [Subscription] = []

Expand Down
4 changes: 2 additions & 2 deletions src/operators/arcMove.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public func arcMove<O1: MotionObservableConvertible, O2: MotionObservableConvert
latestFrom = fromValue
checkAndEmit()

}, state: { _ in }, coreAnimation: { _ in })
}, coreAnimation: { _ in })

let toSubscription = to.asStream().subscribe(next: { toValue in
latestTo = toValue
checkAndEmit()

}, state: { _ in }, coreAnimation: { _ in })
}, coreAnimation: { _ in })

return {
fromSubscription.unsubscribe()
Expand Down
4 changes: 2 additions & 2 deletions src/operators/foundation/_nextOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension MotionObservableConvertible {
return MotionObservable<U> { observer in
return self.asStream().subscribe(next: {
return operation($0, observer.next)
}, state: observer.state, coreAnimation: { _ in
}, coreAnimation: { _ in
assertionFailure("Core animation is not supported by this operator.")
}).unsubscribe
}
Expand All @@ -45,7 +45,7 @@ extension MotionObservableConvertible {
return MotionObservable<U> { observer in
return self.asStream().subscribe(next: {
return operation($0, observer.next)
}, state: observer.state, coreAnimation: {
}, coreAnimation: {
return coreAnimation($0, observer.coreAnimation)
}).unsubscribe
}
Expand Down
3 changes: 1 addition & 2 deletions src/operators/merge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ extension MotionObservableConvertible {
public func merge(with stream: MotionObservable<T>) -> MotionObservable<T> {
return MotionObservable<T> { observer in
let upstreamSubscription = self.asStream().subscribe(next: observer.next,
state: observer.state,
coreAnimation: observer.coreAnimation)
let subscription = stream.subscribe(next: observer.next, state: { _ in }, coreAnimation: { _ in })
let subscription = stream.subscribe(next: observer.next, coreAnimation: { _ in })

return {
subscription.unsubscribe()
Expand Down
11 changes: 0 additions & 11 deletions src/operators/multicast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ extension MotionObservableConvertible {
for observer in observers {
observer.next(value)
}
}, state: { state in
lastState = state
for observer in observers {
observer.state(state)
}
}, coreAnimation: { event in
lastCoreAnimationEvent = event
for observer in observers {
Expand All @@ -59,18 +54,12 @@ extension MotionObservableConvertible {
// Add the observer to the list after subscribing so that we don't double-send.
observers.append(observer)

if let lastState = lastState, lastState == .active {
observer.state(.active)
}
if let lastValue = lastValue {
observer.next(lastValue)
}
if let lastCoreAnimationEvent = lastCoreAnimationEvent {
observer.coreAnimation(lastCoreAnimationEvent)
}
if let lastState = lastState, lastState == .atRest {
observer.state(.atRest)
}

return {
if let index = observers.index(where: { $0 === observer }) {
Expand Down
2 changes: 1 addition & 1 deletion src/operators/read.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension MotionObservableConvertible {
*/
public func read() -> T? {
var value: T?
asStream().subscribe(next: { value = $0 }, state: { _ in }, coreAnimation: { _ in })
asStream().subscribe(next: { value = $0 }, coreAnimation: { _ in })
return value
}
}
4 changes: 2 additions & 2 deletions src/operators/slop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ extension MotionObservableConvertible where T == CGFloat {
.threshold(min: -size, max: size,
whenBelow: true, whenWithin: nil as Bool?, whenAbove: true)
.dedupe()
.subscribe(next: { didLeaveSlopRegion.value = $0 }, state: { _ in }, coreAnimation: { _ in })
.subscribe(next: { didLeaveSlopRegion.value = $0 }, coreAnimation: { _ in })

let downstreamSubscription = self
.valve(openWhenTrue: didLeaveSlopRegion)
.threshold(min: -size, max: size,
whenBelow: onExit, whenWithin: onReturn, whenAbove: onExit)
.dedupe()
.subscribe(next: observer.next, state: observer.state, coreAnimation: observer.coreAnimation)
.subscribe(next: observer.next, coreAnimation: observer.coreAnimation)

return {
upstreamSubscription.unsubscribe()
Expand Down
89 changes: 0 additions & 89 deletions src/operators/toggled.swift

This file was deleted.

3 changes: 1 addition & 2 deletions src/operators/valve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extension MotionObservableConvertible {

var connectUpstream = {
upstreamSubscription = self.asStream().subscribe(next: observer.next,
state: observer.state,
coreAnimation: observer.coreAnimation)
}

Expand All @@ -47,7 +46,7 @@ extension MotionObservableConvertible {
upstreamSubscription = nil
}

}, state: { _ in }, coreAnimation: { _ in })
}, coreAnimation: { _ in })

return {
valveSubscription?.unsubscribe()
Expand Down
6 changes: 0 additions & 6 deletions src/sources/GestureToStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ private final class GestureConnection<T: UIGestureRecognizer> {

private func propagate(_ gesture: UIGestureRecognizer) {
let isActive = gesture.state == .began || gesture.state == .changed
if isActive {
observer.state(.active)
}
observer.next(gesture as! T)
if !isActive {
observer.state(.atRest)
}
}

private var gesture: (T)?
Expand Down
Loading

0 comments on commit c2657b8

Please sign in to comment.