zulip/static/js/vdom.js

186 lines
5.4 KiB
JavaScript
Raw Normal View History

const _ = require("lodash");
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
exports.eq_array = (a, b, eq) => {
if (a === b) {
// either both are undefined, or they
// are referentially equal
return true;
}
if (a === undefined || b === undefined) {
return false;
}
if (a.length !== b.length) {
return false;
}
return a.every((item, i) => eq(item, b[i]));
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
};
exports.ul = (opts) => ({
tag_name: "ul",
opts,
});
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
exports.render_tag = (tag) => {
/*
This renders a tag into a string. It will
automatically escape attributes, but it's your
responsibility to make sure keyed_nodes provide
a `render` method that escapes HTML properly.
(One option is to use templates.)
Do NOT call this method directly, except for
testing. The vdom scheme expects you to use
the `update` method.
*/
const opts = tag.opts;
const tag_name = tag.tag_name;
const attr_str = opts.attrs
.map((attr) => " " + attr[0] + '="' + _.escape(attr[1]) + '"')
.join("");
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
const start_tag = "<" + tag_name + attr_str + ">";
const end_tag = "</" + tag_name + ">";
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
if (opts.keyed_nodes === undefined) {
blueslip.error("We need keyed_nodes to render innards.");
return;
}
const innards = opts.keyed_nodes.map((node) => node.render()).join("\n");
return start_tag + "\n" + innards + "\n" + end_tag;
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
};
exports.update = (replace_content, find, new_dom, old_dom) => {
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
/*
The update method allows you to continually
update a "virtual" representation of your DOM,
and then this method actually updates the
real DOM using jQuery. The caller will pass
in a method called `replace_content` that will replace
the entire html and a method called `find` to
find the existing DOM for more surgical updates.
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
The first "update" will be more like a create,
because your `old_dom` should be undefined.
After that initial call, it is important that
you always pass in a correct value of `old_dom`;
otherwise, things will be incredibly confusing.
The basic scheme here is simple:
1) If old_dom is undefined, we render
everything for the first time.
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
2) If the keys of your new children are no
longer the same order as the old
children, then we just render
everything anew.
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
(We may refine this in the future.)
3) If your key structure remains the same,
then we update your child nodes on
a child-by-child basis, and we avoid
updates where the data had remained
the same.
The key to making this all work is that
`new_dom` should include a `keyed_nodes` option
where each `keyed_node` has a `key` and supports
these methods:
eq - can compare itself to similar nodes
for data equality
render - can create an HTML representation
of itself
The `new_dom` should generally be created with
something like `vdom.ul`, which will set a
tag field internally and which will want options
like `attrs` for attributes.
For examples of creating vdom objects, look at
`pm_list_dom.js`.
*/
function do_full_update() {
const rendered_dom = exports.render_tag(new_dom);
replace_content(rendered_dom);
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
}
if (old_dom === undefined) {
do_full_update();
return;
}
const new_opts = new_dom.opts;
const old_opts = old_dom.opts;
if (new_opts.keyed_nodes === undefined) {
// We generally want to use vdom on lists, and
// adding keys for childrens lets us avoid unnecessary
// redraws (or lets us know we should just rebuild
// the dom).
blueslip.error("We need keyed_nodes for updates.");
return;
}
const same_structure = exports.eq_array(
new_opts.keyed_nodes,
old_opts.keyed_nodes,
(a, b) => a.key === b.key,
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
);
if (!same_structure) {
/* We could do something smarter like detecting row
moves, but it's overkill for small lists.
*/
do_full_update();
return;
}
/*
DO "QUICK" UPDATES:
We've gotten this far, so we know we have the
same overall structure for our parent tag, and
the only thing left to do with our child nodes
is to possibly update them in place (via jQuery).
We will only update nodes whose data has changed.
*/
const child_elems = find().children();
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const [i, new_node] of new_opts.keyed_nodes.entries()) {
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
const old_node = old_opts.keyed_nodes[i];
if (new_node.eq(old_node)) {
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
continue;
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
}
const rendered_dom = new_node.render();
child_elems.eq(i).replaceWith(rendered_dom);
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
exports.update_attrs(find(), new_opts.attrs, old_opts.attrs);
};
exports.update_attrs = (elem, new_attrs, old_attrs) => {
const new_dict = new Map(new_attrs);
const old_dict = new Map(old_attrs);
for (const [k, v] of new_attrs) {
if (v !== old_dict.get(k)) {
elem.attr(k, v);
}
}
for (const [k] of old_attrs) {
if (!new_dict.has(k)) {
elem.removeAttr(k);
}
}
pm_list: Simplify redraws for Private Messages. We now use vdom-ish techniques to track the list items for the pm list. When we go to update the list, we only re-render nodes whose data has changed, with two exceptions: - Obviously, the first time we do a full render. - If the keys for the items have changed (i.e. a new node has come in or the order has changed), we just re-render the whole list. If the keys are the same since the last re-render, we only re-render individual items if their data has changed. Most of the new code is in these two modules: - pm_list_dom.js - vdom.js We remove all of the code in pm_list.js that is related to updating DOM with unread counts. For presence updates, we are now *never* re-rendering the whole list, since presence updates only change individual line items and don't affect the keys. Instead, we just update any changed elements in place. The main thing that makes this all work is the `update` method in `vdom`, which is totally generic and essentially does a few simple jobs: - detect if keys are different - just render the whole ul as needed - for items that change, do the appropriate jQuery to update the item in place Note that this code seems to play nice with simplebar. Also, this code continues to use templates to render the individual list items. FWIW this code isn't radically different than list_render, but it's got some key differences: - There are fewer bells and whistles in this code. Some of the stuff that list_render does is overkill for the PM list. - This code detects data changes. Note that the vdom scheme is agnostic about templates; it simply requires the child nodes to provide a render method. (This is similar to list_render, which is also technically agnostic about rendering, but which also does use templates in most cases.) These fixes are somewhat related to #13605, but we haven't gotten a solid repro on that issue, and the scrolling issues there may be orthogonal to the redraws. But having fewer moving parts here should help, and we won't get the rug pulled out from under us on every presence update. There are two possible extensions to this that are somewhat overlapping in nature, but can be done one a time. * We can do a deeper vdom approach here that gets us away from templates, and just have nodes write to an AST. I have this on another branch, but it might be overkill. * We can avoid some redraws by detecting where keys are moving up and down. I'm not completely sure we need it for the PM list. If this gets merged, we may want to try similar things for the stream list, which also does a fairly complicated mixture of big-hammer re-renders and surgical updates-in-place (with custom code). BTW we have 100% line coverage for vdom.js.
2020-01-04 17:17:44 +01:00
};
window.vdom = exports;