Skip to content
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

Paper-toast permanent duration #27

Closed
ryanwhite04 opened this issue Sep 18, 2015 · 1 comment · Fixed by #35
Closed

Paper-toast permanent duration #27

ryanwhite04 opened this issue Sep 18, 2015 · 1 comment · Fixed by #35
Assignees
Labels

Comments

@ryanwhite04
Copy link

It would be cool to be able to set an attribute for an infinite duration to use the paper-toast for things like showing a terms and cookie policy link on a site, shown here which can't go away until it is closed by the user.
You can already just put in a huge wait so it's necessary, It would just be cool.

Maybe something like

<paper-toast permanent></paper-toast>

or even possible

<paper-toast wait="0"></paper-toast>

I'm not sure what actually happens when you put a wait time of '0', except it calls debounce with a wait of '0' which calls async with a wait time of '0'

paper-toast.html

    show: function() {
      if (PaperToast.currentToast) {
        PaperToast.currentToast.hide();
      }
      PaperToast.currentToast = this;
      this.removeAttribute('aria-hidden');
      this._setVisible(true);
      this.fire('iron-announce', {
        text: this.text
      });
      this.debounce('hide', this.hide, this.duration);
    },

polymer.html

    Polymer.Async = {
        _currVal: 0,
        _lastVal: 0,
        _callbacks: [],
        _twiddleContent: 0,
        _twiddle: document.createTextNode(''),
        run: function(callback, waitTime) {

            //- if (waitTime === 0) possibly just return so the function never gets run
            if (waitTime > 0) {

                //- Not relevant to this issue, but what does the ~ do to the return value of setTimeout?
                return ~setTimeout(callback, waitTime);
            } else {
                this._twiddle.textContent = this._twiddleContent++;
                this._callbacks.push(callback);
                return this._currVal++;
            }
        },
        cancel: function(handle) {
            if (handle < 0) {
                clearTimeout(~handle);
            } else {
                var idx = handle - this._lastVal;
                if (idx >= 0) {
                    if (!this._callbacks[idx]) {
                        throw 'invalid async handle: ' + handle;
                    }
                    this._callbacks[idx] = null;
                }
            }
        },
        _atEndOfMicrotask: function() {
            var len = this._callbacks.length;
            for (var i = 0; i < len; i++) {
                var cb = this._callbacks[i];
                if (cb) {
                    try {
                        cb();
                    } catch (e) {
                        i++;
                        this._callbacks.splice(0, i);
                        this._lastVal += i;
                        this._twiddle.textContent = this._twiddleContent++;
                        throw e;
                    }
                }
            }
            this._callbacks.splice(0, len);
            this._lastVal += len;
        }
    };
    new(window.MutationObserver || JsMutationObserver)(Polymer.Async._atEndOfMicrotask.bind(Polymer.Async)).observe(Polymer.Async._twiddle, {
        characterData: true
    });
    Polymer.Debounce = function() {
        var Async = Polymer.Async;
        var Debouncer = function(context) {
            this.context = context;
            this.boundComplete = this.complete.bind(this);
        };
        Debouncer.prototype = {
            go: function(callback, wait) {
                var h;
                this.finish = function() {
                    Async.cancel(h);
                };
                h = Async.run(this.boundComplete, wait);
                this.callback = callback;
            },
            stop: function() {
                if (this.finish) {
                    this.finish();
                    this.finish = null;
                }
            },
            complete: function() {
                if (this.finish) {
                    this.stop();
                    this.callback.call(this.context);
                }
            }
        };

        function debounce(debouncer, callback, wait) {
            if (debouncer) {
                debouncer.stop();
            } else {
                debouncer = new Debouncer(this);
            }
            debouncer.go(callback, wait);
            return debouncer;
        }
        return debounce;
    }();

🐱

@valdrinkoshi
Copy link
Member

Hi @ryanwhite04, as you suggested this could be already achieved by setting a high value to duration.
Edge cases like 0, -10 or Infinity are not currently handled (toast won't show), I'll fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants