Skip to content

Instantly share code, notes, and snippets.

@karlwilcox
Last active August 29, 2015 14:20
Show Gist options
  • Save karlwilcox/0dc52a94ec832f9783a4 to your computer and use it in GitHub Desktop.
Save karlwilcox/0dc52a94ec832f9783a4 to your computer and use it in GitHub Desktop.
A very simple notification framework. Objects may register a call back for when pre-defined event types occur, and other objects may indicate that those events have occurred.
//
// Notifier.swift
//
// Created by Karl Wilcox on 07/05/2015.
// Copyright (c) 2015 Karl Wilcox.
//
/* Description: A very simple notification framework.
Objects may register a call back for when pre-defined event types occur, and other objects may indicate that
those events have occurred. A typical use case would be a switching view controller type application
(tabbed views) in which user actions in some of the views influence the appearance of other views.
Event receiving views should register for events in their init functions, event sending
views can called "eventOccured" either in immediate response to user input or at prepareForSegue.
Sending notifications in viewWillDisappear is NOT recommend as this can occur *after* the viewWillAppear
function of the newly selected view.
*/
import Foundation
/// Protocol for notification receivers - can be used to confirm that the type signature is correct
protocol NotificationReceiver {
func eventHasOccured(event: Notifier.EventTypes, message:AnyObject?)
}
/// A simple notification framework.
class Notifier {
/// function signature for receiving notifications
typealias receiverFunc = (event:EventTypes, message:AnyObject?)->()
/**
Define your event types here.
- Event1: Something that occurs.
- Event2: Some other thing that occurs.
- Event3: As many as required...
*/
enum EventTypes {
case .Event1, .Event2, .Event3 // Could give these raw values if required...
}
/// Dictionary of event types matching lists of registered receiving functions
/// Note that repeatedly registering the same function for the same event will caused repeated invocations
/// of that function; this being an implementation of the YAFIYGI principle (You-Asked-For-It-You-Got-It).
/// We can't simply use a set here as functions are not Hashable types
var receivers:[EventTypes:[receiverFunc]] = [:]
/**
Indicates that an event has occurred, triggers calls to receiving functions.
Invoked by whatever object detects or creates the event in question
:param: event One of the event types defined above.
:param: message optional object, e.g. a String giving more details
:returns: None.
*/
func eventOccured(event:EventTypes, message:AnyObject?) {
if let targets = receivers[event] {
for target in targets {
target(event: event, message: message)
}
}
}
/**
Adds the given function to the list of those called wen the given event occurs.
Called by whatever object has an interest in knowing about the event
:param: event one of the event types defined above
:param: target a function that takes as arguments the event type and an optional message object
:returns: None.
*/
func registerForEvent(event:EventTypes, target:receiverFunc) {
if let _ = receivers[event] {
receivers[event]?.append(target)
} else {
receivers[event] = [target]
}
}
/**
A convenience function that provides an easy way for a single target function to register
to receive mutliple events (the first argument to the target function can be used to
distinguish between multiple events)
:param: events an array of the event types defined above
:param: target a function that takes as arguments the event type and an optional message object
:returns: None.
*/
func registerForMultipleEvents(events:[EventTypes], target:receiverFunc) {
for event in events {
registerForEvent(event, target: target)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment