Skip to content

Commit cb9f711

Browse files
committed
#30 Optimization: send 'data-modified' packets only if needed
1 parent ba69371 commit cb9f711

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

lib/firequery-actor.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ var FireQueryActor = ActorClass(
357357
return;
358358
}
359359

360+
// Bail out if the element has already been cached as modified.
360361
if (this.modifiedElements.has(element)) {
361362
return;
362363
}
@@ -368,22 +369,46 @@ var FireQueryActor = ActorClass(
368369
}),
369370

370371
/**
371-
* Send chunk of modified elements.
372+
* Send chunk of modified elements after a timeout.
372373
*/
373-
sendDataModified: function() {
374+
sendDataModified: makeInfallible(function() {
374375
this.modifiedElementsTimeout = null;
375376

377+
let walkerActor = this.conn.getActor(this.walkerActorID);
378+
379+
// WalkerActor.hasNode() method has been introduced in Firefox 43.
380+
// See also: https://bugzilla.mozilla.org/show_bug.cgi?id=1195742
381+
// TODO: The workaround can be removed when Fx43 is the minimum
382+
// requirement FIXME.
383+
let hasNode = typeof(walkerActor.hasNode) == "function" ?
384+
walkerActor.hasNode.bind(walkerActor) :
385+
walkerActor._refMap.has.bind(walkerActor._refMap);
386+
387+
// This is an important optimization that can filter list of
388+
// packets send to the client down to the bare minimum.
389+
// Make sure all modifiedElements are still known by the walker,
390+
// they could have been removed during the timeout or they haven't
391+
// even been imported by the client yet.
392+
for (let node of this.modifiedElements) {
393+
if (!hasNode(node)) {
394+
this.modifiedElements.delete(node);
395+
}
396+
}
397+
398+
// Bail out if there is nothing to send.
376399
if (!this.modifiedElements.size) {
377400
return;
378401
}
379402

380-
let walkerActor = this.conn.getActor(this.walkerActorID);
381403
let data = walkerActor.attachElements(this.modifiedElements);
382404

405+
// Clean up the cache with modified elements.
383406
this.modifiedElements.clear();
384407

408+
// Send data modified event to the client,
409+
// so the UI can be updated.
385410
Events.emit(this, "data-modified", data);
386-
},
411+
}),
387412

388413
/**
389414
* Returns jQuery data for given node.

0 commit comments

Comments
 (0)