Skip to content

Commit

Permalink
Merge pull request #1 from jasonnam/develop
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
jasonnam authored Apr 1, 2020
2 parents 3e71a86 + 8c41102 commit c39768b
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 82 deletions.
4 changes: 2 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ load(
)

swift_library(
name = "Megaphone",
name = "DarwinNotificationCenter",
srcs = glob([
"Sources/Megaphone/**/*.swift",
"Sources/DarwinNotificationCenter/**/*.swift",
]),
visibility = [
"//visibility:public",
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2020-04-01
### Changed
- Rename class to `DarwinNotificationCenter`.

### Removed
- Notification to `NotificationCenter`.

## [0.1.0] - 2019-09-25
### Added
- `Megaphone.swift`

[0.1.0]: /~https://github.com/jasonnam/Megaphone/releases/tag/0.1.0
[0.2.0]: /~https://github.com/jasonnam/DarwinNotificationCenter/compare/0.1.0...0.2.0
[0.1.0]: /~https://github.com/jasonnam/DarwinNotificationCenter/releases/tag/0.1.0
10 changes: 5 additions & 5 deletions Megaphone.podspec → DarwinNotificationCenter.podspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Pod::Spec.new do |spec|
spec.name = 'Megaphone'
spec.version = '0.1.0'
spec.name = 'DarwinNotificationCenter'
spec.version = '0.2.0'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.homepage = '/~https://github.com/jasonnam/Megaphone'
spec.homepage = '/~https://github.com/jasonnam/DarwinNotificationCenter'
spec.authors = { 'Jason Nam' => 'contact@jasonnam.com' }
spec.summary = 'Inter-process notification with Darwin notification center.'
spec.source = { :git => '/~https://github.com/jasonnam/Megaphone.git', :tag => spec.version.to_s }
spec.source = { :git => '/~https://github.com/jasonnam/DarwinNotificationCenter.git', :tag => spec.version.to_s }
spec.swift_version = '5.0'
spec.ios.deployment_target = '8.0'
spec.osx.deployment_target = '10.10'
spec.tvos.deployment_target = '9.0'
spec.watchos.deployment_target = '2.0'
spec.source_files = 'Sources/Megaphone/*.swift'
spec.source_files = 'Sources/DarwinNotificationCenter/*.swift'
end
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Jason Nam
Copyright (c) 2019 - 2020 Jason Nam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import PackageDescription

let package = Package(
name: "Megaphone",
name: "DarwinNotificationCenter",
products: [
.library(
name: "Megaphone",
targets: ["Megaphone"])
name: "DarwinNotificationCenter",
targets: ["DarwinNotificationCenter"])
],
targets: [
.target(
name: "Megaphone"),
name: "DarwinNotificationCenter"),
.testTarget(
name: "MegaphoneTests",
dependencies: ["Megaphone"])
name: "DarwinNotificationCenterTests",
dependencies: ["DarwinNotificationCenter"])
]
)
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Megaphone
# DarwinNotificationCenter

[![GitHub release](https://img.shields.io/github/release/jasonnam/Megaphone.svg)](/~https://github.com/jasonnam/Megaphone/releases)
[![CocoaPods](https://img.shields.io/cocoapods/v/Megaphone.svg)](https://cocoapods.org/pods/Megaphone)
[![travis-ci build status](https://travis-ci.com/jasonnam/Megaphone.svg?branch=master)](https://travis-ci.com/jasonnam/Megaphone)
[![GitHub release](https://img.shields.io/github/release/jasonnam/DarwinNotificationCenter.svg)](/~https://github.com/jasonnam/DarwinNotificationCenter/releases)
[![CocoaPods](https://img.shields.io/cocoapods/v/DarwinNotificationCenter.svg)](https://cocoapods.org/pods/DarwinNotificationCenter)
[![travis-ci build status](https://travis-ci.com/jasonnam/DarwinNotificationCenter.svg?branch=master)](https://travis-ci.com/jasonnam/DarwinNotificationCenter)

Inter-process notification with Darwin notification center.

Expand All @@ -12,22 +12,22 @@ Inter-process notification with Darwin notification center.

#### Swift Package Manager

Add Megaphone to the dependencies value of your `Package.swift`.
Add DarwinNotificationCenter to the dependencies value of your `Package.swift`.

```swift
dependencies: [
.package(url: "/~https://github.com/jasonnam/Megaphone", from: "0.1.0")
.package(url: "/~https://github.com/jasonnam/DarwinNotificationCenter", from: "0.2.0")
]
```

#### CocoaPods

Add `Megaphone` to your `Podfile`.
Add `DarwinNotificationCenter` to your `Podfile`.

```ruby
pod 'Megaphone'
pod 'DarwinNotificationCenter'
```

## License

Megaphone is released under the MIT license. [See LICENSE](/~https://github.com/jasonnam/Megaphone/blob/master/LICENSE) for details.
DarwinNotificationCenter is released under the MIT license. [See LICENSE](/~https://github.com/jasonnam/DarwinNotificationCenter/blob/master/LICENSE) for details.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// Megaphone.swift
// Megaphone
// DarwinNotificationCenter.swift
// DarwinNotificationCenter
//
// Copyright (c) 2019 Jason Nam (https://jasonnam.com)
// Copyright (c) 2019 - 2020 Jason Nam (https://jasonnam.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,46 +25,52 @@

import Foundation

open class Megaphone {
public class DarwinNotificationCenter {

public static let `default` = Megaphone()
public static let `default` = DarwinNotificationCenter()

public private(set) var observations: [String: (String) -> Void] = [:]

public func addObserver(forName name: String, using block: @escaping (String) -> Void) {
observations[name] = block

open func addObserver(forName name: Notification.Name) {
let callback: CFNotificationCallback = { _, _, name, _, _ in
guard let name = name else { return }
NotificationCenter.default.post(name: .init(name.rawValue as String), object: nil)
guard let name = name?.rawValue as String? else { return }
DarwinNotificationCenter.default.observations[name]?(name)
}

CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
Unmanaged.passUnretained(self).toOpaque(),
callback,
name.rawValue as CFString,
name as CFString,
nil,
.deliverImmediately
)
}

open func removeObserver(withName name: Notification.Name) {
public func removeObserver(withName name: String) {
observations.removeValue(forKey: name)

CFNotificationCenterRemoveObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
Unmanaged.passUnretained(self).toOpaque(),
CFNotificationName(name.rawValue as CFString),
CFNotificationName(name as CFString),
nil
)
}

open func removeAllObservers() {
public func removeAllObservers() {
CFNotificationCenterRemoveEveryObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
Unmanaged.passUnretained(self).toOpaque()
)
}

open func post(name: Notification.Name) {
public func post(name: String) {
CFNotificationCenterPostNotification(
CFNotificationCenterGetDarwinNotifyCenter(),
CFNotificationName(name.rawValue as CFString),
CFNotificationName(name as CFString),
nil,
nil,
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// MegaphoneTests.swift
// MegaphoneTests
// DarwinNotificationCenterTests.swift
// DarwinNotificationCenterTests
//
// Copyright (c) 2019 Jason Nam (https://jasonnam.com)
// Copyright (c) 2019 - 2020 Jason Nam (https://jasonnam.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,84 +23,69 @@
// THE SOFTWARE.
//

@testable import DarwinNotificationCenter
import XCTest
@testable import Megaphone

final class MegaphoneTests: XCTestCase {
final class DarwinNotificationCenterTests: XCTestCase {

private var megaphone: Megaphone!

override func setUp() {
super.setUp()
megaphone = .init()
override func tearDown() {
super.tearDown()
DarwinNotificationCenter.default.removeAllObservers()
}

func testAddObserver() {
let notificationName = Notification.Name(rawValue: "testAddObserver")

megaphone.addObserver(forName: notificationName)
let notificationName = "testAddObserver"

let expectation = self.expectation(description: "Observe test notification")

let observer = NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: .main) { _ in
DarwinNotificationCenter.default.addObserver(forName: notificationName) { name in
XCTAssertEqual(name, notificationName)
expectation.fulfill()
}

megaphone.post(name: notificationName)
DarwinNotificationCenter.default.post(name: notificationName)

waitForExpectations(timeout: 1, handler: nil)

NotificationCenter.default.removeObserver(observer)
}

func testRemoveObserver() {
let notificationName = Notification.Name(rawValue: "testRemoveObserver")

megaphone.addObserver(forName: notificationName)
let notificationName = "testRemoveObserver"

let expectation = self.expectation(description: "Should not observe test notification")
expectation.isInverted = true

let observer = NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: .main) { _ in
DarwinNotificationCenter.default.addObserver(forName: notificationName) { _ in
expectation.fulfill()
}

megaphone.removeObserver(withName: notificationName)
DarwinNotificationCenter.default.removeObserver(withName: notificationName)

megaphone.post(name: notificationName)
DarwinNotificationCenter.default.post(name: notificationName)

waitForExpectations(timeout: 1, handler: nil)

NotificationCenter.default.removeObserver(observer)
}

func testRemoveAllObservers() {
let notificationName1 = Notification.Name(rawValue: "testRemoveAllObservers1")
let notificationName2 = Notification.Name(rawValue: "testRemoveAllObservers2")

megaphone.addObserver(forName: notificationName1)
megaphone.addObserver(forName: notificationName2)
let notificationName1 = "testRemoveAllObservers1"
let notificationName2 = "testRemoveAllObservers2"

let expectation1 = expectation(description: "Should not observe test notification 1")
expectation1.isInverted = true
let expectation2 = expectation(description: "Should not observe test notification 2")
expectation2.isInverted = true

let observer1 = NotificationCenter.default.addObserver(forName: notificationName1, object: nil, queue: .main) { _ in
DarwinNotificationCenter.default.addObserver(forName: notificationName1) { _ in
expectation1.fulfill()
}
let observer2 = NotificationCenter.default.addObserver(forName: notificationName2, object: nil, queue: .main) { _ in
DarwinNotificationCenter.default.addObserver(forName: notificationName2) { _ in
expectation2.fulfill()
}

megaphone.removeAllObservers()
DarwinNotificationCenter.default.removeAllObservers()

megaphone.post(name: notificationName1)
megaphone.post(name: notificationName2)
DarwinNotificationCenter.default.post(name: notificationName1)
DarwinNotificationCenter.default.post(name: notificationName2)

waitForExpectations(timeout: 1, handler: nil)

NotificationCenter.default.removeObserver(observer1)
NotificationCenter.default.removeObserver(observer2)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#if !canImport(ObjectiveC)
import XCTest

extension MegaphoneTests {
extension DarwinNotificationCenterTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__MegaphoneTests = [
static let __allTests__DarwinNotificationCenterTests = [
("testAddObserver", testAddObserver),
("testRemoveAllObservers", testRemoveAllObservers),
("testRemoveObserver", testRemoveObserver),
Expand All @@ -14,7 +14,7 @@ extension MegaphoneTests {

public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(MegaphoneTests.__allTests__MegaphoneTests),
testCase(DarwinNotificationCenterTests.__allTests__DarwinNotificationCenterTests),
]
}
#endif
4 changes: 2 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import XCTest

import MegaphoneTests
import DarwinNotificationCenterTests

var tests = [XCTestCaseEntry]()
tests += MegaphoneTests.__allTests()
tests += DarwinNotificationCenterTests.__allTests()

XCTMain(tests)
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
workspace(name = "Megaphone")
workspace(name = "DarwinNotificationCenter")

0 comments on commit c39768b

Please sign in to comment.