Skip to content

Commit

Permalink
Merge pull request #244 from DockYard/sn/schedule-once
Browse files Browse the repository at this point in the history
ScheduleOnce is not deduping
  • Loading branch information
snewcomer authored Jul 28, 2020
2 parents a6c516e + ebfe7fb commit da623cc
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions addon/services/in-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,12 @@ export default class InViewport extends Service {
}
const observerOptions = this.buildObserverOptions(configOptions);

scheduleOnce('afterRender', this, () => {
if (this.isDestroyed || this.isDestroying) {
return;
}

// create IntersectionObserver instance or add to existing
this.setupIntersectionObserver(
element,
observerOptions,
enterCallback,
exitCallback
);
});
scheduleOnce('afterRender', this, this.setupIntersectionObserver, element, observerOptions, enterCallback, exitCallback);
} else {
if (!this.rafAdmin) {
this.startRAF();
}
scheduleOnce('afterRender', this, () => {
if (this.isDestroyed || this.isDestroying) {
return;
}

enterCallback = enterCallback || noop;
exitCallback = exitCallback || noop;

// this isn't using the same functions as the mixin case, but that is b/c it is a bit harder to unwind.
// So just rewrote it with pure functions for now
startRAF(
element,
configOptions,
enterCallback,
exitCallback,
this.addRAF.bind(this, element.id),
this.removeRAF.bind(this, element.id)
);
});
scheduleOnce('afterRender', this, this._startRaf, element, configOptions, enterCallback, exitCallback);
}

return {
Expand Down Expand Up @@ -157,6 +127,10 @@ export default class InViewport extends Service {
* @void
*/
setupIntersectionObserver(element, observerOptions, enterCallback, exitCallback) {
if (this.isDestroyed || this.isDestroying) {
return;
}

this.addToRegistry(element, observerOptions);

this.observerAdmin.add(
Expand Down Expand Up @@ -241,4 +215,24 @@ export default class InViewport extends Service {
return assign(defaultOptions, owner.lookup('config:in-viewport'));
}
}

_startRaf(element, configOptions, enterCallback, exitCallback) {
if (this.isDestroyed || this.isDestroying) {
return;
}

enterCallback = enterCallback || noop;
exitCallback = exitCallback || noop;

// this isn't using the same functions as the mixin case, but that is b/c it is a bit harder to unwind.
// So just rewrote it with pure functions for now
startRAF(
element,
configOptions,
enterCallback,
exitCallback,
this.addRAF.bind(this, element.id),
this.removeRAF.bind(this, element.id)
);
}
}

0 comments on commit da623cc

Please sign in to comment.