Git Product home page Git Product logo

signals's Introduction

Signals

codebeat badge

A Signal is a way to decouple between a subject(dispatcher) and the observers(listeners) in distributed event handling systems.

Signals diagram

Setup

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.gazman-sdk:signals:1.5'
}

Kotlin example

fun interface Chat{
    fun onNewMessage(s:String)    
}

class Foo{
    val chatSignal = Signals.signal(Chat::class)
    
    fun bar(){
        chatSignal.addListener { s-> Log.d("chat", s) } // logs all the messaged to Logcat
    }
}

class Foo2{
    val chatSignal = Signals.signal(Chat::class)
    
    fun bar2(){
        chatSignal.dispatcher.onNewMessage("Hello from Foo2") // dispatches "Hello from Foo2" message to all the listeners
    }
}

Java example

interface Chat{
    void onNewMessage(String s);    
}

class Foo{
    Signal<Chat> chatSignal = Signals.signal(Chat.class);
    
    void bar(){
        chatSignal.addListener( s-> Log.d("chat", s) ); // logs all the messaged to Logcat
    }
}

class Foo2{
    Signal<Chat> chatSignal = Signals.signal(Chat.class);
    
    void bar2(){
        chatSignal.dispatcher.onNewMessage("Hello from Foo2"); // dispatches "Hello from Foo2" message to all the listeners
    }
}

The Signal is automatically created from the Chat interface. It allows Foo to register for it and Foo2 to dispatch new messages without interaction.

Signal API

Register/Unregister listeners

  • addListener(listener) - registers a listeners for this signal
  • addListenerOnce(listener) - registers a listeners for this signal and unregister it after the first dispatch
  • removeListener(listener) - removes a listener that was registered using one of the methods above

Dispatch events

  • dispatcher - property from the interface-type used to create this Signal. When it is invoked, it is propagating to all the listeners.
  • setInvoker(executor/handler) - by default, all the listeners, will be executed synchronously over the dispatcher thread. You can change this behavior by using an explicit executor/handler to execute all the calls for listeners

SignalsHelper

A helper class to unregister all the signals at once.

  • addListener(class, listener)
  • addListenerOnce(class, listener)
  • removeListener(class, listener)
  • removeAll() - Removes all the listeners registered via this SignalsHelper

G

G is a super light utility class to simplify the work with Signals.

  • G.app - app context. It is automatically obtained via a ContentProvider during the Application creation
  • G.main - Main handler
  • G.CE - A multithreaded cached executor

Behavior

  • Signals support modification during iteration. You can add and remove listeners during the dispatch phase, and it will take effect immediately. See the ListenersList implementation for details, as well as ListenersListTest
  • Signals is thread-safe, you can add/remove/clear and dispatch in a multithreaded environment

signals's People

Contributors

ilyagazman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

signals's Issues

Maven Version

Good afternoon Sir!

Would it be possible to also publish a Maven Version of this wonderful library?

Thanks in advance

Samsung

This library doesn't work on Samsung devices, doesn't allow to install apk, says it conflicts with an existing package

removeListener does not remove listener

Removing a listener does not work at all.

Log.d(TAG, "---------------Test 1------------")
val sig = Signals.localSignal(TestSignal1::class)
Log.d(TAG, "BEFORE has: ${sig.hasListeners()}") // false
sig.addListener(::testSig)
Log.d(TAG, "AFTER ADD has: ${sig.hasListeners()}") // true
sig.removeListener(::testSig)
Log.d(TAG, "AFTER REMOVE has: ${sig.hasListeners()}") // true

Log.d(TAG, "---------------Test 2------------")
val sig2 = Signals.localSignal(TestSignal2::class)
Log.d(TAG, "BEFORE has: ${sig2.hasListeners()}") // false
sig2.addListener { s -> Log.d("test", s) }
Log.d(TAG, "AFTER ADD has: ${sig2.hasListeners()}") // true
sig2.removeListener { s -> Log.d("test", s) }
Log.d(TAG, "AFTER REMOVE has: ${sig2.hasListeners()}") // true

Log.d(TAG, "---------------Test 3------------")
val sig3 = Signals.signal(TestSignal3::class)
Log.d(TAG, "BEFORE has: ${sig3.hasListeners()}") // false
sig3.addListener(::testSig)
Log.d(TAG, "AFTER ADD has: ${sig3.hasListeners()}") // true
sig3.removeListener(::testSig)
Log.d(TAG, "AFTER REMOVE has: ${sig3.hasListeners()}") // true

Log.d(TAG, "---------------Test 4------------")
val sig4 = Signals.signal(TestSignal4::class)
Log.d(TAG, "BEFORE has: ${sig4.hasListeners()}") // false
sig4.addListener { s -> Log.d("test", s) }
Log.d(TAG, "AFTER ADD has: ${sig4.hasListeners()}") // true
sig4.removeListener { s -> Log.d("test", s) }
Log.d(TAG, "AFTER REMOVE has: ${sig4.hasListeners()}") // true

fun testSig(s: String) {
}

fun interface TestSignal1 {
    fun test(s: String)
}

fun interface TestSignal2 {
    fun test(s: String)
}

fun interface TestSignal3 {
    fun test(s: String)
}

fun interface TestSignal4 {
    fun test(s: String)
}

 ---------------Test 1------------
BEFORE has: false
AFTER ADD has: true
AFTER REMOVE has: true
---------------Test 2------------
BEFORE has: false
AFTER ADD has: true
AFTER REMOVE has: true
---------------Test 3------------
 BEFORE has: false
AFTER ADD has: true
AFTER REMOVE has: true
---------------Test 4------------
BEFORE has: false
AFTER ADD has: true
AFTER REMOVE has: true

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.