Skip to content

Commit

Permalink
Add JS/WASM support to lifecycle-viewmodel
Browse files Browse the repository at this point in the history
Change-Id: Ied71ef44e96a84e4c3b2962aad97cc314db9984d
  • Loading branch information
MatkovIvan committed Mar 5, 2024
1 parent 1fcac13 commit eab124c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lifecycle/lifecycle-viewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@
*/

import androidx.build.KmpPlatformsKt
import androidx.build.KotlinTarget
import androidx.build.PlatformIdentifier
import androidx.build.Publish
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.konan.target.Family

plugins {
id("AndroidXPlugin")
id("com.android.library")
}

def jsEnabled = KmpPlatformsKt.enableJs(project)
def wasmEnabled = KmpPlatformsKt.enableWasm(project)

androidXMultiplatform {
android()
desktop()
mac()
linux()
ios()
js()
wasm()

kotlin {
explicitApi = ExplicitApiMode.Strict
Expand Down Expand Up @@ -104,6 +111,10 @@ androidXMultiplatform {
darwinMain.dependsOn(nativeMain)
linuxMain.dependsOn(nativeMain)

if (jsEnabled || wasmEnabled) {
webMain.dependsOn(nonJvmMain)
}

targets.all { target ->
if (target.platformType == KotlinPlatformType.native) {
target.compilations["main"].defaultSourceSet {
Expand All @@ -116,6 +127,13 @@ androidXMultiplatform {
throw new GradleException("unknown native target ${target}")
}
}
} else if (target.platformType in [
KotlinPlatformType.js,
KotlinPlatformType.wasm
]) {
target.compilations["main"].defaultSourceSet {
dependsOn(webMain)
}
}
}
}
Expand All @@ -131,6 +149,7 @@ android {
androidx {
name = "Lifecycle ViewModel"
publish = Publish.SNAPSHOT_AND_RELEASE
kotlinTarget = KotlinTarget.KOTLIN_1_9
inceptionYear = "2017"
description = "Android Lifecycle ViewModel"
metalavaK2UastEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package androidx.lifecycle.viewmodel.internal
* The implementation depends on the platform:
* - On JVM/ART: uses JDK's synchronization.
* - On Native: uses posix.
* - On JS/WASM: No-op as it's single thread environment.
*/
internal expect class Lock() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024 The Android Open Source Project
*
* 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 androidx.lifecycle.viewmodel.internal

internal actual class Lock actual constructor() {
actual inline fun <T> withLock(crossinline block: () -> T): T = block()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 The Android Open Source Project
*
* 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 androidx.lifecycle.viewmodel.internal

import kotlin.reflect.KClass

internal actual val <T : Any> KClass<T>.canonicalName: String?
get() = simpleName // `qualifiedName` reflection API is not supported yet in JavaScript

0 comments on commit eab124c

Please sign in to comment.