Swiftpack.co - vknabel/SwiftHook as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vknabel.
vknabel/SwiftHook v1.0.3
A simple and key-based callback library written in Swift.
⭐️ 5
🕓 4 years ago
.package(url: "https://github.com/vknabel/SwiftHook.git", from: "v1.0.3")

SwiftHook

SwiftHook is a simple and key-based callback library written in Swift.

Check out the generated docs at vknabel.github.io/SwiftHook.

Installation

SwiftHook is a pure Swift project and supports Swift Package Manager, Carthage and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .package(url: "https://github.com/vknabel/SwiftHook.git", from: "3.0.0")
    ]
)

Carthage

github "vknabel/SwiftHook" ~> 3.0.0

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'SwiftHook', '~> 3.0.0'

HookEvent

A HookEvent is a wrapper around a raw value that adds some additional type information. This type information gives callbacks compile-time safety.

/// example for DelegationHook
enum RepositoryPolicy: HookAction {
  case nameAllowed
}
/// example for SerialHook
enum RepositoryAction: HookAction {
  case nameChanged
}

extension HookEvent {
  static let nameAllowed: HookEvent<RepositoryPolicy, String, Bool> = HookEvent(action: .nameAllowed)
  static let nameChanged: HookEvent<RepositoryAction, String, ()> = HookEvent(action: .nameChanged)
}

DelegationHook

A DelegationHook only supports one single callback for each hook key and can be used instead of small delegates. Added closures are stored retained.

var hook = DelegationHook<RepositoryPolicy>()
hook.respond(to: .nameAllowed) { string in
  let len = count(string)
  return len > 1 && len < 15
}
let allowed = hook.trigger(event: .nameAllowed, with: "SwiftHook")
  .reduce(into: true, { $0 && $1 })

SerialHook

A SerialHooksupports multiple closures per HookEvent, but respond(to:, with:) returns an optional reference to be stored in a strong reference.

var hook = SerialHook<RepositoryAction>()
var updateObject = hook.respond(to: .nameChanged) { name in /*update title*/ }
var update2Object = hook.respond(to: .nameChanged) { name in /*update title in another place*/ }
hook.trigger(event: .nameChanged, with: "NewName")
updateObject = nil // removes the first name changed closure

License

SwiftHook is released unter MIT License.

GitHub

link
Stars: 5
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Swift 3.0
7 years ago

2.0.0

Released: 2016-09-26

Breaking Changes

  • Upgraded to Swift 3.0.0 - @vknabel
  • Renamed HookType.add(_:,closure:) to HookType.add(key:,with:) - @vknabel
  • Renamed HookType.perform(_:,argument:) to HookType.performAction(forKey:,with:) - @vknabel

API Additions

  • Added Cocoapods support - @vknabel
  • Added generated Jazzy docs - @vknabel
  • Added Travis support - @vknabel

Other Changes

  • Regenerated Xcode Project - @vknabel
  • Renamed remaining ConclurerHook references - @vknabel
  • Introduced Changelog - @vknabel

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics