-
Notifications
You must be signed in to change notification settings - Fork 444
Deprecate automatically opening a connection when building a Connection instance
#1069
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1069 +/- ##
==========================================
- Coverage 80.77% 80.75% -0.03%
==========================================
Files 86 86
Lines 4384 4395 +11
Branches 793 796 +3
==========================================
+ Hits 3541 3549 +8
- Misses 584 587 +3
Partials 259 259
Continue to review full report at Codecov.
|
59ffd8e to
0b91609
Compare
| process.nextTick(() => { | ||
| if (this.state === this.STATE.INITIALIZED) { | ||
| const message = 'In the next major version of `tedious`, creating a new ' + | ||
| '`Connection` instance will no longer establish a connection to the ' + | ||
| 'server automatically. Please use the new `connect` helper function or ' + | ||
| 'call the `.connect` method on the newly created `Connection` object to ' + | ||
| 'silence this message.'; | ||
| deprecate(message); | ||
| this.connect(); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a neat "trick" - if new Connection() is called without a call to .connect in the same event loop tick, .connect will be called automatically, but there will also be a deprecation message emitted. This ensures existing code continues working as-is.
The deprecation message can easily be silenced by calling .connect right after calling new Connection(), allowing users to migrate their code to the new API before upgrading to the next major version.
817a738 to
2febd34
Compare
2febd34 to
79f2747
Compare
| if (this.state !== this.STATE.INITIALIZED) { | ||
| throw new ConnectionError('`.connect` can not be called on a Connection in `' + this.state.name + '` state.'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something I'm not 100% sure about. net.Socket allows calling .connect multiple times (for whatever reason 🤷♂).
I'd argue that calling .connect more than once is a programmer error, and thus throwing an exception is the correct way to handle this.
|
🎉 This PR is included in version 8.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
In #730, the ability to build a new
Connectionobject without immediately opening the actual connection to the server was requested.I think this requested behavior should actually be the default. This is also how
net.Socketin NodeJS works - callingnew net.Socketwon't actually open a connection to the remove server, unless the.connectmethod is called on the returned socket object. Thenetmodule also provides a module level.connectconvenience method that creates and returns a newSocketinstance and calls.connecton it.This pull request adds a similar method on the
tediousmodule, that creates a newConnection, calls.connecton it and returns the created connection object.Calling
new Connectionwithout immediately calling.connecton it will emit a new deprecation warning, but will automatically call.connectto emulate the existing behavior. This existing behavior will be removed in the future, and not calling.connectwill cause the connection to never be opened.Fixes: #730