Skip to content

Rework throttled refresh #8677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions core/modules/startup/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ exports.startup = function() {
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
document.head.insertBefore($tw.styleElement,document.head.firstChild);
// Prepare refresh mechanism
var deferredChanges = Object.create(null),
timerId,
throttledRefreshFn = function(changes,callback,mainCondition,styleCondition) {
var mainDeferredChanges = Object.create(null),
styleDeferredChanges = Object.create(null),
mainTimerId,
styleTimerId,
throttledRefreshFn = function(changes,deferredChanges,timerId,throttledRefresh,callback,mainCondition,styleCondition) {
// Check if only tiddlers that are throttled have changed
var onlyThrottledTiddlersHaveChanged = true;
for(var title in changes) {
Expand All @@ -76,20 +78,30 @@ exports.startup = function() {
$tw.utils.extend(deferredChanges,changes);
} else {
$tw.utils.extend(deferredChanges,changes);
callback(changes);
callback();
}
};
function styleRefresh(changes) {
if($tw.styleWidgetNode.refresh(changes,$tw.styleContainer,null)) {
function refresh() {
// Process the refresh
$tw.hooks.invokeHook("th-page-refreshing");
$tw.pageWidgetNode.refresh(mainDeferredChanges);
mainDeferredChanges = Object.create(null);
$tw.hooks.invokeHook("th-page-refreshed");
}
function styleRefresh() {
if($tw.styleWidgetNode.refresh(styleDeferredChanges,$tw.styleContainer,null)) {
var newStyles = $tw.styleContainer.textContent;
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
$tw.styleWidgetNode.assignedStyles = newStyles;
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
}
}
}
styleDeferredChanges = Object.create(null);
}
var mainThrottledRefresh = $tw.perf.report("throttledRefresh",refresh),
styleThrottledRefresh = $tw.perf.report("throttledRefresh",styleRefresh);
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
throttledRefreshFn(changes,styleRefresh,false,true);
throttledRefreshFn(changes,styleDeferredChanges,styleTimerId,styleThrottledRefresh,styleRefresh,false,true);
}));
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
$tw.perf.report("mainRender",function() {
Expand All @@ -107,18 +119,9 @@ exports.startup = function() {
removeItem.parentNode.removeChild(removeItem);
}
});
function refresh() {
// Process the refresh
$tw.hooks.invokeHook("th-page-refreshing");
$tw.pageWidgetNode.refresh(deferredChanges);
deferredChanges = Object.create(null);
$tw.hooks.invokeHook("th-page-refreshed");
}
var throttledRefresh = $tw.perf.report("throttledRefresh",refresh);

// Add the change event handler
$tw.wiki.addEventListener("change",$tw.perf.report("mainRefresh",function(changes) {
throttledRefreshFn(changes,refresh,true,false);
throttledRefreshFn(changes,mainDeferredChanges,mainTimerId,mainThrottledRefresh,refresh,true,false);
}));
// Fix up the link between the root widget and the page container
$tw.rootWidget.domNodes = [$tw.pageContainer];
Expand Down
27 changes: 16 additions & 11 deletions core/modules/startup/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ exports.startup = function() {
widgetNode = $tw.wiki.makeWidget(parser,{document: srcDocument, parentWidget: $tw.rootWidget, variables: variables});
widgetNode.render(srcDocument.body,srcDocument.body.firstChild);
// Prepare refresh mechanism
var deferredChanges = Object.create(null),
timerId,
throttledRefreshFn = function(changes,callback,mainCondition,styleCondition) {
var mainDeferredChanges = Object.create(null),
styleDeferredChanges = Object.create(null),
mainTimerId,
styleTimerId,
throttledRefreshFn = function(changes,deferredChanges,timerId,throttledRefresh,callback,mainCondition,styleCondition) {
// Check if only tiddlers that are throttled have changed
var onlyThrottledTiddlersHaveChanged = true;
for(var title in changes) {
Expand All @@ -106,27 +108,30 @@ exports.startup = function() {
$tw.utils.extend(deferredChanges,changes);
} else {
$tw.utils.extend(deferredChanges,changes);
callback(changes);
callback();
}
};
var styleRefresh = function(changes) {
if(styleWidgetNode.refresh(changes,styleContainer,null)) {
var styleRefresh = function() {
if(styleWidgetNode.refresh(styleDeferredChanges,styleContainer,null)) {
var newStyles = styleContainer.textContent;
if(newStyles !== styleWidgetNode.assignedStyles) {
styleWidgetNode.assignedStyles = newStyles;
styleElement.innerHTML = styleWidgetNode.assignedStyles;
}
}
styleDeferredChanges = Object.create(null);
};
var mainRefresh = function(changes) {
widgetNode.refresh(changes);
deferredChanges = Object.create(null);
var mainRefresh = function() {
widgetNode.refresh(mainDeferredChanges);
mainDeferredChanges = Object.create(null);
};
var mainThrottledRefresh = $tw.perf.report("throttledRefresh",mainRefresh),
styleThrottledRefresh = $tw.perf.report("throttledRefresh",styleRefresh);
styleRefreshHandler = function(changes) {
throttledRefreshFn(changes,styleRefresh,false,true);
throttledRefreshFn(changes,styleDeferredChanges,styleTimerId,styleThrottledRefresh,styleRefresh,false,true);
};
mainRefreshHandler = function(changes) {
throttledRefreshFn(changes,mainRefresh,true,false);
throttledRefreshFn(changes,mainDeferredChanges,mainTimerId,mainThrottledRefresh,mainRefresh,true,false);
};
$tw.wiki.addEventListener("change",styleRefreshHandler);
$tw.wiki.addEventListener("change",mainRefreshHandler);
Expand Down
Loading