Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@arthurschreiber
Copy link
Collaborator

@arthurschreiber arthurschreiber commented Mar 15, 2020

In #730, the ability to build a new Connection object 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.Socket in NodeJS works - calling new net.Socket won't actually open a connection to the remove server, unless the .connect method is called on the returned socket object. The net module also provides a module level .connect convenience method that creates and returns a new Socket instance and calls .connect on it.

This pull request adds a similar method on the tedious module, that creates a new Connection, calls .connect on it and returns the created connection object.

Calling new Connection without immediately calling .connect on it will emit a new deprecation warning, but will automatically call .connect to emulate the existing behavior. This existing behavior will be removed in the future, and not calling .connect will cause the connection to never be opened.

Fixes: #730

@arthurschreiber arthurschreiber requested a review from a team March 15, 2020 11:57
@codecov
Copy link

codecov bot commented Mar 15, 2020

Codecov Report

Merging #1069 into master will decrease coverage by 0.02%.
The diff coverage is 54.54%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
src/tedious.ts 25.00% <0.00%> (-75.00%) ⬇️
src/connection.ts 64.58% <63.15%> (+0.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2fb901a...79f2747. Read the comment docs.

@arthurschreiber arthurschreiber force-pushed the arthur/deprecate-automatic-connection branch 2 times, most recently from 59ffd8e to 0b91609 Compare March 15, 2020 19:55
Comment on lines +985 to +995
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();
}
});
Copy link
Collaborator Author

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.

@arthurschreiber arthurschreiber force-pushed the arthur/deprecate-automatic-connection branch 2 times, most recently from 817a738 to 2febd34 Compare March 16, 2020 07:48
@arthurschreiber arthurschreiber force-pushed the arthur/deprecate-automatic-connection branch from 2febd34 to 79f2747 Compare March 16, 2020 07:50
Comment on lines +999 to +1001
if (this.state !== this.STATE.INITIALIZED) {
throw new ConnectionError('`.connect` can not be called on a Connection in `' + this.state.name + '` state.');
}
Copy link
Collaborator Author

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.

@arthurschreiber arthurschreiber merged commit 7500ee3 into master Apr 13, 2020
@arthurschreiber arthurschreiber deleted the arthur/deprecate-automatic-connection branch April 13, 2020 09:24
@arthurschreiber
Copy link
Collaborator Author

🎉 This PR is included in version 8.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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 this pull request may close these issues.

4 participants