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

timers: give Timeouts a constructor name #5793

Merged
merged 1 commit into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ exports.clearInterval = function(timer) {
};


const Timeout = function(after) {
function Timeout(after) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to make it a class Timeout?

class Timeout {
  constructor(after) {
    this._called = false;
    this._idleTimeout = after;
    this._idlePrev = this;
    this._idleNext = this;
    this._idleStart = null;
    this._onTimeout = null;
    this._repeat = null;
  }
  unref () {
    if (this._handle) {
      this._handle.unref();
    } else if (typeof this._onTimeout === 'function') {
      var now = TimerWrap.now();
      if (!this._idleStart) this._idleStart = now;
      var delay = this._idleStart + this._idleTimeout - now;
      if (delay < 0) delay = 0;

      // Prevent running cb again when unref() is called during the same cb
      if (this._called && !this._repeat) {
        exports.unenroll(this);
        return;
      }

      var handle = reuse(this);

      this._handle = handle || new TimerWrap();
      this._handle.owner = this;
      this._handle[kOnTimeout] = function unrefdHandle() {
        this.owner._onTimeout();
        if (!this.owner._repeat)
          this.owner.close();
      };
      this._handle.start(delay, 0);
      this._handle.domain = this.domain;
      this._handle.unref();
    }
    return this;
  }
  ref() {
    if (this._handle)
      this._handle.ref();
    return this;
  }
  close() {
    this._onTimeout = null;
    if (this._handle) {
      this._handle[kOnTimeout] = null;
      this._handle.close();
    } else {
      exports.unenroll(this);
    }
    return this;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No gain, more code change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it modernizes things a little and makes them more explicit. If we want to minimize change why not keep it const Timeout and do const Timeout = function Timeout - by naming the function expression you get the same trace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why even assign it though? It's unnecessary.

Typically we have avoided modernization unless it meant performance increases or was part of some refactor that touched the code a lot anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough - I'm sorry if all these questions annoy you :)

this._called = false;
this._idleTimeout = after;
this._idlePrev = this;
Expand Down
2 changes: 1 addition & 1 deletion test/message/timeout_throw.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
undefined_reference_error_maker;
^
ReferenceError: undefined_reference_error_maker is not defined
at ._onTimeout (*test*message*timeout_throw.js:*:*)
at Timeout._onTimeout (*test*message*timeout_throw.js:*:*)
at tryOnTimeout (timers.js:*:*)
at Timer.listOnTimeout (timers.js:*:*)