Skip to content

Commit

Permalink
Adds wasmJs target (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablichjenkov authored Apr 4, 2024
1 parent 31effba commit 9d92b4c
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Check the [Developers Guide](/component-toolkit/README.md) in the component-tool
**Artifacts are published on Maven Central:**

```kotlin
val commonMain by getting {
dependencies {
sourceSets {
commonMain.dependencies {
// The basic components and plugins API. Use it if you have an
// application architecture already and you just want to use some plugin implementations.
implementation("io.github.pablichjenkov:component-toolkit:0.6.1")
Expand Down
6 changes: 3 additions & 3 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ kotlin {
implementation(project(":macao-sdk-di-koin"))
implementation(project(":macao-sdk-di-manual"))
implementation(compose.material3)
implementation("androidx.activity:activity-compose:1.8.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("androidx.activity:activity-compose:1.8.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions component-toolkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
id("com.android.library")
Expand Down Expand Up @@ -138,17 +140,17 @@ kotlin {
}
}

// JS
// Browser
js(IR) {
browser()
}

// WASM, once kotlin 1.8.20 is out. Although I believe this should go in the jsApp module not
// in the library. Perhaps can go here without the binaries.executable() statement
/*wasm {
binaries.executable()
browser {}
}*/
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "ComponentToolkitKt"
browser()
binaries.library()
}

// JVM
jvm()
Expand All @@ -161,7 +163,7 @@ kotlin {
implementation(compose.ui)
implementation(compose.material3)
implementation(compose.animation)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
// implementation("org.jetbrains.compose.ui:ui-util:1.5.10")
// implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.macaosoftware.component

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.macaosoftware.component.core.Component
import com.macaosoftware.component.core.deeplink.LocalRootComponentProvider
import com.macaosoftware.plugin.Lifecycle
import com.macaosoftware.plugin.LifecycleEventObserver

@Composable
fun BrowserComponentRender(
rootComponent: Component
) {

val lifecycle = remember(rootComponent) { Lifecycle() }

CompositionLocalProvider(
LocalRootComponentProvider provides rootComponent
) {
rootComponent.Content(Modifier.fillMaxSize())
/*Box(modifier = Modifier.fillMaxSize()) {
// Should listen for keyboard back instead
FloatingBackButton(
modifier = Modifier.offset(y = 48.dp),
alignment = Alignment.TopStart,
onClick = { webBackPressDispatcher.dispatchBackPressed() }
)
}*/
}

LifecycleEventObserver(
lifecycle = lifecycle,
onStart = {
println("Receiving Js.onStart() event")
rootComponent.dispatchActive()
},
onStop = {
println("Receiving Js.onStop() event")
rootComponent.dispatchInactive()
},
initializeBlock = {
rootComponent.dispatchAttach()
}
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.macaosoftware.plugin

class Lifecycle {

private val platformLifecyclePlugin = DefaultPlatformLifecyclePlugin()

init {
startObserving()
}

private fun startObserving() {
started()
}

fun subscribe(appLifecycleCallback: AppLifecycleCallback) {
platformLifecyclePlugin.subscribe(appLifecycleCallback)
}

private fun started() {
platformLifecyclePlugin.dispatchAppLifecycleEvent(AppLifecycleEvent.Start)
}

private fun stopped() {
platformLifecyclePlugin.dispatchAppLifecycleEvent(AppLifecycleEvent.Stop)
}

fun unsubscribe(appLifecycleCallback: AppLifecycleCallback) {
platformLifecyclePlugin.unsubscribe(appLifecycleCallback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.macaosoftware.plugin

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState

@Composable
fun LifecycleEventObserver(
lifecycle: Lifecycle,
onStart: () -> Unit,
onStop: () -> Unit,
initializeBlock: () -> Unit
) {
// Safely update the current lambdas when a new one is provided
val currentOnStart by rememberUpdatedState(newValue = onStart)
val currentOnStop by rememberUpdatedState(newValue = onStop)

// If the enclosing lifecycleOwner changes, dispose and reset the effect
DisposableEffect(key1 = initializeBlock, key2 = lifecycle) {
initializeBlock.invoke()
// Create an observer that triggers our remembered callbacks
// when the LifecycleOwner that contains this composable changes its state.
val observer = object : AppLifecycleCallback {
override fun onEvent(appLifecycleEvent: AppLifecycleEvent) {
if (appLifecycleEvent == AppLifecycleEvent.Start) {
currentOnStart()
} else if (appLifecycleEvent == AppLifecycleEvent.Stop) {
currentOnStop()
}
}
}

// Add the observer to the lifecycle
lifecycle.subscribe(observer)

// When the effect leaves the Composition, remove the observer
onDispose {
lifecycle.unsubscribe(observer)
println(
"LifecycleEventObserver::Disposing LifecycleEventObserver"
)
}
}

}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2

# Compose
org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true
org.jetbrains.compose.experimental.wasm.enabled=true

# Android
agp.version=8.3.1
Expand All @@ -30,4 +30,4 @@ kotlin.version=1.9.23
compose.version=1.6.1

# component-toolkit version
component-toolkit.version=0.6.1
component-toolkit.version=0.6.2
4 changes: 2 additions & 2 deletions jsApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ kotlin {
implementation(project(":shared"))
implementation(project(":macao-sdk-di-koin"))
implementation(project(":macao-sdk-di-manual"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.8.0")
}
}
}
4 changes: 2 additions & 2 deletions jvmApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ kotlin {
implementation(compose.desktop.common)
implementation(compose.desktop.currentOs)
implementation(compose.material3)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.0")
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions macao-sdk-di-koin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
id("com.android.library")
Expand Down Expand Up @@ -111,32 +113,40 @@ kotlin {
browser()
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "MacaoSdkKoin"
browser()
binaries.library()
}

jvm()

sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.ui)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")

// Macao Libs
api(project(":component-toolkit"))

// Koin
api("io.insert-koin:koin-core:3.5.3")
// api("io.insert-koin:koin-core:3.5.3")
api("io.insert-koin:koin-core:3.6.0-wasm-alpha2")
}
commonTest.dependencies {
// implementation(libs.kotlin.test)
}
androidMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")
}
jvmMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.0")
}
jsMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.8.0")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.macaosoftware.app

import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import com.macaosoftware.component.BrowserComponentRender

@Composable
fun MacaoKoinApplication(
applicationState: MacaoKoinApplicationState
) {

when (val stage = applicationState.stage.value) {

KoinAppStage.Created -> {
SideEffect { applicationState.start() }
}

KoinAppStage.Loading -> {
}

is KoinAppStage.Started -> {
BrowserComponentRender(rootComponent = stage.rootComponent)
}
}
}
17 changes: 13 additions & 4 deletions macao-sdk-di-manual/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
id("com.android.library")
Expand Down Expand Up @@ -25,14 +27,21 @@ kotlin {
browser()
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "MacaoSdkKoin"
browser()
binaries.library()
}

jvm()

sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.ui)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")

// Macao Libs
api(project(":component-toolkit"))
Expand All @@ -41,13 +50,13 @@ kotlin {
// implementation(libs.kotlin.test)
}
androidMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")
}
jvmMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.0")
}
jsMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.8.0")
}
}
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include(":shared")
include(":androidApp")
include(":jsApp")
include(":jvmApp")
include(":wasmApp")

pluginManagement {
repositories {
Expand Down
11 changes: 10 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
Expand Down Expand Up @@ -34,6 +36,13 @@ kotlin {
browser()
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "shared"
browser()
binaries.library()
}

// JVM
jvm()

Expand All @@ -43,7 +52,7 @@ kotlin {
implementation(compose.ui)
implementation(compose.foundation)
implementation(compose.material3)
implementation("org.jetbrains.compose.components:components-resources:1.5.11")
implementation("org.jetbrains.compose.components:components-resources:1.6.1")

// Macao Libs
api(project(":macao-sdk-di-koin"))
Expand Down
Loading

0 comments on commit 9d92b4c

Please sign in to comment.