-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workers. Common factory for
WorkerFactory
- Loading branch information
Showing
3 changed files
with
49 additions
and
48 deletions.
There are no files selected for viewing
30 changes: 6 additions & 24 deletions
30
kotlin-browser/src/jsMain/kotlin/web/workers/SharedWorkerFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,12 @@ | ||
package web.workers | ||
|
||
import js.coroutines.internal.IsolatedCoroutineScope | ||
import js.globals.globalThis | ||
import js.reflect.unsafeCast | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.launch | ||
|
||
fun SharedWorkerFactory( | ||
block: suspend CoroutineScope.(self: SharedWorkerGlobalScope) -> Unit, | ||
): WorkerFactory<SharedWorker> { | ||
val self = if (globalThis["SharedWorkerGlobalScope"] != null) { | ||
globalThis as? SharedWorkerGlobalScope | ||
} else null | ||
|
||
requireNotNull(self) { | ||
"Invalid shared worker context! `SharedWorkerGlobalScope` as `self` is required!" | ||
} | ||
|
||
IsolatedCoroutineScope() | ||
.launch( | ||
start = CoroutineStart.UNDISPATCHED, | ||
block = { block(self) }, | ||
) | ||
|
||
return unsafeCast { | ||
error("Missed plugin integration! SharedWorker factory shouldn't be called directly!") | ||
} | ||
} | ||
): WorkerFactory<SharedWorker> = | ||
createWorkerFactory( | ||
workerName = "SharedWorker", | ||
scopeClassName = "SharedWorkerGlobalScope", | ||
block = block, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
kotlin-browser/src/jsMain/kotlin/web/workers/createWorkerFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package web.workers | ||
|
||
import js.coroutines.internal.IsolatedCoroutineScope | ||
import js.globals.globalThis | ||
import js.reflect.unsafeCast | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.launch | ||
import kotlin.reflect.cast | ||
|
||
internal fun <T : AbstractWorker, S : WorkerGlobalScope> createWorkerFactory( | ||
workerName: String, | ||
scopeClassName: String, | ||
block: suspend CoroutineScope.(self: S) -> Unit, | ||
): WorkerFactory<T> { | ||
val self: S = getGlobalScope(scopeClassName) | ||
|
||
IsolatedCoroutineScope() | ||
.launch( | ||
start = CoroutineStart.UNDISPATCHED, | ||
block = { block(self) }, | ||
) | ||
|
||
return unsafeCast { | ||
error("Missed plugin integration! $workerName factory shouldn't be called directly!") | ||
} | ||
} | ||
|
||
private fun <S : WorkerGlobalScope> getGlobalScope( | ||
scopeClassName: String, | ||
): S { | ||
val jsClass = globalThis[scopeClassName] | ||
?: error("Class `$scopeClassName` not found!") | ||
|
||
val kotlinClass = unsafeCast<JsClass<S>>(jsClass).kotlin | ||
return kotlinClass.cast(globalThis) | ||
} |