Sample, staic context-aware cache
This commit is contained in:
parent
78ec885294
commit
086494bd97
@ -1,4 +1,16 @@
|
||||
[
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.CacheMarkwonSample",
|
||||
"id": "202007189102458",
|
||||
"title": "Cache Markwon instance",
|
||||
"description": "A static cache for `Markwon` instance to be associated with a `Context`",
|
||||
"artifacts": [
|
||||
"CORE"
|
||||
],
|
||||
"tags": [
|
||||
"cache"
|
||||
]
|
||||
},
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.tasklist.TaskListMutateSample",
|
||||
"id": "202007184140901",
|
||||
@ -756,12 +768,13 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.GithubUserIssueOnTextAddedSample",
|
||||
"javaClassName": "io.noties.markwon.app.samples.GithubUserIssueInlineParsingSample",
|
||||
"id": "202006181162024",
|
||||
"title": "User mention and issue (via text)",
|
||||
"description": "Github-like user mention and issue rendering via `CorePlugin.OnTextAddedListener`",
|
||||
"artifacts": [
|
||||
"CORE"
|
||||
"CORE",
|
||||
"INLINE_PARSER"
|
||||
],
|
||||
"tags": [
|
||||
"parsing",
|
||||
@ -770,13 +783,12 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.GithubUserIssueInlineParsingSample",
|
||||
"javaClassName": "io.noties.markwon.app.samples.GithubUserIssueOnTextAddedSample",
|
||||
"id": "202006181162024",
|
||||
"title": "User mention and issue (via text)",
|
||||
"description": "Github-like user mention and issue rendering via `CorePlugin.OnTextAddedListener`",
|
||||
"artifacts": [
|
||||
"CORE",
|
||||
"INLINE_PARSER"
|
||||
"CORE"
|
||||
],
|
||||
"tags": [
|
||||
"parsing",
|
||||
|
@ -33,4 +33,5 @@ object Tags {
|
||||
const val html = "HTML"
|
||||
const val knownBug = "known-bug"
|
||||
const val precomputedText = "precomputed-text"
|
||||
const val cache = "cache"
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package io.noties.markwon.app.samples
|
||||
|
||||
import android.content.Context
|
||||
import io.noties.debug.Debug
|
||||
import io.noties.markwon.Markwon
|
||||
import io.noties.markwon.app.sample.Tags
|
||||
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample
|
||||
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
|
||||
import io.noties.markwon.sample.annotations.MarkwonArtifact
|
||||
import io.noties.markwon.sample.annotations.MarkwonSampleInfo
|
||||
import java.util.Collections
|
||||
import java.util.WeakHashMap
|
||||
|
||||
@MarkwonSampleInfo(
|
||||
id = "202007189102458",
|
||||
title = "Cache Markwon instance",
|
||||
description = "A static cache for `Markwon` instance " +
|
||||
"to be associated with a `Context`",
|
||||
artifacts = [MarkwonArtifact.CORE],
|
||||
tags = [Tags.cache]
|
||||
)
|
||||
class CacheMarkwonSample : MarkwonTextViewSample() {
|
||||
override fun render() {
|
||||
render("# First!")
|
||||
render("## Second!!")
|
||||
render("### Third!!!")
|
||||
}
|
||||
|
||||
fun render(md: String) {
|
||||
val markwon = MarkwonCache.with(context)
|
||||
Debug.i("markwon: ${markwon.hashCode()}, md: '$md'")
|
||||
markwon.setMarkdown(textView, md)
|
||||
}
|
||||
}
|
||||
|
||||
object MarkwonCache {
|
||||
private val cache = Collections.synchronizedMap(WeakHashMap<Context, Markwon>())
|
||||
|
||||
fun with(context: Context): Markwon {
|
||||
// yeah, why work as expected? new value is returned each time, no caching occur
|
||||
// kotlin: 1.3.72
|
||||
// intellij plugin: 1.3.72-release-Studio4.0-5
|
||||
// return cache.getOrPut(context) {
|
||||
// // create your markwon instance here
|
||||
// return Markwon.builder(context)
|
||||
// .usePlugin(StrikethroughPlugin.create())
|
||||
// .build()
|
||||
// }
|
||||
|
||||
return cache[context] ?: {
|
||||
Markwon.builder(context)
|
||||
.usePlugin(StrikethroughPlugin.create())
|
||||
.build()
|
||||
.also {
|
||||
cache[context] = it
|
||||
}
|
||||
}.invoke()
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user