Skip to content

Commit

Permalink
Merge pull request #23 from ziir/modern-setup
Browse files Browse the repository at this point in the history
feature: modernize setup
  • Loading branch information
ziir authored Feb 26, 2021
2 parents dfe8ff9 + bab9ee3 commit d878327
Show file tree
Hide file tree
Showing 18 changed files with 4,249 additions and 3,261 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

6 changes: 6 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Firefox ESR
last 2 Chrome major versions
last 2 Edge major versions
last 2 Firefox major versions
last 2 Opera major versions
last 2 Safari major versions
16 changes: 16 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
],
presets: [
[
'@babel/preset-env',
{
bugfixes: true,
useBuiltIns: false,
modules: 'commonjs',
},
],
'@babel/preset-typescript',
],
};
90 changes: 40 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,56 @@
"files": [
"build"
],
"husky": {
"hooks": {
"pre-push": "npm run ci"
}
},
"directories": {
"example": "examples"
},
"devDependencies": {
"@types/mocha": "^2.2.46",
"@types/sinon": "^4.1.3",
"@types/ws": "^3.2.1",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"husky": "^2.4.0",
"mocha": "^4.0.1",
"should": "^13.1.3",
"sinon": "^4.1.5",
"ts-loader": "^5.2.2",
"ts-node": "^3.3.0",
"tslint": "^5.6.0",
"typescript": "^3.1.2",
"typescript-formatter": "^7.0.0",
"webpack": "^4.23.1",
"webpack-cli": "^3.1.2",
"ws": "^3.3.2"
"repository": {
"type": "git",
"url": "[email protected]:Dashlane/ts-event-bus.git"
},
"keywords": [
"ts-event-bus",
"typescript",
"event-bus"
],
"license": "Apache-2.0",
"scripts": {
"ci": "npm run validate:lint && npm run validate:format && npm run test && node ./meta/audit.js",
"test": "mocha --require ts-node/register --inline-diffs --reporter spec --recursive ./test/**/*.ts",
"prebuild": "yarn build:clean",
"build": "webpack --config webpack.config.js",
"ci": "run-s validate:* test",
"test": "jest",
"build": "run-s build:*",
"build:clean": "rm -rf ./build",
"build:src": "babel src -d ./build --extensions \".ts\"",
"build:dec": "tsc -d --emitDeclarationOnly --declarationMap",
"validate": "run-p validate:*",
"validate:lint": "tslint -t stylish --project .",
"validate:format": "tsfmt --verify",
"validate:typecheck": "tsc --noEmit",
"validate:audit": "node ./meta/audit.js",
"lint": "tslint --fix --project .",
"format": "tsfmt -r",
"prepublish": "yarn run build"
"prepublishOnly": "run-s validate build"
},
"repository": {
"type": "git",
"url": "[email protected]:Dashlane/ts-event-bus.git"
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@types/jest": "^26.0.20",
"@types/ws": "^3.2.1",
"husky": "^4.3.8",
"jest": "^26.6.3",
"npm-run-all": "^4.1.5",
"tslint": "^5.6.0",
"typescript": "^4.1.3",
"typescript-formatter": "^7.2.2",
"ws": "^3.3.2"
},
"husky": {
"hooks": {
"pre-push": "npm run ci"
}
},
"contributors": [
{
Expand All @@ -60,22 +67,5 @@
"name": "Denis Schneider",
"email": "[email protected]"
}
],
"keywords": [
"ts-event-bus",
"typescript",
"event-bus"
],
"resolutions": {
"elliptic": ">=6.5.3",
"js-yaml": ">=3.13.1",
"just-extend": ">=4.0.0",
"lodash": ">=4.17.19",
"minimist": ">=0.2.1 <1.0.0 || >=1.2.3",
"mixin-deep": ">=1.3.2 <2.0.0 || >=2.0.1",
"serialize-javascript": ">=3.1.0",
"set-value": ">=2.0.1 <3.0.0 || >=3.0.1",
"yargs-parser": ">=13.1.2 <14.0.0 || >=15.0.1 <16.0.0 || >=18.1.2"
},
"license": "Apache-2.0"
]
}
32 changes: 18 additions & 14 deletions src/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ export function combineEvents<
C16 extends EventDeclaration, C17 extends EventDeclaration, C18 extends EventDeclaration,
C19 extends EventDeclaration, C20 extends EventDeclaration, C21 extends EventDeclaration,
C22 extends EventDeclaration, C23 extends EventDeclaration, C24 extends EventDeclaration
>(
_c1: C1, _c2: C2, _c3?: C3, _c4?: C4, _c5?: C5, _c6?: C6, _c7?: C7, _c8?: C8,
_c9?: C9, _c10?: C10, _c11?: C11, _c12?: C12, _c13?: C13, _c14?: C14, _c15?: C15,
_c16?: C16, _c17?: C17, _c18?: C18, _c19?: C19, _c20?: C20, _c21?: C21, _c22?: C22,
_c23?: C23, _c24?: C24
): C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9 & C10 & C11 & C12 & C13 & C14 & C15 & C16
>(
_c1: C1, _c2: C2, _c3?: C3, _c4?: C4, _c5?: C5, _c6?: C6, _c7?: C7, _c8?: C8,
_c9?: C9, _c10?: C10, _c11?: C11, _c12?: C12, _c13?: C13, _c14?: C14, _c15?: C15,
_c16?: C16, _c17?: C17, _c18?: C18, _c19?: C19, _c20?: C20, _c21?: C21, _c22?: C22,
_c23?: C23, _c24?: C24
): C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9 & C10 & C11 & C12 & C13 & C14 & C15 & C16
& C17 & C18 & C19 & C20 & C21 & C22 & C23 & C24 {

const args = Array.from(arguments)

const keys = args.reduce((keys, arg) => {
return [...keys, ...Object.keys(arg)]
const keys = args.reduce((acc, arg) => {
acc.push.apply(acc, Object.keys(arg))
return acc
}, [])

const uniqKeys = [...new Set(keys)]
Expand All @@ -40,10 +41,13 @@ export function combineEvents<

export function createEventBus<C extends EventDeclaration>(args: { events: C, channels?: Channel[] }): C {
const transports = (args.channels || []).map(c => new Transport(c))
return Object.keys(args.events)
.reduce((conn: C, slotName) => {
const config = args.events[slotName].config
conn[slotName] = connectSlot(slotName, transports as Transport[], config)
return conn
}, {} as any)

const eventBus: Partial<C> = {}
for (const event in args.events) {
if (args.events.hasOwnProperty(event)) {
eventBus[event] = (connectSlot(event, transports, args.events[event].config) as C[Extract<keyof C, string>])
}
}

return eventBus as C
}
2 changes: 1 addition & 1 deletion src/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Handlers may be synchronous or asynchronous. Asynchronicity is Promise-based.
*/
export type Handler<RequestData=any, ResponseData=any> = (requestData: RequestData) => ResponseData | Promise<ResponseData>
export type Handler<RequestData = any, ResponseData = any> = (requestData: RequestData) => ResponseData | Promise<ResponseData>

/**
* Helper function that allows to call synchronous and asynchronous handler
Expand Down
8 changes: 4 additions & 4 deletions src/Slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const findAllUsedParams = (handlers: Handlers): string[] =>
*
* The slot can also be subscribed to, by using the `on` property.
*/
export interface Slot<RequestData=null, ResponseData=void> {
export interface Slot<RequestData = null, ResponseData = void> {
// TODO: Find a solution to make it possible to omit the requestData as
// optional only when explicitly typed as such by the client.

Expand Down Expand Up @@ -91,13 +91,13 @@ export interface Slot<RequestData=null, ResponseData=void> {
* It returns a fake slot, that will throw if triggered or subscribed to.
* Slots need to be connected in order to be functional.
*/
export function slot<RequestData=void, ResponseData=void>(
export function slot<RequestData = void, ResponseData = void>(
config: SlotConfig = defaultSlotConfig
): Slot<RequestData, ResponseData> {
return getNotConnectedSlot(config)
}

export function connectSlot<T=void, T2=void>(
export function connectSlot<T = void, T2 = void>(
slotName: string,
transports: Transport[],
config: SlotConfig = {}
Expand Down Expand Up @@ -300,7 +300,7 @@ export function connectSlot<T=void, T2=void>(

// Get param and handler from arguments, depending if param was passed or not
let param = ""
let handler: Handler<any, any> = () => new Promise(r => r())
let handler: Handler<any, any> = () => new Promise<void>(r => r())

if (typeof paramOrHandler === 'string') {
param = paramOrHandler
Expand Down
42 changes: 19 additions & 23 deletions test/Channel.test.ts → test/Channel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import 'should'

import { TestChannel, TestChunkedChannel } from './TestChannel'
import * as sinon from 'sinon'
import { largeData } from './data'

describe('GenericChannel', () => {

it('should call onConnect subscribers when its _connected method is called', () => {
const testInstance = new TestChannel()
const spy = sinon.spy()
const spy = jest.fn()
testInstance.onConnect(spy)
testInstance.callConnected()
spy.called.should.be.True()
expect(spy).toHaveBeenCalled()
})

it('should call onDisconnect subscribers when its _disconnected method is called', () => {
const testInstance = new TestChannel()
const spy = sinon.spy()
const spy = jest.fn()
testInstance.onDisconnect(spy)
testInstance.callDisconnected()
spy.called.should.be.True()
expect(spy).toHaveBeenCalled()
})

it('should call onData callbacks when its _messageReceived method is called', () => {
const testInstance = new TestChannel()
const spy = sinon.spy()
const spy = jest.fn()
testInstance.onData(spy)
testInstance.callMessageReceived()
spy.called.should.be.True()
expect(spy).toHaveBeenCalled()
})

it('should call onError callbacks when its _error method is called', () => {
const testInstance = new TestChannel()
const spy = sinon.spy()
const spy = jest.fn()
testInstance.onError(spy)
testInstance.callError()
spy.called.should.be.True()
expect(spy).toHaveBeenCalled()
})

})
Expand All @@ -50,13 +46,13 @@ describe('ChunkedChannel', () => {

const testInstance = new TestChunkedChannel(chunkSize)
testInstance.onData(d => {
testInstance.sendSpy.getCall(0).args[0].type.should.equal('chunk_start')
testInstance.sendSpy.getCall(1).args[0].type.should.equal('chunk_data')
testInstance.sendSpy.getCall(1).args[0].chunkId.should.be.a.String
testInstance.sendSpy.getCall(1).args[0].data.should.be.lengthOf(chunkSize)
d.should.match(testObject)
testInstance.sendSpy.callCount.should.equal(numberOfMessages)
testInstance.dataSpy.callCount.should.equal(1)
expect(testInstance.sendSpy.mock.calls[0][0].type).toEqual('chunk_start')
expect(testInstance.sendSpy.mock.calls[1][0].type).toEqual('chunk_data')
expect(typeof testInstance.sendSpy.mock.calls[1][0].chunkId).toEqual('string')
expect(testInstance.sendSpy.mock.calls[1][0].data).toHaveLength(chunkSize)
expect(d).toMatchObject(testObject)
expect(testInstance.sendSpy.mock.calls).toHaveLength(numberOfMessages)
expect(testInstance.dataSpy.mock.calls).toHaveLength(1)
})
testInstance.send(testObject)
}
Expand Down Expand Up @@ -108,9 +104,9 @@ describe('ChunkedChannel', () => {
const testInstance = new TestChunkedChannel(100)
testInstance.onData(d => {
// The first message sent should be the message itself, instead of 'chunk_start'
testInstance.sendSpy.getCall(0).args[0].should.match(smallObject)
testInstance.sendSpy.callCount.should.equal(1)
testInstance.dataSpy.callCount.should.equal(1)
expect(testInstance.sendSpy.mock.calls[0][0]).toMatchObject(smallObject)
expect(testInstance.sendSpy.mock.calls).toHaveLength(1)
expect(testInstance.dataSpy.mock.calls).toHaveLength(1)
})
testInstance.send(smallObject as any)
})
Expand All @@ -124,7 +120,7 @@ describe('ChunkedChannel', () => {
}
const testInstance = new TestChunkedChannel(100, 500)
testInstance.onData(d => {
testInstance.dataSpy.getCall(0).args[0].should.match(obj)
expect(testInstance.dataSpy.mock.calls[0][0]).toMatchObject(obj)
})
testInstance.send(obj as any)
})
Expand Down
Loading

0 comments on commit d878327

Please sign in to comment.