Comments (10)
@nmDejamobile Yes I do, @cesarferreira Sad to hear that, hope you'll get the time to get back to it, you did a great job here :-)
from swifteventbus.
Sure :-)
from swifteventbus.
No problem mate. I am sorry for the delay, I was and still am really busy with work.
I hope I'll have some time this weekend.
from swifteventbus.
Can you elaborate please?
from swifteventbus.
Sure, most of my project is based on structs rather then classes, and EventBus can't accept struct base "classes" as target.
For now I've worked around this by using actual classes. it would be nice if there were a way to set structs as target as well.
from swifteventbus.
;@shshalom Do you mean structs as "sender" param ? If yes, I agree it would be nice to pass complex data structures without the need of an object
from swifteventbus.
Hey guys,
To be honest I haven't touched Swift in like 2 years (full time android) and I don't quite have the time atm to re-learn it and learn the 2.0, if you guys can make a PR with the changes I'll merge it asap 😃
from swifteventbus.
Hey @cesarferreira and @nmDejamobile.
I finally had some time trying to make SwiftEventBus to work with structs, and i think I've made it!
with the following method you can post complicated structs as sender as well setting structs as target.
Allow me to show you how I would use it first, and then how it has been done:
public struct Employee{
public var id:Int
public var name:String
}
public struct EmployeeManager {
private var list:[Int:Employee] = [:]
public init() {
var `self` = self //required to allow mutating list inside the handler
EventBus<Employee>.main(self, name: "NewWorkerEvent", handler: { newWorker in
if let worker = newWorker {
self.list[worker.id] = worker
print(worker.name)
}
})
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
_ = EmployeeManager()
//Posting events with new structs....
EventBus.post(name:"NewWorkerEvent", sender: Employee(id: 0, name: "UserA"))
EventBus.post(name:"NewWorkerEvent", sender: Employee(id: 1, name: "UserB"))
EventBus.post(name:"NewWorkerEvent", sender: Employee(id: 2, name: "UserC"))
EventBus.post(name:"NewWorkerEvent", sender: Employee(id: 3, name: "UserD"))
}
}
Now, the way I made it work:
I've created an EventObject that basically take what ever generic type.
open class EventObject<T>:Any {
var object:T!
public convenience init (data:T) {
self.init()
self.object = data
}
}
For the sake of this example I've created a generic class that allows passing any kind of object as the sender or as target.
open class EventBus<T> {
open class func post(name: String, sender: T? = nil) {
NotificationCenter.default.post(name: Notification.Name(rawValue: name), object:
EventObject<T>(data:sender!))
}
@discardableResult
open class func main(_ target: AnyObject, name: String, handler: @escaping (T?) -> Void) -> NSObjectProtocol {
return SwiftEventBus.on(target, name: name, sender: nil, queue: OperationQueue.main, handler: { result in
if let object:T = (result.object as? EventObject<T>)?.object {
handler(object)
}else{
handler(nil)
}
})
}
@discardableResult
open class func main<Target>(_ target:Target, name: String, handler: @escaping (T?) -> Void) -> NSObjectProtocol {
let targetObject = EventObject(data: target)
return SwiftEventBus.on(targetObject, name: name, sender: nil, queue: OperationQueue.main, handler: { result in
if let object:T = (result.object as? EventObject<T>)?.object {
handler(object)
}else{
handler(nil)
}
})
}
}
Let me know what do you think, and if you would like me to create PR.
from swifteventbus.
Looks good :)
can you create a pull request and write a few tests?
from swifteventbus.
I'll close this issue waiting for the PR :)
from swifteventbus.
Related Issues (20)
- WatchOS support HOT 1
- why the versoin is also 2.2.0 HOT 3
- should I call SwiftEventBus.unregister(self) in deinit HOT 1
- Your 3.0.0 tag has the wrong podspec version (2.2.0) HOT 1
- can't get the newest version in the standard way
- How to create an new instance HOT 1
- Conflicting overloading functions HOT 2
- Publish 3.0.1 to Cocoapods HOT 1
- Update swift version to 5
- Swift Package Manager HOT 5
- Module compiled with Swift 5.1.3 cannot be imported by the Swift 5.1 compiler: HOT 3
- Static.instance.cache is not clearing automatically unless unregister the target
- compiler warning HOT 1
- Check if event is already registered HOT 4
- Cocoapods downloads 5.0.0 instead of the latest
- Module compiled with Swift 5.3.2 cannot be imported by the Swift 5.4.2 compiler HOT 2
- Update podspec to 5.1.0 HOT 1
- event only triggered once
- Could you add the SWIFT_VERSION attribute in at least one of the targets? HOT 1
- Why hasn't it been updated in years
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swifteventbus.