Skip to content

Commit 81ae150

Browse files
committed
Cache OneNote hierarchy
Brings the performance improvement from notebook explorer to recent pages and title search
1 parent 7e816d9 commit 81ae150

8 files changed

Lines changed: 64 additions & 93 deletions

File tree

Flow.Launcher.Plugin.OneNote/Main.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Flow.Launcher.Plugin.OneNote.Search;
88
using Flow.Launcher.Plugin.OneNote.UI.Views;
99
using OneNoteApp = LinqToOneNote.OneNote;
10+
1011
namespace Flow.Launcher.Plugin.OneNote
1112
{
1213
#nullable disable
@@ -18,7 +19,6 @@ public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IDisposable
1819
private SearchManager searchManager;
1920
private Settings settings;
2021
private IconProvider iconProvider;
21-
private VisibilityChanged visibilityChanged;
2222

2323
private static SemaphoreSlim semaphore;
2424

@@ -28,17 +28,18 @@ public Task InitAsync(PluginInitContext context)
2828
this.context = context;
2929
settings = context.API.LoadSettingJsonStorage<Settings>();
3030

31-
visibilityChanged = new VisibilityChanged(context);
3231
iconProvider = new IconProvider(context, settings);
3332
resultCreator = new ResultCreator(context, settings, iconProvider);
34-
searchManager = new SearchManager(context, settings, resultCreator, visibilityChanged);
33+
searchManager = new SearchManager(context, settings, resultCreator);
3534
semaphore = new SemaphoreSlim(1, 1);
3635

37-
visibilityChanged.Subscribe(static (isVisible) =>
36+
context.API.VisibilityChanged += (_, args) =>
3837
{
39-
if (!isVisible)
40-
Task.Run(OneNoteApp.ReleaseComObject);
41-
});
38+
if (args.IsVisible)
39+
return;
40+
Task.Run(OneNoteApp.ReleaseComObject);
41+
searchManager.RootCache.SetDirty();
42+
};
4243
return Task.CompletedTask;
4344
}
4445

@@ -84,7 +85,6 @@ public Control CreateSettingPanel()
8485

8586
public void Dispose()
8687
{
87-
visibilityChanged.Dispose();
8888
semaphore.Dispose();
8989
OneNoteApp.ReleaseComObject();
9090
}

Flow.Launcher.Plugin.OneNote/Search/NotebookExplorer.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,11 @@
55
using LinqToOneNote.Abstractions;
66
using OneNoteApp = LinqToOneNote.OneNote;
77

8-
98
namespace Flow.Launcher.Plugin.OneNote.Search
109
{
11-
public class NotebookExplorer : SearchBase
10+
public class NotebookExplorer(PluginInitContext context, Settings settings, ResultCreator resultCreator, TitleSearch titleSearch, RootCache rootCache)
11+
: SearchBase(context, settings, resultCreator, settings.Keywords.NotebookExplorer)
1212
{
13-
private readonly TitleSearch titleSearch;
14-
private Root? cache;
15-
private bool updateCache;
16-
public NotebookExplorer(PluginInitContext context, Settings settings, ResultCreator resultCreator, TitleSearch titleSearch, VisibilityChanged visibilityChanged)
17-
: base(context, settings, resultCreator, settings.Keywords.NotebookExplorer)
18-
{
19-
this.titleSearch = titleSearch;
20-
visibilityChanged.Subscribe(isVisible =>
21-
{
22-
if (!isVisible)
23-
{
24-
updateCache = true;
25-
}
26-
});
27-
}
28-
2913
public override List<Result> GetResults(Query query)
3014
{
3115
if (!ValidateSearch(query, out string? search, out IOneNoteItem? parent, out IEnumerable<IOneNoteItem> collection))
@@ -53,13 +37,8 @@ private bool ValidateSearch(Query query, out string? lastSearch, out IOneNoteIte
5337
{
5438
lastSearch = null;
5539
parent = null;
56-
if (updateCache || query.IsReQuery || cache == null)
57-
{
58-
cache = OneNoteApp.GetFullHierarchy();
59-
updateCache = false;
60-
}
6140

62-
collection = cache.Notebooks;
41+
collection = rootCache.Root.Notebooks;
6342

6443
string search = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];
6544
const string separator = Keywords.NotebookExplorerSeparator;
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using LinqToOneNote;
4-
using OneNoteApp = LinqToOneNote.OneNote;
54

65
namespace Flow.Launcher.Plugin.OneNote.Search
76
{
8-
public class RecentPages(PluginInitContext context, Settings settings, ResultCreator resultCreator)
7+
public class RecentPages(PluginInitContext context, Settings settings, ResultCreator resultCreator, RootCache rootCache)
98
: SearchBase(context, settings, resultCreator, settings.Keywords.RecentPages)
109
{
1110
public override List<Result> GetResults(Query query)
@@ -15,14 +14,14 @@ public override List<Result> GetResults(Query query)
1514
if (search.Length > keyword.Length && int.TryParse(search[keyword.Length..], out int userChosenCount))
1615
count = userChosenCount;
1716

18-
return OneNoteApp.GetFullHierarchy()
19-
.Notebooks
20-
.GetAllPages()
21-
.FilterBySettings(settings)
22-
.OrderByDescending(pg => pg.LastModified)
23-
.Take(count)
24-
.Select(resultCreator.CreateRecentPageResult)
25-
.ToList();
17+
return rootCache.Root
18+
.Notebooks
19+
.GetAllPages()
20+
.FilterBySettings(settings)
21+
.OrderByDescending(pg => pg.LastModified)
22+
.Take(count)
23+
.Select(resultCreator.CreateRecentPageResult)
24+
.ToList();
2625
}
2726
}
2827
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using LinqToOneNote;
2+
3+
namespace Flow.Launcher.Plugin.OneNote.Search
4+
{
5+
public class RootCache
6+
{
7+
private bool isDirty;
8+
private Root? root;
9+
public Root Root
10+
{
11+
get
12+
{
13+
if (!isDirty && root is not null)
14+
return root;
15+
root = LinqToOneNote.OneNote.GetFullHierarchy();
16+
isDirty = false;
17+
return root;
18+
}
19+
}
20+
public void SetDirty()
21+
{
22+
isDirty = true;
23+
}
24+
}
25+
}

Flow.Launcher.Plugin.OneNote/Search/SearchBase.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
namespace Flow.Launcher.Plugin.OneNote.Search
44
{
5-
public abstract class SearchBase
5+
public abstract class SearchBase(PluginInitContext context, Settings settings, ResultCreator resultCreator, Keyword? keyword)
66
{
7-
protected readonly PluginInitContext context;
8-
protected readonly Settings settings;
9-
protected readonly ResultCreator resultCreator;
7+
protected readonly PluginInitContext context = context;
8+
protected readonly Settings settings = settings;
9+
protected readonly ResultCreator resultCreator = resultCreator;
1010
#nullable disable
11-
public readonly Keyword keyword;
11+
public readonly Keyword keyword = keyword;
1212
#nullable restore
13-
protected SearchBase(PluginInitContext context, Settings settings, ResultCreator resultCreator, Keyword? keyword)
14-
{
15-
this.context = context;
16-
this.settings = settings;
17-
this.resultCreator = resultCreator;
18-
this.keyword = keyword;
19-
}
2013
protected Keywords Keywords => settings.Keywords;
2114
public abstract List<Result> GetResults(Query query);
15+
2216
}
2317
}

Flow.Launcher.Plugin.OneNote/Search/SearchManager.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ public class SearchManager
88
private readonly NotebookExplorer notebookExplorer;
99
private readonly DefaultSearch defaultSearch;
1010
private readonly RecentPages recentPages;
11+
private readonly RootCache rootCache;
12+
public RootCache RootCache => rootCache;
1113

12-
public SearchManager(PluginInitContext context, Settings settings, ResultCreator resultCreator, VisibilityChanged visibilityChanged)
14+
public SearchManager(PluginInitContext context, Settings settings, ResultCreator resultCreator)
1315
{
14-
titleSearch = new TitleSearch(context, settings, resultCreator);
15-
notebookExplorer = new NotebookExplorer(context, settings, resultCreator, titleSearch, visibilityChanged);
16-
recentPages = new RecentPages(context, settings, resultCreator);
16+
rootCache = new RootCache();
17+
titleSearch = new TitleSearch(context, settings, resultCreator, rootCache);
18+
notebookExplorer = new NotebookExplorer(context, settings, resultCreator, titleSearch, rootCache);
19+
recentPages = new RecentPages(context, settings, resultCreator, rootCache);
1720
defaultSearch = new DefaultSearch(context, settings, resultCreator);
1821
}
1922

2023
public List<Result> Query(Query query)
2124
{
25+
if (query.IsReQuery)
26+
{
27+
rootCache.SetDirty();
28+
}
2229
string search = query.Search;
2330
return search switch
2431
{

Flow.Launcher.Plugin.OneNote/Search/TitleSearch.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using LinqToOneNote;
4-
using OneNoteApp = LinqToOneNote.OneNote;
54

65
namespace Flow.Launcher.Plugin.OneNote.Search
76
{
8-
public class TitleSearch(PluginInitContext context, Settings settings, ResultCreator resultCreator)
7+
public class TitleSearch(PluginInitContext context, Settings settings, ResultCreator resultCreator, RootCache rootCache)
98
: SearchBase(context, settings, resultCreator, settings.Keywords.TitleSearch)
109
{
11-
public override List<Result> GetResults(Query query) => Filter(query.Search, null, OneNoteApp.GetFullHierarchy().Notebooks);
10+
public override List<Result> GetResults(Query query) => Filter(query.Search, null, rootCache.Root.Notebooks);
1211

1312
public List<Result> Filter(string query, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
1413
{

Flow.Launcher.Plugin.OneNote/VisibilityChanged.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)