Skip to content

Instantly share code, notes, and snippets.

@simevidas
Forked from AdaRoseCannon/Finished polyfill.js
Last active August 3, 2020 10:58

Revisions

  1. simevidas revised this gist Aug 22, 2017. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions finished-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -9,15 +9,15 @@ if (document.body.animate) {
    if (Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {
    get() {
    if (!this._finished) {
    this._finished = this.playState === 'finished' ?
    Promise.resolve() :
    new Promise((resolve, reject) => {
    this.addEventListener('finish', resolve, {once: true});
    this.addEventListener('cancel', reject, {once: true});
    });
    }
    return this._finished;
    if (!this._finished) {
    this._finished = this.playState === 'finished' ?
    Promise.resolve() :
    new Promise((resolve, reject) => {
    this.addEventListener('finish', resolve, {once: true});
    this.addEventListener('cancel', reject, {once: true});
    });
    }
    return this._finished;
    }
    });
    }
  2. simevidas renamed this gist Aug 22, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. simevidas renamed this gist Aug 22, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. simevidas renamed this gist Aug 22, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. simevidas revised this gist Aug 22, 2017. 1 changed file with 23 additions and 16 deletions.
    39 changes: 23 additions & 16 deletions Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,24 @@
    if (document.body.animate && typeof Animation === 'undefined') {
    window.Animation = document.body.animate({}).constructor;
    }
    // only polyfill .finished in browsers that already support animate()
    if (document.body.animate) {

    // Chrome does not seem to expose the Animation constructor globally
    if (typeof Animation === 'undefined') {
    window.Animation = document.body.animate({}).constructor;
    }

    if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    if (this.__finishPromise) return this.__finishPromise;
    if (this.playState === 'finished') {
    this.__finishPromise = Promise.resolve();
    } else {
    this.__finishPromise = new Promise(function (resolve) {
    this.addEventListener('finish', resolve, {once: true});
    }.bind(this));
    }
    return this.__finishPromise;
    }});
    }
    if (Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {
    get() {
    if (!this._finished) {
    this._finished = this.playState === 'finished' ?
    Promise.resolve() :
    new Promise((resolve, reject) => {
    this.addEventListener('finish', resolve, {once: true});
    this.addEventListener('cancel', reject, {once: true});
    });
    }
    return this._finished;
    }
    });
    }
    }
  6. Ada Rose Edwards revised this gist Aug 20, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    if (document.body.animate && typeof Animation === 'undefined') {
    window.Animation = document.body.animate({}).constructor;
    }

    if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    if (this.__finishPromise) return this.__finishPromise;
  7. Ada Rose Edwards revised this gist Aug 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefin
    this.__finishPromise = Promise.resolve();
    } else {
    this.__finishPromise = new Promise(function (resolve) {
    this.addEventListener('finish', resolve);
    this.addEventListener('finish', resolve, {once: true});
    }.bind(this));
    }
    return this.__finishPromise;
  8. Ada Rose Edwards revised this gist Aug 19, 2017. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    return new Promise(function (resolve) {
    this.addEventListener('finish', resolve);
    }.bind(this));
    }});
    }
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    if (this.__finishPromise) return this.__finishPromise;
    if (this.playState === 'finished') {
    this.__finishPromise = Promise.resolve();
    } else {
    this.__finishPromise = new Promise(function (resolve) {
    this.addEventListener('finish', resolve);
    }.bind(this));
    }
    return this.__finishPromise;
    }});
    }
  9. Ada Rose Edwards revised this gist Aug 18, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    return new Promise(function (resolve) {
    this.addEventListener('finish', resolve);
    this.addEventListener('finish', resolve);
    }.bind(this));
    }});
    }
  10. Ada Rose Edwards created this gist Aug 18, 2017.
    7 changes: 7 additions & 0 deletions Finished polyfill.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    if (typeof Animation !== 'undefined' && Animation.prototype.finished === undefined) {
    Object.defineProperty(Animation.prototype, 'finished', {get: function () {
    return new Promise(function (resolve) {
    this.addEventListener('finish', resolve);
    }.bind(this));
    }});
    }