Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/swiftpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Swift Package Manager

on:
push:
branches: [master]
release:
types: [published]
pull_request:
branches: [master]

permissions:
contents: read

jobs:
test:
name: macOS Debug and Release
runs-on: macos-15
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Show Swift version
run: swift --version
- name: Validate package manifest
run: swift package dump-package
- name: Test Debug configuration
run: swift test --jobs 2
- name: Test Release configuration
run: swift test --configuration release --jobs 2
- name: Verify public API from an external package
if: github.event_name != 'release'
env:
PACKAGE_PATH: ${{ github.workspace }}
run: bash Scripts/verify-swiftpm-consumer.sh 1.1.0
- name: Verify released version as an external package
if: github.event_name == 'release'
env:
RELEASE_VERSION: ${{ github.event.release.tag_name }}
EXPECTED_REVISION: ${{ github.sha }}
run: bash Scripts/verify-swiftpm-consumer.sh "$RELEASE_VERSION"
27 changes: 27 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "SwiftNotificationCenter",
platforms: [
.iOS("8.0"),
.macOS("10.9")
],
products: [
.library(name: "SwiftNotificationCenter", targets: ["SwiftNotificationCenter"])
],
targets: [
.target(
name: "SwiftNotificationCenter",
path: "SwiftNotificationCenter",
exclude: ["Info.plist", "SwiftNotificationCenter.h"]
),
.testTarget(
name: "SwiftNotificationCenterTests",
dependencies: ["SwiftNotificationCenter"],
path: "SwiftNotificationCenterTests",
exclude: ["Info.plist"]
)
],
swiftLanguageVersions: [.v5]
)
65 changes: 62 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,83 @@ func UIKeyboardWillShow(beginFrame: CGRect, endFrame: CGRect) {
}
~~~

## Delivery and threading

`Broadcaster.notify` runs its callbacks synchronously on the calling thread or
queue, using a snapshot of the currently live observers. Registration and
removal are queued behind internal barriers; a subsequent notification observes
those earlier changes. Callbacks may register, unregister, or issue a nested
notification without changing the current notification's snapshot.

Use bounded concurrency when many callers notify at once. Submitting an
unbounded number of blocking callers to a shared Dispatch worker pool can
exhaust its workers while the internal barrier work is waiting to run. Thread
safety does not imply a progress guarantee at arbitrary parallelism.

## Installation

CocoaPods:
### Swift Package Manager

Add `https://github.com/100mango/SwiftNotificationCenter.git` as a package
dependency in Xcode, choose version **1.1.0 or later**, and add the
`SwiftNotificationCenter` product to your target. Version 1.1.0 is the first
release with Swift Package Manager support; 1.0.5 and earlier do not include
`Package.swift`.

In another package's `Package.swift`, use:

~~~swift
dependencies: [
.package(
url: "https://github.com/100mango/SwiftNotificationCenter.git",
from: "1.1.0"
)
],
targets: [
.target(name: "YourTarget", dependencies: ["SwiftNotificationCenter"])
]
~~~

Then `import SwiftNotificationCenter` in your Swift files. For local development,
add the checkout as a local package in Xcode or use
`.package(path: "../SwiftNotificationCenter")`.

The manifest uses Swift tools 5.0 and Swift 5 language mode, and retains the
library's declared iOS 8.0 and macOS 10.9 deployment targets. The installed
Xcode/Swift toolchain may require a newer deployment target. CocoaPods and
Carthage installation remain unchanged.

### CocoaPods

~~~
pod 'SwiftNotificationCenter'
~~~

Carthage:
### Carthage

~~~
github "100mango/SwiftNotificationCenter"
~~~

Manually:
### Manually

Just copy source files in the SwiftNotificationCenter folder into your project.

## Testing

Run the package tests from the repository root with a current Swift toolchain:

~~~sh
swift build
swift test
swift test --configuration release
~~~

The package builds the library sources directly and reuses the Xcode test
suite. It does not include the UIKit example application, umbrella header, or
Xcode Info.plist files. The Foundation-only library can also be tested on Linux;
Apple-platform builds still require an appropriate Xcode SDK.


## License

Expand Down
124 changes: 124 additions & 0 deletions Scripts/verify-swiftpm-consumer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env bash
set -euo pipefail

version="${1:?Usage: verify-swiftpm-consumer.sh VERSION}"
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Expected a semantic version such as 1.1.0" >&2
exit 1
fi

consumer="$(mktemp -d)"
trap 'rm -rf "$consumer"' EXIT
mkdir -p "$consumer/Sources/Consumer" "$consumer/Tests/ConsumerTests"

# PACKAGE_PATH is only for checking this consumer against a local checkout.
# Without it, resolve the exact public release rather than a moving branch.
python3 - "$consumer/Package.swift" "$version" "${PACKAGE_PATH:-}" <<'PY'
import json
import pathlib
import sys

manifest, version, local_path = sys.argv[1:]
dependency = (
'.package(path: ' + json.dumps(local_path, ensure_ascii=False) + ')'
if local_path else
'.package(url: "https://github.com/100mango/SwiftNotificationCenter.git", .exact("' + version + '"))'
)
pathlib.Path(manifest).write_text('''// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "Consumer",
dependencies: [''' + dependency + '''],
targets: [
.target(name: "Consumer", dependencies: ["SwiftNotificationCenter"]),
.testTarget(name: "ConsumerTests", dependencies: ["Consumer", "SwiftNotificationCenter"])
],
swiftLanguageVersions: [.v5]
)
''')
PY

cat > "$consumer/Sources/Consumer/Consumer.swift" <<'SWIFT'
import SwiftNotificationCenter

public protocol MessageObserver: AnyObject {
func receive(_ message: String)
}

public func broadcast(_ message: String) {
Broadcaster.notify(MessageObserver.self) { $0.receive(message) }
}
SWIFT

cat > "$consumer/Tests/ConsumerTests/ConsumerTests.swift" <<'SWIFT'
import Consumer
import SwiftNotificationCenter
import XCTest

final class Observer: MessageObserver {
var messages = [String]()
func receive(_ message: String) { messages.append(message) }
}

final class WeakReference {
weak var value: Observer?
init(_ value: Observer) { self.value = value }
}

final class ConsumerTests: XCTestCase {
override func tearDown() {
Broadcaster.unregister(MessageObserver.self)
Broadcaster.notify(MessageObserver.self) { _ in }
super.tearDown()
}

func testPublicProductDeliversTypedMessages() {
let observer = Observer()
Broadcaster.register(MessageObserver.self, observer: observer)
broadcast("from the external consumer")
XCTAssertEqual(observer.messages, ["from the external consumer"])
}

func testUnregisterStopsDelivery() {
let observer = Observer()
Broadcaster.register(MessageObserver.self, observer: observer)
Broadcaster.unregister(MessageObserver.self, observer: observer)
broadcast("must not arrive")
XCTAssertTrue(observer.messages.isEmpty)
}

func testPublicRegistrationDoesNotRetainObservers() {
var observer: Observer? = Observer()
let reference = WeakReference(observer!)
Broadcaster.register(MessageObserver.self, observer: observer!)
broadcast("drain registration")
observer = nil
XCTAssertNil(reference.value)
Broadcaster.notify(MessageObserver.self) { _ in
XCTFail("A released observer must not be delivered to")
}
}
}
SWIFT

swift package --package-path "$consumer" resolve
if [[ -z "${PACKAGE_PATH:-}" ]]; then
python3 - "$consumer/Package.resolved" "$version" "${EXPECTED_REVISION:-}" <<'PY'
import json
import pathlib
import sys

path, expected_version, expected_revision = sys.argv[1:]
resolved = json.loads(pathlib.Path(path).read_text())
pins = resolved.get('pins', resolved.get('object', {}).get('pins', []))
assert len(pins) == 1, pins
state = pins[0]['state']
assert state['version'] == expected_version, state
if expected_revision:
assert state['revision'] == expected_revision, state
print('Resolved SwiftNotificationCenter ' + state['version'] + ' at ' + state['revision'])
PY
fi
swift build --package-path "$consumer" --jobs 2
swift test --package-path "$consumer" --jobs 2
swift test --package-path "$consumer" --configuration release --jobs 2
2 changes: 1 addition & 1 deletion SwiftNotificationCenter.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SwiftNotificationCenter"
s.version = "1.0.5"
s.version = "1.1.0"
s.summary = "A Type Safe, Thread Safe, ARC Safe Protocol Oriented NotificationCenter"
s.homepage = "https://github.com/100mango/SwiftNotificationCenter"
s.license = { :type => 'MIT', :file => 'LICENSE.txt' }
Expand Down
Loading
Loading