Latest 3.0.0
Homepage https://github.com/vknabel/SwiftHook
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0
Authors

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.

Latest podspec

{
    "name": "SwiftHook",
    "version": "3.0.0",
    "summary": "A simple and key-based callback library written in Swift.",
    "description": "SwiftHook is a simple and key-based callback library written in Swift.",
    "social_media_url": "https://twitter.com/vknabel",
    "homepage": "https://github.com/vknabel/SwiftHook",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Valentin Knabel": "[email protected]"
    },
    "source": {
        "git": "https://github.com/vknabel/SwiftHook.git",
        "tag": "3.0.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source_files": "Sources/SwiftHook/*.swift"
}

Pin It on Pinterest

Share This