Git Product home page Git Product logo

rxtask's Introduction

RxTask

a easy Asynchronous Library by RxJava3.X

get lib version by use this web link: https://jitpack.io/#KilleTom/rxtask

be careful v0.0.1-beta defalut scheduler was android

but start from v1.0.3-beta version you can use other scheduler

and now v1.1.0 was first release version.

TobeUse

Step 1.in you project build.gradle(Project:XXXX) file, make repositories maven https://jitpack.io

look like this

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

dependencies lib

now lib version was: v1.1.0

libRxtask a base lib, use this can run on webService or other

libRxtaskAndroidScheduler a expand android lib

not run on android just like this dependdencies

dependencies {
  //rxTask baselib
  implementation 'com.github.KilleTom:rxtask:libRxtask:version'
}

run on android must write this:

dependencies {
  //rxTask baselib
  implementation 'com.github.KilleTom:rxtask:libRxtask:version'
  //android expand scheduler lib
  implementation 'com.github.KilleTom:rxtask:libRxTaskAndroidExpand:version'
}

if you don't want find lib version just write like this

dependencies {
   //rxTask base lib 
    implementation 'com.github.KilleTom.rxtask:libRxtask:v1.1.0'
    
   
    //run on android must use : android expand  lib 
    implementation 'com.github.KilleTom.rxtask:libRxTaskAndroidExpand:v1.1.0'
}

use lib way:

init gloabl defalut scheduler

// init RxTask gloabl defalut scheduler
//for example like android use this lib init gloable defalut scheduler
RxTaskSchedulerManager.setLocalScheduler(RxAndroidDefaultScheduler())

sometime you don't want asynchronous run on default schedule you can make this asynchronous run on other schedule

asynchronous operation to be easy use

  • use gloable defalut scheduler
//simple asynchronous operation
 val singleTask = RxSingleEvaluationTaskTask.createTask {

            //get news
            val response = okHttpClient.newCall(createRequest(createNewUrl("top")))
                .execute()

            val body = response.body ?: throw RuntimeException("body null")

            val result = Gson().fromJson(body.string(), JsonObject::class.java)

            return@createTask result
        }.successAction {
            //do your sucess logic
            Log.i("KilleTom", "$it")
        }.failAction {
            //do your fail logic
            it.printStackTrace()
      }.start()
  • run on designated schedule
 val singleTask = RxSingleEvaluationTaskTask.createTask({

            val response = okHttpClient.newCall(createRequest(createNewUrl("top")))
                .execute()

            val body = response.body ?: throw RuntimeException("body null")

            val result = Gson().fromJson(body.string(), JsonObject::class.java)

            return@createTask result
        },
        // like this use a  designated schedule for RxAndroidDefaultScheduler
        RxAndroidDefaultScheduler()
        ).successAction {
         
            Log.i("KilleTom", "$it")
        }.failAction {
            
            it.printStackTrace()
      }.start()

asynchronous operation can be has progress call

  • like this android example use a init gloable defalut scheduler
 val progressTask = RxProgressEvaluationTaskTask
            .createTask<JsonObject, Boolean> { task ->

                val types = arrayListOf<String>("top", "shehui", "guonei")

                types.forEach { value ->

                    val result = okHttpClient
                        .newCall(createRequest(createNewUrl(value)))
                        .execute()

                    val body = result.body ?: throw RuntimeException("body null")

                    val jsonObject = Gson().fromJson(body.string(), JsonObject::class.java)

                    Log.d("KilleTom", "推送新闻类型$value")
                    
                    //pulish progress
                    task.publishProgressAction(jsonObject)
                    
                }
                //asynchronous result
                return@createTask true
                
            }.progressAction {
                //do on progress call logic
                Log.i("KilleTom", "收到进度,message:$it")
            }.successAction {
                // do sucess logic
                Log.i("KilleTom", "Done")
            }.failAction {
                // do fail logic
                Log.i("KilleTom", "error message:${it.message ?: "unknown"}")
            }.start()
  • run on designated schedule
 val progressTask = RxProgressEvaluationTaskTask
            .createTask<JsonObject, Boolean> ({ task ->

                val types = arrayListOf<String>("top", "shehui", "guonei")

                types.forEach { value ->

                    val result = okHttpClient
                        .newCall(createRequest(createNewUrl(value)))
                        .execute()

                    val body = result.body ?: throw RuntimeException("body null")

                    val jsonObject = Gson().fromJson(body.string(), JsonObject::class.java)

                    Log.d("KilleTom", "推送新闻类型$value")
                    
                    task.publishProgressAction(jsonObject)
                }
                
                return@createTask true
            },
            // like this use a  designated schedule for RxAndroidDefaultScheduler
            RxAndroidDefaultScheduler())
            .progressAction {
                Log.i("KilleTom", "收到进度,message:$it")
            }.successAction {
                Log.i("KilleTom", "Done")
            }.failAction {
                Log.i("KilleTom", "error message:${it.message ?: "unknown"}")
            }.start()

asynchronous operation can be timer

RxTimerTask.createTask { task ->

            if (task.getTimeTicker().countTimes >= 10) {
                task.cancel()
            }
            // do your logic
            
            return@createTask

        }.setDelayTime(0L)
            .setIntervalTime(1000)
            .setTaskScheduler(Schedulers.computation())
            .start()

rxtask's People

Contributors

killetom avatar

Stargazers

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

Watchers

 avatar  avatar  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.