Skip to content

Combine mutations on the same parent paths into single cache db updates. Improves performance #69

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 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
16 changes: 13 additions & 3 deletions src/api-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export class WebApi extends Api {
// Automatic reconnect should be done by socket.io
this._connectionState = CONNECTION_STATE_CONNECTING;
this._eventTimeline.disconnect = Date.now();
if (reason === 'io server disconnect') {
if (reason === 'io server disconnect' || reason === 'transport close') {
// if the server has shut down and disconnected all clients, we have to reconnect manually
this.socket = null;
this.connect().catch(err => {
Expand Down Expand Up @@ -557,14 +557,24 @@ export class WebApi extends Api {
// NOTE: fireThisEvent === true, because it is impossible that this mutation was caused by us (well, it should be!)
}
else if (data.event === 'mutations') {
// Apply all mutations
// Apply all mutations, combine set operations on paths with the same parent path to single updates for better performance
const mutations = val.current as IDataMutationsArray;
const updates = [] as Array<{ path: string, value: any }>;
mutations.forEach(m => {
const pathInfo = m.target.reduce(
(pathInfo, key) => pathInfo.child(key),
PathInfo.get(this.getCachePath())
);
this.cache.db.api.set(pathInfo.path, m.val, { suppress_events: !fireCacheEvents, context });
const update = updates.find(u => u.path === pathInfo.parentPath);
if (update) {
update.value[pathInfo.key] = m.val;
}
else {
updates.push({ path: pathInfo.parentPath, value: { [pathInfo.key]: m.val } });
}
});
updates.forEach(update => {
this.cache.db.api.update(update.path, update.value, { suppress_events: !fireCacheEvents, context });
});
}
else if (data.event === 'notify_child_removed') {
Expand Down