LinkifyPlugin is thread-safe

This commit is contained in:
Dimitry Ivanov 2019-11-08 18:03:46 +03:00
parent 681a7f68d7
commit a6201b1b35
3 changed files with 6 additions and 10 deletions

View File

@ -7,6 +7,7 @@
* Add `SvgPictureMediaDecoder` in `image` module to deal with SVG without dimensions ([#165]) * Add `SvgPictureMediaDecoder` in `image` module to deal with SVG without dimensions ([#165])
* `LinkSpan#getLink` method * `LinkSpan#getLink` method
* `LinkifyPlugin` applies link span that is configured by `Markwon` (obtain via span factory) * `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 [#165]: https://github.com/noties/Markwon/issues/165

View File

@ -58,15 +58,12 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
}); });
} }
// todo: thread safety (builder is reused)
private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener { private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener {
private final int mask; private final int mask;
private final SpannableStringBuilder builder;
LinkifyTextAddedListener(int mask) { LinkifyTextAddedListener(int mask) {
this.mask = mask; this.mask = mask;
this.builder = new SpannableStringBuilder();
} }
@Override @Override
@ -79,12 +76,9 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
return; return;
} }
// clear previous state // @since 4.2.0-SNAPSHOT we no longer re-use builder (thread safety achieved for
builder.clear(); // render calls from different threads and ... better performance)
builder.clearSpans(); final SpannableStringBuilder builder = new SpannableStringBuilder(text);
// append text to process
builder.append(text);
if (Linkify.addLinks(builder, mask)) { if (Linkify.addLinks(builder, mask)) {
// target URL span specifically // target URL span specifically

View File

@ -35,6 +35,7 @@ import io.noties.markwon.editor.MarkwonEditor;
import io.noties.markwon.editor.MarkwonEditorTextWatcher; import io.noties.markwon.editor.MarkwonEditorTextWatcher;
import io.noties.markwon.editor.MarkwonEditorUtils; import io.noties.markwon.editor.MarkwonEditorUtils;
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.sample.R; import io.noties.markwon.sample.R;
public class EditorActivity extends Activity { public class EditorActivity extends Activity {
@ -154,7 +155,7 @@ public class EditorActivity extends Activity {
final Markwon markwon = Markwon.builder(this) final Markwon markwon = Markwon.builder(this)
.usePlugin(StrikethroughPlugin.create()) .usePlugin(StrikethroughPlugin.create())
// .usePlugin(LinkifyPlugin.create()) .usePlugin(LinkifyPlugin.create())
.build(); .build();
final MarkwonTheme theme = markwon.configuration().theme(); final MarkwonTheme theme = markwon.configuration().theme();