Skip to content

Commit

Permalink
add wasmJs target
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Jan 23, 2024
1 parent fc34674 commit c9c849e
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 4 deletions.
13 changes: 13 additions & 0 deletions projects/core/koin-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand All @@ -15,6 +16,11 @@ kotlin {
binaries.executable()
}

wasmJs {
binaries.executable()
nodejs()
}

iosX64()
iosArm64()
iosSimulatorArm64()
Expand Down Expand Up @@ -47,5 +53,12 @@ tasks.withType<KotlinCompile>().all {
jvmTarget = "1.8"
}
}
rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "21.0.0-v8-canary202309143a48826a08"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}
apply(from = file("../../gradle/publish.gradle.kts"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2017-Present the original author or authors.
*
* 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 org.koin.core.context

import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.error.ApplicationAlreadyStartedException
import org.koin.core.module.Module
import org.koin.dsl.KoinAppDeclaration

/**
* Global context - current Koin Application available globally
*
* Support to help inject automatically instances once KoinApp has been started
*
* @author Arnaud Giuliani
*/
object GlobalContext : KoinContext {

private var _koin: Koin? = null

override fun get(): Koin = _koin ?: error("KoinApplication has not been started")

override fun getOrNull(): Koin? = _koin

private fun register(koinApplication: KoinApplication) {
if (_koin != null) {
throw ApplicationAlreadyStartedException("A Koin Application has already been started")
}
_koin = koinApplication.koin
}

override fun stopKoin() {
_koin?.close()
_koin = null
}

override fun startKoin(koinApplication: KoinApplication): KoinApplication {
register(koinApplication)
return koinApplication
}

override fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication {
val koinApplication = KoinApplication.init()
register(koinApplication)
appDeclaration(koinApplication)
return koinApplication
}

override fun loadKoinModules(module: Module,createEagerInstances : Boolean) {
get().loadModules(listOf(module), createEagerInstances = createEagerInstances)
}

override fun loadKoinModules(modules: List<Module>, createEagerInstances : Boolean) {
get().loadModules(modules, createEagerInstances = createEagerInstances)
}

override fun unloadKoinModules(module: Module) {
get().unloadModules(listOf(module))
}

override fun unloadKoinModules(modules: List<Module>) {
get().unloadModules(modules)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017-Present the original author or authors.
*
* 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 org.koin.core.logger

/**
* Logger that print on system.out
* @author - Arnaud GIULIANI
*/
class PrintLogger(level: Level = Level.INFO) : Logger(level) {

override fun display(level: Level, msg: MESSAGE) {
println("[$level] $KOIN_TAG $msg")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.koin.mp

import kotlin.time.TimeSource

actual object KoinPlatformTimeTools {
actual fun getTimeInNanoSeconds(): Long {
return TimeSource.Monotonic.markNow().elapsedNow().inWholeNanoseconds
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2017-present the original author or authors.
*
* 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 org.koin.mp

import co.touchlab.stately.concurrency.ThreadLocalRef
import org.koin.core.context.GlobalContext
import org.koin.core.context.KoinContext
import org.koin.core.logger.*
import kotlin.random.Random
import kotlin.reflect.KClass

// actual object PlatformTools {
// actual fun getClassName(kClass: KClass<*>): String {
// return kClass.simpleName ?: "KClass@${hashCode()}"
// }
//
// actual fun printStackTrace(throwable: Throwable) {
// throwable.printStackTrace()
// }
//
// actual fun stackTrace(): List<String> = Exception().toString().split("\n")
//
// actual fun printLog(level: Level, msg: MESSAGE) {
// println("[$level] $KOIN_TAG $msg")
// }
// }
// internal actual fun Any.ensureNeverFrozen() {}
// internal actual fun <R> mpsynchronized(lock: Any, block: () -> R): R = block()

actual object KoinPlatformTools {
actual fun getStackTrace(e: Exception): String = e.toString() + Exception().toString().split("\n")
actual fun getClassName(kClass: KClass<*>): String = kClass.simpleName ?: "KClass@${kClass.hashCode()}"

// TODO Better Id generation?
actual fun generateId(): String = Random.nextDouble().hashCode().toString()
actual fun defaultLazyMode(): LazyThreadSafetyMode = LazyThreadSafetyMode.NONE
actual fun defaultLogger(level: Level): Logger = PrintLogger(level)
actual fun defaultContext(): KoinContext = GlobalContext
actual fun <R> synchronized(lock: Lockable, block: () -> R) = block()
actual fun <K, V> safeHashMap(): MutableMap<K, V> = HashMap()
}

actual typealias Lockable = Any

actual typealias ThreadLocal<T> = ThreadLocalRef<T>
13 changes: 13 additions & 0 deletions projects/core/koin-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand All @@ -14,6 +15,10 @@ kotlin {
browser()
binaries.executable()
}
wasmJs {
binaries.executable()
nodejs()
}

iosX64()
iosArm64()
Expand Down Expand Up @@ -47,5 +52,13 @@ tasks.withType<KotlinCompile>().all {
jvmTarget = "1.8"
}
}
rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "21.0.0-v8-canary202309143a48826a08"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}

apply(from = file("../../gradle/publish.gradle.kts"))
4 changes: 2 additions & 2 deletions projects/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ org.gradle.parallel=true
#Kotlin
kotlin.code.style=official
#Koin
koinVersion=3.6.0-alpha1
koinVersion=3.6.0-wasm1
#Compose
composeCompiler=1.5.7
composeCompiler=1.5.5
#Android
android.useAndroidX=true
androidMinSDK=14
Expand Down
4 changes: 2 additions & 2 deletions projects/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# /!\ Koin in gradle.properties /!\

# Core
kotlin = "1.9.21"
kotlin = "1.9.20"
binaryValidator = "0.13.2"
publish = "2.0.0-rc-1"
coroutines = "1.7.3" # "1.8.0-RC2" for wasm
Expand All @@ -19,7 +19,7 @@ androidx-workmanager = "2.8.1"
androidx-navigation = "2.7.5"
# Compose
# /!\ Compose compiler in gradle.properties /!\
composeJB = "1.5.11"
composeJB = "1.5.10"
composeJetpackRuntime = "1.5.4"
composeJetpackViewmodel = "2.6.2"
# Test
Expand Down

0 comments on commit c9c849e

Please sign in to comment.