Skip to content

Commit

Permalink
Merge pull request #371 from jwp-consulting/add-prettier
Browse files Browse the repository at this point in the history
Format code using prettier, update node build target
  • Loading branch information
paulbjensen authored Sep 28, 2023
2 parents a248c9b + 9c8152a commit 78de640
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[src/*.ts]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
max_line_length = 80
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Lint

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup Node.js
uses: actions/[email protected]
- name: Clean NPM install
run: npm clean-install
- name: Check with prettier
run: npm run check-prettier
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Anephenix OÜ
Copyright (c) 2019-2023 The Sarus Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
"version": "0.4.6",
"description": "A WebSocket JavaScript library",
"main": "dist/index.js",
"contributors": [
{
"name": "Paul Jensen",
"email": "[email protected]",
"url": "https://paulbjensen.co.uk"
},
{
"name": "Chris Lajoie",
"email": "[email protected]"
},
{
"name": "Justus Perlwitz",
"email": "[email protected]",
"url": "https://www.jwpconsulting.net"
}
],
"devDependencies": {
"@babel/parser": "^7.22.5",
"@babel/types": "^7.22.5",
Expand All @@ -17,14 +33,17 @@
"jsdom": "^22.1.0",
"mock-socket": "^9.2.1",
"npm-upgrade": "^3.1.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
},
"scripts": {
"build": "npx tsc --project tsconfig.json",
"watch": "npx tsc --project tsconfig.json --watch",
"cover": "npx jest --coverage --coverageReporters=text-lcov | coveralls",
"test": "npx jest --coverage"
"test": "npx jest --coverage",
"prettier": "prettier src --write",
"check-prettier": "prettier src --check"
},
"repository": {
"type": "git",
Expand Down
34 changes: 20 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// File Dependencies
import { WS_EVENT_NAMES, DATA_STORAGE_TYPES, DEFAULT_EVENT_LISTENERS_OBJECT } from "./lib/constants";
import {
WS_EVENT_NAMES,
DATA_STORAGE_TYPES,
DEFAULT_EVENT_LISTENERS_OBJECT,
} from "./lib/constants";
import { serialize, deserialize } from "./lib/dataTransformer";
import { PartialEventListenersInterface, EventListenersInterface } from "./lib/validators";
import {
PartialEventListenersInterface,
EventListenersInterface,
} from "./lib/validators";

interface StorageParams {
storageType: string;
Expand Down Expand Up @@ -93,7 +100,7 @@ export default class Sarus {
retryProcessTimePeriod, // TODO - write a test case to check this
retryConnectionDelay,
storageType = "memory",
storageKey = "sarus"
storageKey = "sarus",
} = props;

this.eventListeners = this.auditEventListeners(eventListeners);
Expand Down Expand Up @@ -246,15 +253,14 @@ export default class Sarus {
error: [],
close: [],
};

const mergedEventListeners: EventListenersInterface = {
...defaultEventListeners,
...eventListeners,
} as EventListenersInterface; // Type assertion added here

return mergedEventListeners;
}


/**
* Connects the WebSocket client, and attaches event listeners
Expand Down Expand Up @@ -310,7 +316,7 @@ export default class Sarus {
const eventFunctions = this.eventListeners[eventName];
if (eventFunctions && eventFunctions.indexOf(eventFunc) !== -1) {
throw new Error(
`${eventFunc.name} has already been added to this event Listener`
`${eventFunc.name} has already been added to this event Listener`,
);
}
if (eventFunctions && eventFunctions instanceof Array) {
Expand Down Expand Up @@ -347,7 +353,7 @@ export default class Sarus {
| {
doNotThrowError: boolean;
}
| undefined
| undefined,
) {
if (!existingFunc) {
if (!(opts && opts.doNotThrowError)) {
Expand All @@ -366,7 +372,7 @@ export default class Sarus {
off(
eventName: string,
eventFuncOrName: Function | string,
opts?: { doNotThrowError: boolean } | undefined
opts?: { doNotThrowError: boolean } | undefined,
) {
const existingFunc = this.findFunction(eventName, eventFuncOrName);
if (existingFunc) {
Expand Down Expand Up @@ -421,7 +427,7 @@ export default class Sarus {
*/
attachEventListeners() {
const self: any = this;
WS_EVENT_NAMES.forEach(eventName => {
WS_EVENT_NAMES.forEach((eventName) => {
self.ws[`on${eventName}`] = (e: Function) => {
self.eventListeners[eventName].forEach((f: Function) => f(e));
if (eventName === "close" && self.reconnectAutomatically) {
Expand All @@ -433,16 +439,16 @@ export default class Sarus {
}

/**
* Removes the event listeners from a closed WebSocket instance, so that
* Removes the event listeners from a closed WebSocket instance, so that
* they are cleaned up
*/
removeEventListeners() {
const self: any = this;
WS_EVENT_NAMES.forEach(eventName => {
WS_EVENT_NAMES.forEach((eventName) => {
if (self.ws.listeners && self.ws.listeners[eventName]) {
self.ws.listeners[eventName].forEach((iel:Function) => {
self.ws.listeners[eventName].forEach((iel: Function) => {
self.ws.removeEventListener(eventName, iel);
})
});
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const WS_EVENT_NAMES: Array<string> = [
"open",
"close",
"message",
"error"
"error",
];

/**
Expand All @@ -34,5 +34,5 @@ export const DEFAULT_EVENT_LISTENERS_OBJECT: EventListenersInterface = {
open: [],
message: [],
error: [],
close: []
};
close: [],
};

0 comments on commit 78de640

Please sign in to comment.