package Ġcom . intellij . space . runtime Ċ Ċ import Ġcom . intellij . openapi . application . Application Ċ import Ġcom . intellij . openapi . application . Modal ity State Ċ import Ġkotlinx . coroutines .* Ċ import Ġlibraries . k logging . logger Ċ import Ġruntime . C ancellable Ċ import Ġruntime . Coroutine Exception Logger Ċ import Ġruntime . Dispatcher Ċ import Ġruntime . Task C ancellable Ċ import Ġjava . util . concurrent . Executors Ċ import Ġjava . util . concurrent . Future Ċ import Ġjava . util . concurrent . Scheduled ExecutorService Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġkotlin . coroutines . Coroutine Context Ċ Ċ val Ġlog Ġ= Ġlogger < Application Dispatcher >() Ċ Ċ class ĠApplication Dispatcher ( private Ġval Ġapplication : ĠApplication ) Ġ: ĠDispatcher Ġ{ ĊĠ Ġprivate Ġval Ġexecutor Ġ= ĠExec utors . new Single Thread Scheduled Executor Ġ{ Ġrunnable Ġ-> ĊĠĠĠ Ġval Ġthread Ġ= ĠThread ( run nable , Ġ" Application ĠA ux iliary ĠScheduler ") ĊĊĠĠĠ Ġthread . is Daemon Ġ= Ġtrue ĊĠĠĠ Ġthread ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġcontext Ġ= ĠApplication Coroutine Context ( application , Ġexecutor ) ĊĊĠ Ġprivate Ġval Ġcontext With Log Ġ= Ġcontext Ġ+ ĠCor outine Exception Logger . create ( log ) ĊĊĠ Ġoverride Ġval Ġcoroutine Context : ĠCor outine Context ĊĠĠĠ Ġget () Ġ= Ġcontext With Log ĊĊĠ Ġoverride Ġfun Ġdispatch ( fn : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠĠĠ Ġapplication . invoke Later ( fn , ĠMod ality State . any ()) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġdispatch ( delay : ĠInt , Ġfn : Ġ() Ġ-> ĠUnit ): ĠC ancellable Ġ{ ĊĠĠĠ Ġval Ġinvoke Ġ= Ġjava . lang . Runnable Ġ{ ĊĠĠĠĠĠ Ġapplication . invoke Later ( fn , ĠMod ality State . any ()) ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġdis posable Ġ= Ġexecutor . schedule ( invoke , Ġdelay . to Long (), ĠTimeUnit . MILLISECONDS ) ĊĊĠĠĠ Ġreturn ĠTask C ancellable ( dis posable ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġdispatch Interval ( delay : ĠInt , Ġinterval : ĠInt , Ġfn : Ġ() Ġ-> ĠUnit ): ĠC ancellable Ġ{ ĊĠĠĠ Ġval Ġinvoke Ġ= Ġjava . lang . Runnable Ġ{ ĊĠĠĠĠĠ Ġapplication . invoke Later ( fn , ĠMod ality State . any ()) ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġdis posable Ġ= Ġexecutor . schedule At Fixed Rate ( invoke , Ġdelay . to Long (), Ġinterval . to Long (), ĠTimeUnit . MILLISECONDS ) ĊĊĠĠĠ Ġreturn ĠTask C ancellable ( dis posable ) ĊĠ Ġ} Ċ } Ċ Ċ @ Use Experimental ( Internal Cor outines Api :: class ) Ċ private Ġclass ĠApplication Coroutine Context ( ĊĠ Ġprivate Ġval Ġapplication : ĠApplication , ĊĠ Ġprivate Ġval Ġexecutor : ĠScheduled ExecutorService Ċ ) Ġ: ĠCor outine Dispatcher (), ĠDelay Ġ{ ĊĊĠ Ġoverride Ġfun Ġdispatch ( context : ĠCor outine Context , Ġblock : ĠRunnable ) Ġ{ ĊĠĠĠ Ġapplication . invoke Later ( block , ĠMod ality State . any ()) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġschedule Resume After Delay ( time Millis : ĠLong , Ġcontinuation : ĠC ancellable Continuation < Unit >) Ġ{ ĊĠĠĠ Ġval Ġto Resume Ġ= Ġjava . lang . Runnable Ġ{ ĊĠĠĠĠĠ Ġapplication . invoke Later ({ Ġwith ( continu ation ) Ġ{ Ġthis @ Application Coroutine Context . resume Un dis patched ( Unit ) Ġ} Ġ}, ĠMod ality State . any ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġexecutor . schedule ( to Resume , Ġtime Millis , ĠTimeUnit . MILLISECONDS ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġinvoke On Timeout ( time Millis : ĠLong , Ġblock : ĠRunnable ): ĠDis posable Handle Ġ{ ĊĠĠĠ Ġval Ġto Resume Ġ= Ġjava . lang . Runnable Ġ{ ĊĠĠĠĠĠ Ġapplication . invoke Later ({ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġblock . run () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ}, ĠMod ality State . any ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġreturn ĠDis posable Future Handle ( executor . schedule ( to Resume , Ġtime Millis , ĠTimeUnit . MILLISECONDS )) ĊĠ Ġ} Ċ } Ċ Ċ private Ġclass ĠDis posable Future Handle ( private Ġval Ġfuture : ĠFuture <* >) Ġ: ĠDis posable Handle Ġ{ ĊĠ Ġoverride Ġfun Ġdispose () Ġ{ ĊĠĠĠ Ġfuture . cancel ( false ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠtoString (): ĠString Ġ= Ġ" Disposable Future Handle [$ future ]" Ċ } Ċ