Skip to content

Commit

Permalink
feat: Add TypeScript declaration file (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito committed Feb 24, 2020
1 parent 99d39f7 commit 99d26b9
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 110 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
@@ -1,3 +1,5 @@
examples/
ios/
android/
android/
coverage/
node_modules/
29 changes: 17 additions & 12 deletions .eslintrc.json
@@ -1,8 +1,5 @@
{
"plugins": [
"prettier",
"jest"
],
{
"plugins": ["prettier", "jest", "jsdoc"],
"env": {
"es6": true,
"node": true
Expand All @@ -15,17 +12,25 @@
"eslint:recommended",
"prettier",
"plugin:jest/recommended",
"plugin:jest/style"
"plugin:jest/style",
"plugin:jsdoc/recommended"
],
"rules": {
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}],
"prefer-const": [
"error",
{
"destructuring": "any",
"ignoreReadBeforeAssign": false
}
],
"curly": ["error", "multi", "consistent"],
"no-var": "error",
"prefer-template": 2,
"require-atomic-updates": "off",
"prettier/prettier": ["error"]
"prettier/prettier": ["error"],
"jsdoc/require-returns-description": 0,
"jsdoc/require-param-description": 0,
"jsdoc/no-undefined-types": 0,
"jsdoc/require-returns": 0
}
}
}
2 changes: 1 addition & 1 deletion .releaserc
Expand Up @@ -9,7 +9,7 @@
}],
"@semantic-release/npm",
["@semantic-release/git", {
"assets": ["CHANGELOG.md", "package.json", "coverage/coverage-final.json"],
"assets": ["CHANGELOG.md", "package.json", "coverage/coverage-final.json", "dist/**/*.{js,d.ts}"],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}]
]
Expand Down
6 changes: 3 additions & 3 deletions coverage/coverage-final.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions declaration.tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"declaration": true,
"noEmit": false,
"emitDeclarationOnly": true,
"outFile": "./lib/types/index.d.ts"
},
"exclude": ["__tests__", "__mocks__"]
}
8 changes: 0 additions & 8 deletions jsconfig.json

This file was deleted.

16 changes: 12 additions & 4 deletions package.json
Expand Up @@ -4,10 +4,14 @@
"version": "3.2.9",
"description": "React Native TCP socket API for Android & iOS",
"main": "src/index.js",
"types": "lib/types/index.d.ts",
"scripts": {
"ci": "yarn install --frozen-lockfile && yarn lint && yarn test",
"ci": "yarn install --frozen-lockfile && yarn lint && yarn declaration:build && yarn checkjs && yarn test",
"lint": "eslint .",
"test": "jest ./__tests__"
"checkjs": "tsc",
"test": "jest ./__tests__",
"declaration:build": "tsc -p ./declaration.tsconfig.json",
"prepublishOnly": "yarn declaration:build && yarn checkjs"
},
"files": [
"/android/src/",
Expand Down Expand Up @@ -47,21 +51,25 @@
},
"devDependencies": {
"@babel/core": "^7.7.7",
"babel-jest": "^24.9.0",
"@semantic-release/changelog": "^5.0.0",
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.0.0",
"@semantic-release/npm": "^7.0.0",
"@types/jest": "^25.1.3",
"@types/react-native": "^0.61.17",
"babel-jest": "^24.9.0",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-jest": "^23.6.0",
"eslint-plugin-jsdoc": "^21.0.0",
"eslint-plugin-prettier": "^3.1.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.58.0",
"prettier": "^1.18.2",
"react": "16.9.0",
"react-native": "^0.61.4",
"semantic-release": "^17.0.1"
"semantic-release": "^17.0.1",
"typescript": "^3.8.2"
},
"dependencies": {
"buffer": "^5.4.3"
Expand Down
6 changes: 4 additions & 2 deletions src/TcpServer.js
Expand Up @@ -33,6 +33,7 @@ export default class TcpServer extends TcpSocket {
/**
* @param {{ port: number; host: any; }} options
* @param {(arg0: any) => void} callback
* @returns {TcpServer}
*/
listen(options, callback) {
let gotOptions = {};
Expand All @@ -43,6 +44,7 @@ export default class TcpServer extends TcpSocket {
'TcpServer.listen(port[, host][, callback]) is deprecated and has been moved to TcpServer.listen(options[, callback]). It will be removed in react-native-tcp-socket@4.0.0'
);
gotOptions.port = arguments[0];
/** @type {string} */
gotOptions.host = arguments[1];
callback = arguments[2];
} else {
Expand All @@ -60,12 +62,12 @@ export default class TcpServer extends TcpSocket {
}

/**
* @private
* @param {{ id: number; address: string; }} info
*/
_onConnection(info) {
const socket = new TcpSocket(info.id, this._eventEmitter);
socket._registerEvents();
socket.setConnected(info.address);
socket.setAsAlreadyConnected(info.address);
this._connections.push(socket);
this.connectionCallback(socket);
}
Expand Down
54 changes: 48 additions & 6 deletions src/TcpSocket.js
Expand Up @@ -31,6 +31,9 @@ class RemovableListener {
}
}

/**
* @typedef {{ port: number; host?: string; timeout?: number; localAddress?: string, localPort?: number, interface?: 'wifi', reuseAddress?: boolean}} ConnectionOptions
*/
export default class TcpSocket {
/**
* Initialices a TcpSocket.
Expand All @@ -41,6 +44,7 @@ export default class TcpSocket {
constructor(id, eventEmitter) {
this._id = id;
this._eventEmitter = eventEmitter;
/** @type {number} */
this._state = STATE.DISCONNECTED;
/** @type {RemovableListener[]} */
this._listeners = [];
Expand All @@ -52,8 +56,9 @@ export default class TcpSocket {
* The data arguments emitted will be passed to the listener callback.
*
* @param {string} event Name of the event to listen to
* @param {function(object): void} callback Function to invoke when the specified event is emitted
* @param {(arg0: any) => void} callback Function to invoke when the specified event is emitted
* @param {any} [context] Optional context object to use when invoking the listener
* @returns {RemovableListener}
*/
on(event, callback, context) {
const newListener = this._selectListener(event, callback, context);
Expand All @@ -63,6 +68,7 @@ export default class TcpSocket {
}

/**
* @private
* @param {string} event
* @param {function(any):void} callback
* @param {any} [context]
Expand Down Expand Up @@ -100,14 +106,17 @@ export default class TcpSocket {
}
}

/**
* @deprecated
*/
off() {
console.warn(
'TCPSocket.off() is deprecated and produces no effect, please use the listener remove() method instead.'
);
}

/**
* @param {{ host: string; port: number; timeout: number; }} options
* @param {ConnectionOptions} options
* @param {(address: string) => void} [callback]
*/
connect(options, callback) {
Expand All @@ -128,6 +137,7 @@ export default class TcpSocket {
}

/**
* @private
* @param {number} msecs
* @param {() => void} [wrapper]
*/
Expand All @@ -149,6 +159,9 @@ export default class TcpSocket {
};
}

/**
* @private
*/
_clearTimeout() {
if (this._timeout) {
clearTimeout(this._timeout.handle);
Expand All @@ -157,8 +170,9 @@ export default class TcpSocket {
}

/**
* @deprecated
* @param {number} msecs
* @param {{ (...args: any[]): any; (...args: any[]): any; }} [callback]
* @param {(...args: any[]) => void } [callback]
*/
setTimeout(msecs, callback) {
if (msecs === 0) {
Expand Down Expand Up @@ -195,28 +209,41 @@ export default class TcpSocket {
}
}

/**
* @protected
*/
_registerEvents() {
this.on('connect', (ev) => this._onConnect(ev.address));
this.on('close', () => this._onClose());
this.on('error', () => this._onError());
}

/**
* @private
*/
_unregisterEvents() {
this._listeners.forEach((listener) => (listener.isRemoved() ? listener.remove() : null));
this._listeners = [];
}

/**
* @private
* @param {string} address
*/
_onConnect(address) {
this.setConnected(address);
}

/**
* @private
*/
_onClose() {
this.setDisconnected();
}

/**
* @private
*/
_onError() {
this.destroy();
}
Expand All @@ -228,7 +255,7 @@ export default class TcpSocket {
*
* @param {string | Buffer | Uint8Array} buffer
* @param {BufferEncoding} [encoding]
* @param {(error?: string) => void} [callback]
* @param {(error: string | null) => void} [callback]
*/
write(buffer, encoding, callback) {
const self = this;
Expand All @@ -244,13 +271,24 @@ export default class TcpSocket {
*/
function(err) {
if (self._timeout) self._activeTimer(self._timeout.msecs);
if (err) return callback(err);
callback();
if (callback) {
if (err) return callback(err);
callback(null);
}
}
);
}

/**
* @param {string} address
*/
setAsAlreadyConnected(address) {
this._registerEvents();
this.setConnected(address);
}

/**
* @private
* @param {string | Buffer | Uint8Array} buffer
* @param {BufferEncoding} [encoding]
*/
Expand All @@ -265,13 +303,17 @@ export default class TcpSocket {
}

/**
* @private
* @param {string} address
*/
setConnected(address) {
this._state = STATE.CONNECTED;
this._address = address;
}

/**
* @private
*/
setDisconnected() {
if (this._state === STATE.DISCONNECTED) return;
this._unregisterEvents();
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Expand Up @@ -14,14 +14,16 @@ class TCPSockets {

/**
* @param {(socket: Socket) => void} connectionListener
* @returns {Server}
*/
createServer(connectionListener) {
return new Server(this.instances++, this._eventEmitter, connectionListener);
}

/**
* @param {{ host: string; port: number; timeout: number; }} options
* @param {import('./TcpSocket').ConnectionOptions} options
* @param {(address: string) => void} callback
* @returns {Socket}
*/
createConnection(options, callback) {
const tcpSocket = new Socket(this.instances++, this._eventEmitter);
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
},
"include": ["src/", "__tests__/", "__mocks__/"],
}

0 comments on commit 99d26b9

Please sign in to comment.