Skip to content

Commit

Permalink
Mitigate stork memory leak (srid#412)
Browse files Browse the repository at this point in the history
* Delay stork index refreshing until actually used

* Reduce logging
  • Loading branch information
srid authored Jan 28, 2023
1 parent 9e90a23 commit 01b54dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- Add option to include YAML frontmatter in the Stork index [\#398](https://github.com/srid/emanote/pull/398)
- Features
- Timeline backlinks recognize flexible daily notes suffixed with arbitrary string [\#395](https://github.com/srid/emanote/issues/395)
- Performance
- Address client-side memory leak due to Stork search in live server [\#411](https://github.com/srid/emanote/issues/411#issuecomment-1402056235)
- Misc
- Ignore toplevel `flake.{nix,lock}` by default.
- Remove deprecated `_emanote-bin/compile-css` script
Expand Down
51 changes: 43 additions & 8 deletions default/templates/components/stork/stork-search-head.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
<script src="${ema:emanoteStaticLayerUrl}/stork/stork.js"></script>
<ema:metadata>
<with var="template">
<script data-emanote-base-url="${value:baseUrl}">
<script id="emanote-stork" data-emanote-base-url="${value:baseUrl}">
window.emanote = {};
window.emanote.stork = {
searchShown: false,
indexIsStale: false,
toggleSearch: function () {
window.emanote.stork.refreshIndex();
document.getElementById('stork-search-container').classList.toggle('hidden');
window.emanote.stork.searchShown = document.body.classList.toggle('stork-overflow-hidden-important');
if (window.emanote.stork.searchShown) {
Expand All @@ -32,14 +34,25 @@
window.emanote.stork.searchShown = false;
},

init: function () {
getBaseUrl: function () {
const baseUrl = document.getElementById("emanote-stork").getAttribute('data-emanote-base-url') || '/';
return baseUrl;
},

registerIndex: function (options) {
const indexName = 'emanote-search'; // used to match input[data-stork] attribute value
const baseUrl = document.currentScript.getAttribute('data-emanote-base-url') || '/';
const indexUrl = baseUrl + '-/stork.st';
const indexUrl = window.emanote.stork.getBaseUrl() + '-/stork.st';
stork.register(
indexName,
indexUrl,
options);
},

init: function () {
if (document.readyState !== 'complete') {
window.addEventListener('load', function () {
stork.initialize(baseUrl + '_emanote-static/stork/stork.wasm');
stork.register(indexName, indexUrl);
stork.initialize(window.emanote.stork.getBaseUrl() + '_emanote-static/stork/stork.wasm');
window.emanote.stork.registerIndex();
});

document.addEventListener('keydown', event => {
Expand All @@ -52,10 +65,32 @@
}
});
} else {
// Override existing on Ema's hot-reload
stork.register(indexName, indexUrl, { forceOverwrite: true });
// This section is called during Ema's hot reload.
//
// Mark the current index as stale, and refresh it *only when* the
// user actually invokes search.
//
// We do not refresh the index *right away*, as that will cause
// memory leaks in the browser. See
// https://github.com/srid/emanote/issues/411#issuecomment-1402056235
console.log("stork: Marking index as stale");
window.emanote.stork.markIndexAsStale();
}
},

markIndexAsStale: function () {
window.emanote.stork.indexIsStale = true;
},

refreshIndex: function () {
if (window.emanote.stork.indexIsStale) {
console.log("stork: Reloading index");
window.emanote.stork.indexIsStale = false;
// NOTE: This will leak memory. See the comment above.
window.emanote.stork.registerIndex({ forceOverwrite: true });
}
}

};

window.emanote.stork.init();
Expand Down
2 changes: 1 addition & 1 deletion emanote.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: emanote
version: 1.0.1.7
version: 1.0.1.8
license: AGPL-3.0-only
copyright: 2022 Sridhar Ratnakumar
maintainer: [email protected]
Expand Down

0 comments on commit 01b54dd

Please sign in to comment.