Skip to content

Publish/Subscribe mechanism in Kotlin. (Event-Bus like)

License

Notifications You must be signed in to change notification settings

Yummypets/Notifier.kt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Notifier.kt

Language: Kotlin Platform: Android 8+ codebeat badge License: MIT

Publish/Subscribe mechanism in Kotlin. (Event-Bus like)

// Subscribe
subscribeFor<UserDidLoginEvent> { event ->
    // do something cool
}

// Publish
publishEvent(UserDidLoginEvent())

About

  • Simple & type-safe api (generics FTW)
  • Lightweight: 1 file ~ 50 lines of pure kotlin
  • Does NOT rely on evil Annotations
  • Familiar to the well-known EventBus Library

Installation

Just copy and paste Notifier.kt file in your project.
Make sure you import the kotlinx.coroutines on which Notifier relies.

Usage

1 - Create your event

An event is a pure kotlin class. You can use fields to store whatever data you want to pass along with the event.

data class NewPostEvent(val newPost: Post)

2 - Subscribe to the event

First you need to add the NotifierSubscriber interface. This will force you to implement a notifierToken used for future cancellation & give you the ability to call subscribeFor<Event>.

class MySubscriberFragment: Fragment(), NotifierSubscriber {

    // Store a cancellation token
    override var notifierToken = NotifierToken()

    override fun onStart() {
        super.onStart()

        // Subscribe
        subscribeFor<NewPostEvent>{ event ->
            print(event.newPost)
        }
    }

    override fun onStop() {
        super.onStop()
        notifierToken.unsubscribe()
    }
}

In an android Fragment, you would typically subscribe in onStart and unsubscribe in onStop. This way, events won't be triggered after the fragment stopped.

3 - Publish your event

Add the NotifierPublisher interface, this will give you the ability to call publishEvent as shown in the example below.

class MyPublisherFragment: Fragment(), NotifierPublisher {
    fun foo() {
        publishEvent(NewPostEvent(myNewPost)
    }
}

Contributors

S4cha, PAFagniez

About

Publish/Subscribe mechanism in Kotlin. (Event-Bus like)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages