Skip to content

Commit

Permalink
chore: change high performance EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Sep 20, 2023
1 parent 7bbae2b commit 65454fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@
]
},
"dependencies": {
"events": "^3.3.0",
"mqtt-packet": "^8.1.2",
"react-native-tcp-socket": "^6.0.6"
"react-native-tcp-socket": "^6.0.6",
"tseep": "^1.1.1"
}
}
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Buffer } from 'buffer';
import type { Packet, Parser } from 'mqtt-packet';
import net from 'react-native-tcp-socket';
import mqtt from 'mqtt-packet';
import { EventEmitter } from 'events';
import { EventEmitter } from 'tseep';

export type Callback = (err?: Error | undefined) => any;

Expand All @@ -20,12 +20,18 @@ export class Client extends EventEmitter {
this.emit('data', packet);
this.emit(packet.cmd, packet);
});
this.parser.on('error', this.emit.bind(this, 'error'));
this.parser.on('error', (err) => {
this.emit('error', err);
});
this.socket.on('data', (data) => {
this.parser.parse(data! as Buffer);
});
this.socket.on('error', this.emit.bind(this, 'error'));
this.socket.on('close', this.emit.bind(this, 'close'));
this.socket.on('error', (err) => {
this.emit('error', err);
});
this.socket.on('close', () => {
this.emit('close');
});
}

setKeepAlive(keepalive: number) {
Expand Down
14 changes: 10 additions & 4 deletions src/simple-mq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
IUnsubscribePacket,
} from 'mqtt-packet';
import type { Buffer } from 'buffer';
import { EventEmitter } from 'events';
import { EventEmitter } from 'tseep';
import { Server, Client } from './';

const TOPIC_PART_LIMIT = 16;
Expand Down Expand Up @@ -77,9 +77,15 @@ export class SimpleMQBroker extends EventEmitter {
this.keepalive = options.keepalive ?? 60;
this.clients = {};
this.server.on('connection', this.handleClient.bind(this));
this.server.on('error', this.emit.bind(this, 'error'));
this.server.on('close', this.emit.bind(this, 'close'));
this.server.on('listening', this.emit.bind(this, 'listening'));
this.server.on('error', (err) => {
this.emit('error', err);
});
this.server.on('close', () => {
this.emit('close');
});
this.server.on('listening', () => {
this.emit('listening');
});
this.sessions = {};
this.nextId = 1000;
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9706,6 +9706,11 @@ ts-node@^10.8.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tseep@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/tseep/-/tseep-1.1.1.tgz#76d333d4a354cbfc627e957e49903cc53f46e17e"
integrity sha512-w2MjaqNWGDeliT5/W+/lhhnR0URiVwXXsXbkAZjQVywrOpdKhQAOL1ycyHfNOV1QBjouC7FawNmJePet1sGesw==

tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz"
Expand Down

0 comments on commit 65454fb

Please sign in to comment.