Classes to ease observer pattern implementation in Swift inspired by C# events
This package is built arround the following principes :
-
Genericity : every classes supports generic
-
Simplicity : inspired by C# event
swift-event
supports subscribe (via+=
andsubscribe()
), unsubcribe (via-=
andunsubscribe()
) and notify viainvoke
. -
Encapsulation :
invoke
is not accessible outside of declaration scope, preventing other classes from breaking internal logic.
Installation - Getting started - Samples - Docs - Q&A - Changelog
You can use The Swift Package Manager to install swift-event
by adding the proper description
dependencies: [
.package(url: "https://github.com/MarcAlx/swift-event.git", from: "1.0.1"),
]
From your .xcodeproj
file select your project then go to Swift Packages
tab then add (via +
) : https://github.com/MarcAlx/swift-event.git
You can find source code here : https://github.com/MarcAlx/swift-event/blob/master/Sources/swift-event/swift_event.swift
All commented it's <100 sloc just copy and paste it into your code.
import swift_event
let tmp = Event<String>.create()
//tmp holds the event in 'event' and a pointer to 'invoke' method
var handler = EventHandler<String>(handle: { sender, args in
print(args)
})
tmp.event += handler
tmp.invoke(self,"Hello world !")
//handler should print "Hello world !"
tmp.event -= handler
let tmp = Event<String>.create()
tmp.event += EventHandler<String>(handle: { sender, args in
print(args)
})
tmp.invoke(self,"Hello world !")
//handler should print "Hello world !"
let tmp = Event<String>.create()
let dispose = tmp.event.subscribe(EventHandler<String>(handle: { sender, args in
print(args)
})
)
tmp.invoke(self,"Hello world !")
//handler should print "Hello world !"
dispose()
import swift_event
class Test {
private var _somethingHappened:Event<String>
public var somethingHappened: Event<String> {
return self._somethingHappened
}
private var _somethingHappenedInvoke:Delegate<String>
public init() {
let tmp = Event<String>.create()
self._somethingHappened = tmp.event
self._somethingHappenedInvoke = tmp.invoke
}
public func doSomething() {
self._somethingHappenedInvoke(self, "Hello !")
}
}
var test = Test()
var handler = EventHandler<String>(handle: {sender, args in print(args)})
test.somethingHappened += handler
test.doSomething() //should raise event, thus leading in an handler call
test.somethingHappened -= handler
test.doSomething() //should print nothing, as handler has been unsubscribe
Documentation is also provided as a .doccarchive
that includes some interractive tutorials, here : ./Doc/swift-event.doccarchive
Create an Event and return it along with its invoke method
note: this is the only way to instantiate an Event, this way only the class that call this method has access to invoke
returns: A tuple containing the created Event along with a pointer to its private invoke
Subscribe to an event by adding an handler
Unsubsribe from an event by removing handler
Subscribe to an event by adding an handler
note: shorthand to +=
parameter handler: the EventHandler to add
returns: a function that is a shorthand to -=, to ease unsubscribe
Unsubsribe from an event by removing handler
note: shorthand to -=
Typed EventHandler for typed Event
note: this class is needed as Swift doesn't allow func equity via ===
The Delegate that will handle the Event
Instanciate a new typed EventHandler
parameter handle: the associated Delegate to handle the Event
Shorthand for Event delegation
parameter sender: Sender of the event
parameter args: Event args
A. Because in swift func are not equatable
and doesn't supports ===
thus leaving unsubscribe
and -=
operator unimplemented.
A. To avoid call to invoke()
outside of creation scope. (like invoke() in c#
)
You can contribute to this repo via pull requests, be sure to follow the philosophy of this repo and to update documentation.
First version of the package.