ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - artman/Signals: A micro-library for creating and observing events.
A micro-library for creating and observing events. - artman/Signals
Visit Site

GitHub - artman/Signals: A micro-library for creating and observing events.

GitHub - artman/Signals: A micro-library for creating and observing events.

Signals

Build Status Cocoapods Compatible Carthage Compatible License Platform Twitter

Signals is a library for creating and observing events. It replaces delegates, actions and NSNotificationCenter with something much more powerful and elegant.

Features

  • Attach-and-forget observation
  • Type-safety
  • Filtered observation
  • Delayed and queued observation
  • Comprehensive Unit Test Coverage

Requirements

  • iOS 7.0 / watchOS 2.0 / Mac OS X 10.9
  • Swift 4.2

Installation

To use Signals with a project targeting iOS 7, simply copy Signals.swift into your project.

CocoaPods

To integrate Signals into your project add the following to your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'Signals', '~> 6.0'

Carthage

To integrate Signals into your project using Carthage add the following to your Cartfile:

github "artman/Signals" ~> 6.0

Swift Package Manager

To integrate Signals into your project using SwiftPM add the following to your Package.swift:

dependencies: [
    .package(url: "https://github.com/artman/Signals", from: "6.0.0"),
],

Quick start

Make events on a class observable by creating one or more signals:

class NetworkLoader {

    // Creates a number of signals that can be subscribed to
    let onData = Signal<(data:NSData, error:NSError)>()
    let onProgress = Signal<Float>()

    ...

    func receivedData(receivedData:NSData, receivedError:NSError) {
        // Whenever appropriate, fire off any of the signals
        self.onProgress.fire(1.0)
        self.onData.fire((data:receivedData, error:receivedError))
    }
}

Subscribe to these signals from elsewhere in your application

let networkLoader = NetworkLoader("http://artman.fi")

networkLoader.onProgress.subscribe(with: self) { (progress) in
    print("Loading progress: \(progress*100)%")
}

networkLoader.onData.subscribe(with: self) { (data, error) in
    // Do something with the data
}

Adding subscriptions to Signals is an attach-and-forget operation. If the subscribing object is deallocated, the Signal cancels the subscription, so you don't need to explicitly manage the cancellation of your subsciptions.

Singals aren't restricted to one subscriber. Multiple objects can subscribe to the same Signal.

You can also subscribe to events after they have occurred:

networkLoader.onProgress.subscribePast(with: self) { (progress) in
    // This will immediately fire with last progress that was reported
    // by the onProgress signal
    println("Loading progress: \(progress*100)%")
}

Advanced topics

Signal subscriptions can apply filters:

networkLoader.onProgress.subscribe(with: self) { (progress) in
    // This fires when progress is done
}.filter { $0 == 1.0 }

You can sample up subscriptions to throttle how often you're subscription is executed, regardless how often the Signal fires:

networkLoader.onProgress.subscribe(with: self) { (progress) in
    // Executed once per second while progress changes
}.sample(every: 1.0)

By default, a subscription executes synchronously on the thread that fires the Signal. To change the default behaviour, you can use the dispatchOnQueue method to define the dispatch queue:

networkLoader.onProgress.subscribe(with: self) { (progress) in
    // This fires on the main queue
}.dispatchOnQueue(DispatchQueue.main)

If you don't like the double quotes when you fire signals that take tuples, you can use the custom => operator to fire the data:

// If you don't like the double quotes when firing signals that have tuples
self.onData.fire((data:receivedData, error:receivedError))

// You can use the => operator to fire the signal
self.onData => (data:receivedData, error:receivedError)

// Also works for signals without tuples
self.onProgress => 1.0

Replacing actions

Signals extends all classes that extend from UIControl (not available on OS X) and lets you use Signals to listen to control events for increased code locality.

let button = UIButton()
button.onTouchUpInside.observe(with: self) {
    // Handle the touch
}

let slider = UISlider()
slider.onValueChanged.observe(with: self) {
    // Handle value change
}

Replacing delegates

Signals is simple and modern and greatly reduce the amount of boilerplate that is required to set up delegation.

Would you rather implement a callback using a delegate:

  • Create a protocol that defines what is delegated
  • Create a delegate property on the class that wants to provide delegation
  • Mark each class that wants to become a delegate as comforming to the delegate protocol
  • Implement the delegate methods on the class that want to become a delegate
  • Set the delegate property to become a delegate of the instance
  • Check that your delegate implements each delegate method before invoking it

Or do the same thing with Signals:

  • Create a Signal for the class that wants to provide an event
  • Subscribe to the Signal

Replace NotificationCenter

When your team of engineers grows, NotificationCenter quickly becomes an anti-pattern. Global notifications with implicit data and no compiler safety easily make your code error-prone and hard to maintain and refactor.

Replacing NotificationCenter with Signals will give you strong type safety enforced by the compiler that will help you maintain your code no matter how fast you move.

Communication

  • If you found a bug, open an issue or submit a fix via a pull request.
  • If you have a feature request, open an issue or submit a implementation via a pull request or hit me up on Twitter @artman
  • If you want to contribute, submit a pull request onto the master branch.

License

Signals is released under an MIT license. See the LICENSE file for more information

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory