-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2.x: RxJavaPlugins unwrapRunnable #5734
Changes from 15 commits
d0589d4
3be3fa0
f8ef988
e862c1f
07cbd46
31ff1e9
d5c2166
a75a7f1
b47fcd8
c0d6c78
f63a799
1db27f8
9703201
a5a7ce0
ebe2ca8
b2f0c86
bf72b36
171ac80
19eba48
2a8e6f0
f1af3f7
a1c5ad1
ac5c3f5
3c8aefd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* 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. | ||
*/ | ||
|
||
package io.reactivex.schedulers; | ||
|
||
import io.reactivex.Scheduler; | ||
import io.reactivex.annotations.*; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
/** | ||
* Marker interface to indicate wrapped action inside internal scheduler's task. | ||
* | ||
* Inside of the {@link RxJavaPlugins#onSchedule(Runnable)}, you can unwrap runnable from internal wrappers like | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe change to something like: You can check if runnable implements this interface and unwrap original runnable? |
||
* {@link Scheduler.DisposeTask}. | ||
* | ||
* @since 2.1.7 - experimental | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add "see also" with references to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about something like this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sgtm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I corrected it. |
||
*/ | ||
@Experimental | ||
public interface SchedulerRunnableIntrospection { | ||
|
||
/** | ||
* Returns the wrapped action. | ||
* | ||
* @return the wrapped action, may be null. | ||
*/ | ||
@Nullable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I corrected it |
||
Runnable getWrappedRunnable(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,4 +307,66 @@ public void customScheduleDirectDisposed() { | |
|
||
assertTrue(d.isDisposed()); | ||
} | ||
|
||
@Test | ||
public void unwrapDefaultPeriodicTask() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we move these tests to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I'm waiting for your decision :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we're waiting for @akarnokd to answer I'd recommend you to try to move this code to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want to check that all schedulers support this API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can get complicated and this is an optional introspection. If @lukaszguz wants to expand it, then sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, but I'd encourage @lukaszguz to try that and ask for help if needed, would be great to provide as robust API experience as possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's enough for now? I can always add a new PR with additional tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with this as is. We can always expand the feature set later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my first time when I'm doing PR to your library. Thanks to your tips, I know more or less what your requirements are and I can write better code. I think the better option would be to leave those tests in |
||
TestScheduler scheduler = new TestScheduler(); | ||
|
||
Runnable runnable = new Runnable() { | ||
@Override | ||
public void run() { | ||
} | ||
}; | ||
SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) scheduler.schedulePeriodicallyDirect(runnable, 100, 100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(runnable, wrapper.getWrappedRunnable()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertSame? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it. |
||
} | ||
|
||
@Test | ||
public void unwrapScheduleDirectTask() { | ||
TestScheduler scheduler = new TestScheduler(); | ||
|
||
Runnable runnable = new Runnable() { | ||
@Override | ||
public void run() { | ||
} | ||
}; | ||
SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) scheduler.scheduleDirect(runnable, 100, TimeUnit.MILLISECONDS); | ||
assertEquals(runnable, wrapper.getWrappedRunnable()); | ||
} | ||
|
||
@Test | ||
public void unwrapWorkerPeriodicTask() { | ||
final Runnable runnable = new Runnable() { | ||
@Override | ||
public void run() { | ||
} | ||
}; | ||
|
||
Scheduler scheduler = new Scheduler() { | ||
@Override | ||
public Worker createWorker() { | ||
return new Worker() { | ||
@Override | ||
public Disposable schedule(Runnable run, long delay, TimeUnit unit) { | ||
SchedulerRunnableIntrospection outerWrapper = (SchedulerRunnableIntrospection) run; | ||
SchedulerRunnableIntrospection innerWrapper = (SchedulerRunnableIntrospection) outerWrapper.getWrappedRunnable(); | ||
assertEquals(runnable, innerWrapper.getWrappedRunnable()); | ||
return (Disposable) innerWrapper; | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return false; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
scheduler.schedulePeriodicallyDirect(runnable, 100, 100, TimeUnit.MILLISECONDS); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a marker interface if it declares method(s) that can be consumed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True :)