Skip to content

Commit

Permalink
Adds Polymer.RenderStatus.afterFirstRender method. Call to perform …
Browse files Browse the repository at this point in the history
…tasks after an element first renders.
  • Loading branch information
Steven Orvell committed Oct 30, 2015
1 parent 320d5c7 commit 71b5c2a
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/lib/render-status.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,50 @@

_makeReady: function() {
this._ready = true;
this._callbacks.forEach(function(cb) {
cb();
});
for (var i=0; i < this._callbacks.length; i++) {
this._callbacks[i]();
}
this._callbacks = [];
},

_catchFirstRender: function() {
requestAnimationFrame(function() {
Polymer.RenderStatus._makeReady();
});
},

_afterFirstRenderQueue: [],

_waitingAfterFirstRender: false,

afterFirstRender: function(element, fn, args) {
if (!this._waitingAfterFirstRender) {
this._waitingAfterFirstRender = true;
this.whenReady(this._flushAfterFirstRender);
}
this._afterFirstRenderQueue.push([element, fn, args]);
},

_flushAfterFirstRender: function() {
requestAnimationFrame(function() {
var self = Polymer.RenderStatus;
// if already ready, wait 2 frames to ensure async
if (self._ready) {
requestAnimationFrame(self.__flushAfterFirstRender);
} else {
self.__flushAfterFirstRender();
}
});
},

__flushAfterFirstRender: function() {
var self = Polymer.RenderStatus;
self._waitingAfterFirstRender = false;
for (var i=0, h; i < self._afterFirstRenderQueue.length; i++) {
h = self._afterFirstRenderQueue[i];
h[1].apply(h[0], h[2] || Polymer.nar);
};
self._afterFirstRenderQueue = [];
}
};

Expand Down

0 comments on commit 71b5c2a

Please sign in to comment.