Skip to content

Commit

Permalink
Merge pull request #1193 from jingwei99/jingwei.ziplineLoader
Browse files Browse the repository at this point in the history
Add FreshnessChecker into zipline loading process
  • Loading branch information
jingwei99 authored Dec 16, 2023
2 parents 2635be5 + 86f0339 commit 3742088
Show file tree
Hide file tree
Showing 14 changed files with 468 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package app.cash.zipline.samples.trivia

import app.cash.zipline.Zipline
import app.cash.zipline.loader.DefaultFreshnessCheckerNotFresh
import app.cash.zipline.loader.LoadResult
import app.cash.zipline.loader.ManifestVerifier.Companion.NO_SIGNATURE_CHECKS
import app.cash.zipline.loader.ZiplineLoader
import kotlinx.coroutines.CoroutineDispatcher
import okhttp3.OkHttpClient

fun getTriviaService(zipline: Zipline): TriviaService {
return zipline.take("triviaService")
}
Expand All @@ -33,7 +33,7 @@ suspend fun launchZipline(dispatcher: CoroutineDispatcher): Zipline {
NO_SIGNATURE_CHECKS,
OkHttpClient(),
)
return when (val result = loader.loadOnce("trivia", manifestUrl)) {
return when (val result = loader.loadOnce("trivia", DefaultFreshnessCheckerNotFresh, manifestUrl)) {
is LoadResult.Success -> result.zipline
is LoadResult.Failure -> error(result.exception)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package app.cash.zipline.samples.worldclock

import app.cash.zipline.Zipline
import app.cash.zipline.loader.DefaultFreshnessCheckerNotFresh
import app.cash.zipline.loader.LoadResult
import app.cash.zipline.loader.ZiplineLoader
import kotlinx.coroutines.CoroutineDispatcher
Expand All @@ -41,6 +42,7 @@ fun startWorldClockZipline(
scope.launch(ziplineDispatcher + SupervisorJob()) {
val loadResultFlow: Flow<LoadResult> = ziplineLoader.load(
applicationName = "world-clock",
freshnessChecker = DefaultFreshnessCheckerNotFresh,
manifestUrlFlow = repeatFlow(manifestUrl, 500L),
initializer = { zipline: Zipline ->
zipline.bind("WorldClockHost", host)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package app.cash.zipline.tests

import app.cash.zipline.Zipline
import app.cash.zipline.loader.DefaultFreshnessCheckerNotFresh
import app.cash.zipline.loader.LoadResult
import app.cash.zipline.loader.ManifestVerifier.Companion.NO_SIGNATURE_CHECKS
import app.cash.zipline.loader.ZiplineHttpClient
Expand Down Expand Up @@ -47,7 +48,7 @@ suspend fun launchZipline(dispatcher: CoroutineDispatcher): Zipline {
httpClient = localDirectoryHttpClient,
)

val result = loader.loadOnce("test", "https://localhost/manifest.zipline.json")
val result = loader.loadOnce("test", DefaultFreshnessCheckerNotFresh, "https://localhost/manifest.zipline.json")
return when (result) {
is LoadResult.Success -> result.zipline
is LoadResult.Failure -> throw result.exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package app.cash.zipline.tests

import app.cash.zipline.Zipline
import app.cash.zipline.ZiplineException
import app.cash.zipline.loader.DefaultFreshnessCheckerNotFresh
import app.cash.zipline.loader.LoadResult
import app.cash.zipline.loader.ManifestVerifier.Companion.NO_SIGNATURE_CHECKS
import app.cash.zipline.loader.ZiplineHttpClient
Expand Down Expand Up @@ -56,6 +57,7 @@ suspend fun launchZipline(

val loadResult = loader.loadOnce(
"test",
DefaultFreshnessCheckerNotFresh,
"https://localhost/manifest.zipline.json",
) as LoadResult.Success

Expand Down
13 changes: 13 additions & 0 deletions zipline-loader/api/android/zipline-loader.api
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
public final class app/cash/zipline/loader/DefaultFreshnessCheckerNotFresh : app/cash/zipline/loader/FreshnessChecker {
public static final field INSTANCE Lapp/cash/zipline/loader/DefaultFreshnessCheckerNotFresh;
public fun isFresh (Lapp/cash/zipline/ZiplineManifest;J)Z
}

public final class app/cash/zipline/loader/FastCodeUpdatesKt {
public static final fun schemeAndAuthority (Ljava/lang/String;)Ljava/lang/String;
public static final fun withDevelopmentServerPush-SxA4cEA (Lkotlinx/coroutines/flow/Flow;Lapp/cash/zipline/loader/ZiplineHttpClient;J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun withDevelopmentServerPush-SxA4cEA$default (Lkotlinx/coroutines/flow/Flow;Lapp/cash/zipline/loader/ZiplineHttpClient;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
}

public abstract interface class app/cash/zipline/loader/FreshnessChecker {
public abstract fun isFresh (Lapp/cash/zipline/ZiplineManifest;J)Z
}

public abstract class app/cash/zipline/loader/LoadResult {
}

Expand Down Expand Up @@ -122,9 +131,13 @@ public final class app/cash/zipline/loader/ZiplineLoader {
public synthetic fun <init> (Lkotlinx/coroutines/CoroutineDispatcher;Lapp/cash/zipline/loader/ManifestVerifier;Lapp/cash/zipline/loader/ZiplineHttpClient;Lapp/cash/zipline/EventListener;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun download (Ljava/lang/String;Lokio/Path;Lokio/FileSystem;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getConcurrentDownloads ()I
public final fun load (Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
public final fun load (Ljava/lang/String;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun load$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun load$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun loadOnce (Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun loadOnce (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun loadOnce$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun loadOnce$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun setConcurrentDownloads (I)V
public final fun withCache (Lapp/cash/zipline/loader/ZiplineCache;)Lapp/cash/zipline/loader/ZiplineLoader;
Expand Down
13 changes: 13 additions & 0 deletions zipline-loader/api/jvm/zipline-loader.api
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
public final class app/cash/zipline/loader/DefaultFreshnessCheckerNotFresh : app/cash/zipline/loader/FreshnessChecker {
public static final field INSTANCE Lapp/cash/zipline/loader/DefaultFreshnessCheckerNotFresh;
public fun isFresh (Lapp/cash/zipline/ZiplineManifest;J)Z
}

public final class app/cash/zipline/loader/FastCodeUpdatesKt {
public static final fun schemeAndAuthority (Ljava/lang/String;)Ljava/lang/String;
public static final fun withDevelopmentServerPush-SxA4cEA (Lkotlinx/coroutines/flow/Flow;Lapp/cash/zipline/loader/ZiplineHttpClient;J)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun withDevelopmentServerPush-SxA4cEA$default (Lkotlinx/coroutines/flow/Flow;Lapp/cash/zipline/loader/ZiplineHttpClient;JILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
}

public abstract interface class app/cash/zipline/loader/FreshnessChecker {
public abstract fun isFresh (Lapp/cash/zipline/ZiplineManifest;J)Z
}

public abstract class app/cash/zipline/loader/LoadResult {
}

Expand Down Expand Up @@ -122,9 +131,13 @@ public final class app/cash/zipline/loader/ZiplineLoader {
public synthetic fun <init> (Lkotlinx/coroutines/CoroutineDispatcher;Lapp/cash/zipline/loader/ManifestVerifier;Lapp/cash/zipline/loader/ZiplineHttpClient;Lapp/cash/zipline/EventListener;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun download (Ljava/lang/String;Lokio/Path;Lokio/FileSystem;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getConcurrentDownloads ()I
public final fun load (Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
public final fun load (Ljava/lang/String;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun load$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun load$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lkotlinx/coroutines/flow/Flow;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public final fun loadOnce (Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun loadOnce (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun loadOnce$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Lapp/cash/zipline/loader/FreshnessChecker;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun loadOnce$default (Lapp/cash/zipline/loader/ZiplineLoader;Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/modules/SerializersModule;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun setConcurrentDownloads (I)V
public final fun withCache (Lapp/cash/zipline/loader/ZiplineCache;)Lapp/cash/zipline/loader/ZiplineLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 Cash App
*
* 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 app.cash.zipline.loader

import app.cash.zipline.ZiplineManifest

/**
* Checks if a given ZiplineManifest is considered fresh.
*
* For local development that requires continuous loading (or hot loading), supply a
* FreshnessChecker that always returns false.
*/
interface FreshnessChecker {

/**
* Decides whether the [manifest] is eligible to be used.
*
* Returns true to launch the [manifest] immediately; false to download a fresh
* ZiplineManifest and launch that.
*/
fun isFresh(
manifest: ZiplineManifest,
freshAtEpochMs: Long,
): Boolean
}

/** A FreshnessChecker that always returns true. */
object DefaultFreshnessCheckerNotFresh : FreshnessChecker {
override fun isFresh(manifest: ZiplineManifest, freshAtEpochMs: Long): Boolean {
return false
}
}
Loading

0 comments on commit 3742088

Please sign in to comment.