Skip to content

Commit

Permalink
Support serial async events via DispatchQueue.main
Browse files Browse the repository at this point in the history
  • Loading branch information
akiroz committed May 9, 2018
1 parent 98ab171 commit b8ba75f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Excitation/Excitation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Emitter<T> {
}

public func observe(_ f: @escaping ()->Void) -> Observer<T> {
return self.observe({ (_: T) in f() })
return self.observe { (_: T) in f() }
}
public func observe(_ f: @escaping (T)->Void) -> Observer<T> {
let ob = Observer<T>(f)
Expand All @@ -55,6 +55,15 @@ public class Emitter<T> {
obRef[ObjectIdentifier(ob)] = ob
return ob
}

public func observeAsync(_ f: @escaping ()->Void) -> Observer<T> {
return self.observeAsync { (_: T) in f() }
}
public func observeAsync(_ f: @escaping (T)->Void) -> Observer<T> {
return self.observe { data in
DispatchQueue.main.async { f(data) }
}
}

public func remove(_ ob: Observer<T>) {
nc.removeObserver(ob, name: nn, object: nil)
Expand Down
19 changes: 19 additions & 0 deletions ExcitationTests/ExcitationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ class ExcitationTests: XCTestCase {
e.remove(ob)
e.emit()
}

func testAsyncNoData() {
let e = Emitter<None>()
let ex = XCTestExpectation(description: "async event")
let _ = e.observeAsync { ex.fulfill() }
e.emit()
wait(for: [ex], timeout: 1)
}

func testAsyncWithData() {
let e = Emitter<Int>()
let ex = XCTestExpectation(description: "async event")
let _ = e.observeAsync { n in
XCTAssert( n == 1 )
ex.fulfill()
}
e.emit(1)
wait(for: [ex], timeout: 1)
}
}
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A thin wrapper around NotificationCenter for supporting event-driven code.
A thin wrapper around `NotificationCenter` and `DispatchQueue` for supporting event-driven code.

Event handlers are dispatched synchronously as is NotificationCenter.
Excitation supports both sync and async events via `DispatchQueue.main`.

## Install

Expand Down Expand Up @@ -37,12 +37,13 @@ e.observe {
// Emit event
e.emit()

// Example: Event with Data
// Example: Event with Data & Async
// =====================================================

let e = Emitter<Int>()
e.observe { n in print(n) } // prints 1
e.observe { print("hi") } // prints hi
e.observe { print("hi") }
e.observeAsync { print("async handler") }
e.emit(1)

// Example: Remove Handlers
Expand Down

0 comments on commit b8ba75f

Please sign in to comment.