Giter Club home page Giter Club logo

swiftevents's Introduction

SwiftEvents

Coming from actionscript 3 i have decided to integrate event system into swift. Since swift does not support function pointers, it was required to change the passing also the object self.

Extend the EventDispatcher into your dispatching classes. Also you can implement the IEventDispatcherProtocol into any classes for example any views that cannot extend the EventDispatcher.

Usage :

1. Adding events listeners to internal handlers
   
view1.addEventListener("someEvent1", withFunction: view1.handler1)
    view1.addEventListener("someEvent2", withFunction: view1.handler2)
    view2.addEventListener("someEvent2", withFunction: view2.handler2)
    
    2. View1 will not be interested in any events after this line
    view1.removeAllListeners()
    
3. Triggering events
    view1.dispatchEvent(Event(type: "someEvent1")) // won't trigger anything
    view1.dispatchEvent(Event(type: "someEvent2")) // executes handler 2 in view2
    
    4. We can use the global dispatcher
    EventHub.instance.trigger(Event(type :"someEvent2")); // executes handler 2 in view2
    
5. Removing just one event type from the listening
    view2.removeEventListener("someEvent2")
    
    6. Using the global dispatcher again ( just to check if we have removed it )
    EventHub.instance.trigger(Event(type :"someEvent2")); // silence, no one is interested in that event
    
    7. Custom events (supporting custom data)
    view2.addEventListener(CustomEvent.CUSTOM_TYPE, withFunction: customEventHandler)
    
    var customEvent : CustomEvent = CustomEvent(attribute: "someCustomType")
    EventHub.instance.trigger(customEvent)

8. Added listening trough a global event listener, if you are too lazy to inherit/implement the dispatcher. 	However use this only for testing since its a bad practice.

// too lazy to extend the EventDispatcher ?
    EventHub.instance.addEventListener("some_generic_event", withFunction: test)
    EventHub.instance.trigger(Event(type: "some_generic_event")) // --> outerListener
    EventHub.instance.removeEventListener("some_generic_event")
    EventHub.instance.trigger(Event(type: "some_generic_event")) // --> silence

Limitations :

Since swift cannot compare the pointers of functions it is not possible to have two different handlers for one event type for one object.

Example : Have only one handler per event type per object, the following example will not work. Only the first handler should trigger.

    view1.addEventListener("test_limitations", withFunction: limitations1)
    view1.addEventListener("test_limitations", withFunction: limitations2)
    view2.addEventListener("test_limitations", withFunction: limitations2) // However this is fine
    
    EventHub.instance.trigger(Event(type:"test_limitations")) // dispatched once with warning

swiftevents's People

Contributors

philipg-pm avatar philipgeorgiev123 avatar

Watchers

Carabineiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.