Coder Social home page Coder Social logo

router's Introduction

Latest version

router 1.2.8 —— modified code to avoid concurrent bugs

router 1.2.6 -- use apt to process subscribe annotation

router 1.2.0 -- use WeakReference to avoid MemoryLeak cause by forgot unregister

Project

It had been used in project KnowWeather and diffadapter demo ,you can learn more.

What's Router

English | 中文文档

Router -- A substitute good of EventBus similar implemented by dynamic proxy

dynamic proxy

what's dynamic proxy and how to use , see my blog 代理模式的学习与应用

Background

EventBus is good ,but there are some problems when use.

  • EventBus is not friendly to IDE such as AS.so on registered method ,control+click(or other shortcut key) can't take you to what's actually being called , and there no any prompt when you post a event even you get a mistake.

  • when called post(Event event),EventBus use Event type to find the received method ,so you must define many class for different received methods to avoid invoked unexpected even they have same class type or you only want dispatch a base type such as a int value

above all , when project gets larger or multiple developers work on project,which greatly increases the cost of developing and maintaining.

Feature

  • take even no time and memory when register , only add a object reference to a map.
  • friendly to IDE, you can easily find the action from where it was called. and when you try to send a event there while be a prompt by ide.
  • defined method to match event instead of many methods.
  • both Router and MVP design model using implement interface as a way of communication ,so it fit perfectly in MVP .
  • proven in practice by mature apps.

Use

Router is easily use especially when you had used EventBus before.

Step1. define interface and method whatever you want

interface AnyInterface {
    void anyMethod(AnyType posting) ;
}

Step2. register to Router where you need implement the interface

class AnyClass implement AnyInterface {

    //register on a appropriate lifecycle or constructor
    Router.getInstance().register(this); 
    
    //it will be invoked on UI Thread
    void anyMethod(AnyType object)  {
        //do what you want
    }
}

Step3. invoke the method you defined before

Router.getInstance().getReceiver(AnyInterface.class).anyMethod(AnyType object);

Step4. unregister when not use anymore or a appropriate lifecycle

Router.getInstance().unregister(this);

now a publish/subscribe is completed,simple as EventBus.

Method Invoked Thread

Use Annotation invoke method in different threads

 @Subscribe(runThread = RunThread.MAIN)
 @Subscribe(runThread = RunThread.POSTING)
 @Subscribe(runThread = RunThread.ASYNC)
 @Subscribe(runThread = RunThread.BACKGROUND)

Just like EventBus's way to reduce learning cost. if not add Annotation, will use @Subscribe(runThread = RunThread.Main) as default.

Annotation Mode

mode 1 :add Annotation on implemented method, it as default
@Subscribe(runThread = RunThread.BACKGROUND)
public void anyMethod(String msg) {
    ...
}

anyMethod will invoked on RunThread.BACKGROUND thread

mode 2 :add Annotation on interface's method
Router.getInstance().setAnnotateMethodOnInterface(true);

interface TestRunThread {
     @Subscribe(runThread = RunThread.POSTING)
     void anyMethod(String posting) ;
}

on this mode, all class which implement the method will be invoked on the RunThread.POSTING thread.

more use details , see the simple.

Add to project

gradle

compile 'com.silencedut:router:1.2.8'

maven

<dependency>
  <groupId>com.silencedut</groupId>
  <artifactId>router</artifactId>
  <version>1.2.8</version>
  <type>pom</type>
</dependency>

License

Copyright 2015-2016 SilenceDut

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

router's People

Contributors

silencedut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

router's Issues

采用编译时处理注解?

发送消息时通过动态代理获取接收消息的方法,运用了反射获取了方法和注解,使用编译时处理注解貌似是多余的吧?并没有通过编译时提高性能,不知我是否理解错望解答

通过router传递数据时,报这个错误!

Caused by: com.silencedut.router.RouterException: UnHandler Exception when method invoke ,java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Handler android.app.FragmentHostCallback.getHandler()' on a null object reference

同一个页面,有时报,有时不报,很费解!

请教一个问题

如果是组件化开发,从一个组件调用事件总线,然后另一个组件就接收不到了。请问是不是不能在组件中使用?

MethodThreadFinder

There is no com.silencedut.router.MethodThreadFinder can be found.

ThreadFinder threadFinderFinder = null;
        try {  
            Class<?> methodThreadCls = Class.forName("com.silencedut.router.MethodThreadFinder");  
            threadFinderFinder = (ThreadFinder) methodThreadCls.newInstance();  
            String fullMethodName = mReceiver.getClass().getName()+"."+mInvokedMethod.getName();  
            runThread = RunThread.valueOf(threadFinderFinder.getMethodThread(fullMethodName));  
            Log.d("Reception","Use Annotation ahead of runtime");  
        } 

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.