From a6201b1b350349021bcf2adf55d641dd2c4fdbd0 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Fri, 8 Nov 2019 18:03:46 +0300 Subject: [PATCH] LinkifyPlugin is thread-safe --- CHANGELOG.md | 1 + .../io/noties/markwon/linkify/LinkifyPlugin.java | 12 +++--------- .../noties/markwon/sample/editor/EditorActivity.java | 3 ++- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9daad6c6..ee18c59a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Add `SvgPictureMediaDecoder` in `image` module to deal with SVG without dimensions ([#165]) * `LinkSpan#getLink` method * `LinkifyPlugin` applies link span that is configured by `Markwon` (obtain via span factory) +* `LinkifyPlugin` is thread-safe [#165]: https://github.com/noties/Markwon/issues/165 diff --git a/markwon-linkify/src/main/java/io/noties/markwon/linkify/LinkifyPlugin.java b/markwon-linkify/src/main/java/io/noties/markwon/linkify/LinkifyPlugin.java index 3af48dcd..2bbef877 100644 --- a/markwon-linkify/src/main/java/io/noties/markwon/linkify/LinkifyPlugin.java +++ b/markwon-linkify/src/main/java/io/noties/markwon/linkify/LinkifyPlugin.java @@ -58,15 +58,12 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin { }); } - // todo: thread safety (builder is reused) private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener { private final int mask; - private final SpannableStringBuilder builder; LinkifyTextAddedListener(int mask) { this.mask = mask; - this.builder = new SpannableStringBuilder(); } @Override @@ -79,12 +76,9 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin { return; } - // clear previous state - builder.clear(); - builder.clearSpans(); - - // append text to process - builder.append(text); + // @since 4.2.0-SNAPSHOT we no longer re-use builder (thread safety achieved for + // render calls from different threads and ... better performance) + final SpannableStringBuilder builder = new SpannableStringBuilder(text); if (Linkify.addLinks(builder, mask)) { // target URL span specifically diff --git a/sample/src/main/java/io/noties/markwon/sample/editor/EditorActivity.java b/sample/src/main/java/io/noties/markwon/sample/editor/EditorActivity.java index c09ada4b..14d8a802 100644 --- a/sample/src/main/java/io/noties/markwon/sample/editor/EditorActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/editor/EditorActivity.java @@ -35,6 +35,7 @@ import io.noties.markwon.editor.MarkwonEditor; import io.noties.markwon.editor.MarkwonEditorTextWatcher; import io.noties.markwon.editor.MarkwonEditorUtils; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.sample.R; public class EditorActivity extends Activity { @@ -154,7 +155,7 @@ public class EditorActivity extends Activity { final Markwon markwon = Markwon.builder(this) .usePlugin(StrikethroughPlugin.create()) -// .usePlugin(LinkifyPlugin.create()) + .usePlugin(LinkifyPlugin.create()) .build(); final MarkwonTheme theme = markwon.configuration().theme();