Cache the Links in a note

This requires the markdown to be parsed which can be an incredibly slow
operation, so lets avoid re-doing it unless necessary.
This commit is contained in:
Vishesh Handa
2020-05-26 17:08:03 +02:00
parent 36f31e06bb
commit 4eeff093c1

View File

@ -49,7 +49,9 @@ class Note with NotesNotifier {
var _loadState = NoteLoadState.None; var _loadState = NoteLoadState.None;
var _serializer = MarkdownYAMLCodec(); var _serializer = MarkdownYAMLCodec();
// Computed from body
String _summary; String _summary;
List<Link> _links;
static final _mdYamlDocLoader = MdYamlDocLoader(); static final _mdYamlDocLoader = MdYamlDocLoader();
@ -109,6 +111,7 @@ class Note with NotesNotifier {
set body(String newBody) { set body(String newBody) {
_body = newBody; _body = newBody;
_summary = null; _summary = null;
_links = null;
_notifyModified(); _notifyModified();
} }
@ -371,6 +374,10 @@ class Note with NotesNotifier {
} }
Future<List<Link>> fetchLinks() async { Future<List<Link>> fetchLinks() async {
if (_links != null) {
return _links;
}
final doc = md.Document( final doc = md.Document(
encodeHtml: false, encodeHtml: false,
extensionSet: md.ExtensionSet.gitHubFlavored, extensionSet: md.ExtensionSet.gitHubFlavored,
@ -392,11 +399,11 @@ class Note with NotesNotifier {
} }
doc.linkReferences.forEach((key, value) { doc.linkReferences.forEach((key, value) {
print(value);
var filePath = value.destination; var filePath = value.destination;
links.add(Link(term: key, filePath: filePath)); links.add(Link(term: key, filePath: filePath));
}); });
_links = links;
return links; return links;
} }
} }