-
Notifications
You must be signed in to change notification settings - Fork 0
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
726c06d
commit f06d8a2
Showing
20 changed files
with
344 additions
and
121 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
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,19 @@ | ||
// | ||
// GenericFuture.swift | ||
// VariableDemo | ||
// | ||
// Created by Cao Phuoc Thanh on 5/15/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
extension Genenic { | ||
|
||
public func asObservable() -> Future<Element> { | ||
let promise = Promise<Element>() | ||
Signal<Element> { value in | ||
promise.resolve(value) | ||
}.subscribe(genenic: self) | ||
return promise | ||
} | ||
|
||
} |
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,25 @@ | ||
// | ||
// Distinct.swift | ||
// VariableDemo | ||
// | ||
// Created by Cao Phuoc Thanh on 5/16/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
extension Future where Value: Equatable { | ||
|
||
public func distinct() -> Future<Value> { | ||
let promise = Promise<Value>() | ||
observe { result in | ||
switch result { | ||
case .success(let value): | ||
if value != self._value { | ||
self._value = value | ||
promise.resolve(value) | ||
} | ||
case .failure(let error): promise.reject(error) | ||
} | ||
} | ||
return promise | ||
} | ||
} |
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,24 @@ | ||
// | ||
// Filter.swift | ||
// VariableDemo | ||
// | ||
// Created by Cao Phuoc Thanh on 5/16/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
extension Future where Value: Equatable { | ||
|
||
public func filer(_ condition: @escaping (Value) -> Bool) -> Future<Value> { | ||
let promise = Promise<Value>() | ||
observe { result in | ||
switch result { | ||
case .success(let value): | ||
if condition(value) { | ||
promise.resolve(value) | ||
} | ||
case .failure(let error): promise.reject(error) | ||
} | ||
} | ||
return promise | ||
} | ||
} |
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,46 @@ | ||
// | ||
// Throttle.swift | ||
// VariableDemo | ||
// | ||
// Created by Cao Phuoc Thanh on 5/16/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Future { | ||
|
||
typealias Throttle<Element> = (_ : Element) -> Void | ||
|
||
private func throttle<Element>(interval: Int, queue: DispatchQueue, action: @escaping Throttle<Element>) -> Throttle<Element> { | ||
var lastFireTime = DispatchTime.now() | ||
let dispatchDelay = DispatchTimeInterval.milliseconds(interval) | ||
return { param in | ||
let dispatchTime: DispatchTime = DispatchTime.now() + dispatchDelay | ||
|
||
queue.asyncAfter(deadline: dispatchTime) { | ||
let when: DispatchTime = lastFireTime + dispatchDelay | ||
let now = DispatchTime.now() | ||
if now.rawValue >= when.rawValue { | ||
lastFireTime = now | ||
action(param) | ||
} | ||
} | ||
} | ||
} | ||
|
||
public func throttle(interval: Int) -> Future<Value> { | ||
let promise = Promise<Value>() | ||
let throttle: Throttle<Value> = self.throttle(interval: interval, queue: DispatchQueue.main) { (value: Value) in | ||
promise.resolve(value) | ||
} | ||
observe { result in | ||
switch result { | ||
case .success(let value): throttle(value) | ||
case .failure(let error): promise.reject(error) | ||
} | ||
} | ||
return promise | ||
} | ||
|
||
} |
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,30 @@ | ||
// | ||
// Button.swift | ||
// VariableDemo | ||
// | ||
// Created by Cao Phuoc Thanh on 5/15/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public class Button: UIButton { | ||
|
||
private var promise: Promise<Void> = Promise<Void>() | ||
|
||
@objc dynamic private func selector() { | ||
self.promise.result = .success(()) | ||
} | ||
|
||
public func action(_ controlEvents: UIControl.Event = .touchUpInside) -> Future<Void> { | ||
self.removeTarget(nil, action: nil, for: .allEvents) | ||
self.addTarget(self, action: #selector(selector), for: controlEvents) | ||
return promise | ||
} | ||
|
||
deinit { | ||
self.removeTarget(nil, action: nil, for: .allEvents) | ||
print(self,"deinit") | ||
} | ||
|
||
} |
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,30 @@ | ||
// | ||
// UISwitch+Action.swift | ||
// Flux | ||
// | ||
// Created by Cao Phuoc Thanh on 5/13/20. | ||
// Copyright © 2020 Cao Phuoc Thanh. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public class Switch: UISwitch { | ||
|
||
private var promise: Promise<Void> = Promise<Void>() | ||
|
||
@objc dynamic private func selector() { | ||
self.promise.result = .success(()) | ||
} | ||
|
||
public func action(_ controlEvents: UIControl.Event = .valueChanged) -> Future<Void> { | ||
self.removeTarget(nil, action: nil, for: .allEvents) | ||
self.addTarget(self, action: #selector(selector), for: controlEvents) | ||
return promise | ||
} | ||
|
||
deinit { | ||
self.removeTarget(nil, action: nil, for: .allEvents) | ||
print(self,"deinit") | ||
} | ||
|
||
} |
Oops, something went wrong.