Poller is a simple Kotlin library which runs a certain task at a regular interval
implementation("dev.mslalith:poller:0.3.0")
Use Poller.new
method to create new Poller. This takes
coroutineScope
- The scope in which the poll should execute
pollInterval
- Time in millis between each poll
pollStrategy
- The strategy for the poll to continue
val poller: Poller<Int> = Poller.new(
coroutineScope = coroutineScope,
pollInterval = 1_000,
pollStrategy = IndefiniteStrategy()
)
IndefiniteStrategy
- runs indefinitelyRetryLimitStrategy
- stop when retires are exhaustedTimeoutStrategy
- stop when time is exhausted
Poller exposes a StateFlow<PollerState>
which holds the current state of your Poll.
PollerState
can be of 4 states:
Initial
- the initial state when the poller is createdInProgress
- indicates the poll is in progress. This state holds the current result from thepollBlock
Complete
- the poll is completeCancelled
- poll was cancelled manually or the providedcoroutineScope
was cancelled
pollerStateFlow.collect { pollerState ->
when (pollerState) {
PollerState.Cancelled -> // handle cancel
PollerState.Complete -> // handle complete
is PollerState.InProgress -> // handle in progress
PollerState.Initial -> // handle initial state
}
}
Calling poll
will start the poll.
It takes a pollBlock
which executes on every poll.
The advantage with this is that your poll logic can be conditional if needed.
This will return a Job
which gives more control over your poll
val job = poller.poll {
// your poll logic
}
Calling stop
will stop the poll.
poller.stop()
Calling isPolling
will return a boolean which tells whether your poll is running or not
poller.isPolling()
Copyright 2022 M S Lalith
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.