瀏覽代碼

| Interval没有观察者时执行reset重置轮循器崩溃

drake 5 年之前
父節點
當前提交
5203a301d8

+ 10 - 9
README.md

@@ -58,7 +58,7 @@ allprojects {
 module 的 build.gradle
 
 ```groovy
-implementation 'com.github.liangjingkanji:Net:1.3.4'
+implementation 'com.github.liangjingkanji:Net:1.3.5'
 ```
 
 
@@ -295,7 +295,7 @@ post<Model>(""){
 
 如果是`net`跟随生命周期
 
-```
+```kotlin
 post<Model>(""){
   param("key", "value")
 }.net(activity, Lifecycle.Event.ON_DESTROY) { 
@@ -534,7 +534,7 @@ abstract class DefaultConverter(
 本框架附带一个超级强大的轮循器`Interval`, 基本上包含轮循器所需要到所有功能
 
 - 支持多个观察者订阅同一个计时器
-- 开始(即subscribe订阅) | 暂停 | 继续 | 停止
+- 开始 | 暂停 | 继续 | 停止
 - 同时包含RxJava的操作符`interval|intervelRange`功能
 - 轮循器即Observable对象, 所以可以进行任意操作符转换. 例如轮循器控制网络请求
 
@@ -554,10 +554,10 @@ reset // 重置轮循器(包含计数器count和计时period)
 
 ### 事件间隔
 
-修复官方timInterval初始事件依然存在时间间隔问题
+可以在观察者中接收到相邻两次事件之间的事件间隔, 第一次事件的间隔(`time()`)是: -1
 
 ```
-Observabl.interval
+Observabl().interval()
 ```
 
 
@@ -599,17 +599,18 @@ class MainActivity : AppCompatActivity() {
 
 
 
-```
+```kotlin
 from { 
 
 // 返回值为事件
-}.auto(this).subscribe { 
+  8
+}.subscribe { 
 	
 }
 
 shoot<Int> { 
 	it.onNext(2)
-}.auto(this).subscribe { 
+}.subscribe { 
 	
 }
 ```
@@ -626,7 +627,7 @@ shoot<Int> {
 from { 
   // 这里属于IO线程 
   8
-}.io().observeMain.auto(this).subscribe { 
+}.io().observeMain.subscribe { 
   // 这里属于主线程
   // 这里会受到 事件 8
 }

+ 7 - 6
net/src/main/java/com/drake/net/observable/Interval.kt

@@ -46,7 +46,7 @@ class Interval(
     private var observerList = ArrayList<IntervalRangeObserver>()
     private var pause = false
     private var stop = false
-    private lateinit var dispose: Disposable
+    private var dispose: Disposable? = null
     private val iterator = {
 
         if (!pause) {
@@ -61,17 +61,18 @@ class Interval(
 
     public override fun subscribeActual(observer: Observer<in Long?>) {
 
-        val agentObserver =
-            IntervalRangeObserver(observer)
+        val agentObserver = IntervalRangeObserver(observer)
         observerList.add(agentObserver)
 
         observer.onSubscribe(agentObserver)
 
-        if (!this::dispose.isInitialized) init()
+        if (dispose == null) init()
         agentObserver.setResource(dispose)
     }
 
     private fun init() {
+        if (observerList.isEmpty()) return
+
         dispose = if (scheduler is TrampolineScheduler) {
             val worker = scheduler.createWorker()
             worker.schedulePeriodically(iterator, initialDelay, period, unit)
@@ -103,7 +104,7 @@ class Interval(
      */
     fun reset() {
         count = start
-        dispose.dispose()
+        dispose?.dispose()
         init()
     }
 
@@ -123,7 +124,7 @@ class Interval(
 
     // </editor-fold>
 
-    class IntervalRangeObserver(
+    private class IntervalRangeObserver(
         private val downstream: Observer<in Long?>
     ) : AtomicReference<Disposable?>(), Disposable {
 

+ 0 - 5
sample/src/main/java/com/drake/net/sample/App.kt

@@ -2,9 +2,6 @@ package com.drake.net.sample
 
 import android.app.Application
 import com.drake.net.initNet
-import com.scwang.smart.refresh.footer.ClassicsFooter
-import com.scwang.smart.refresh.header.ClassicsHeader
-import com.scwang.smart.refresh.layout.SmartRefreshLayout
 
 class App : Application() {
 
@@ -19,7 +16,5 @@ class App : Application() {
             })*/
         }
 
-        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout -> ClassicsHeader(this) }
-        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> ClassicsFooter(this) }
     }
 }

+ 0 - 42
sample/src/main/res/layout/activity_main.xml

@@ -7,46 +7,4 @@
     android:orientation="vertical"
     tools:context=".MainActivity">
 
-
-    <TextView
-        android:id="@+id/textView"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView2"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView3"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView4"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView5"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView6"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
-
-    <TextView
-        android:id="@+id/textView7"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="TextView" />
 </LinearLayout>