Swiftpack.co -  vknabel/SwiftHook as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
vknabel/SwiftHook
A simple and key-based callback library written in Swift.
.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: 2 years ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Swift 3.0
4 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