Skip to content

Commit

Permalink
refactor!: improve and clean up codebase, refactor typings
Browse files Browse the repository at this point in the history
  • Loading branch information
weyoss committed Mar 8, 2024
1 parent 2d28c41 commit 71cfe06
Show file tree
Hide file tree
Showing 96 changed files with 866 additions and 645 deletions.
28 changes: 11 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@
* in the root directory of this source tree.
*/

export * from './types/index.js';
export * from './src/async/index.js';
export * from './src/common/index.js';
export * from './src/env/index.js';
export * from './src/errors/index.js';
export * from './src/logger/errors/index.js';
export * from './src/timer/errors/index.js';
export * from './src/locker/errors/index.js';
export * from './src/redis-client/errors/index.js';
export * from './src/worker/errors/index.js';
export { Locker } from './src/locker/locker.js';
export { PowerSwitch } from './src/power-switch/power-switch.js';
export { RedisClient } from './src/redis-client/redis-client.js';
export { Timer } from './src/timer/timer.js';
export { Runnable } from './src/runnable/runnable.js';
export { WorkerCallable } from './src/worker/worker-callable.js';
export { WorkerRunnable } from './src/worker/worker-runnable.js';
export { WorkerResourceGroup } from './src/worker/worker-resource-group.js';
export { logger } from './src/logger/logger.js';
export { async } from './src/async/async.js';
export { redis } from './src/redis-client/index.js';
export * from './src/event/index.js';
export * from './src/event-bus/index.js';
export * from './src/locker/index.js';
export * from './src/logger/index.js';
export * from './src/power-switch/index.js';
export * from './src/redis-client/index.js';
export * from './src/runnable/index.js';
export * from './src/timer/index.js';
export * from './src/worker/index.js';
8 changes: 4 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ rm -rf dist

# esm
tsc -p ./tsconfig.json
cp -r src/redis-client/lua dist/esm/src/redis-client/
cp -r src/locker/redis-client dist/esm/src/locker/
cp -r src/redis-client/lua-scripts/scripts dist/esm/src/redis-client/lua-scripts/
cp -r src/locker/redis-client/lua-scripts/scripts dist/esm/src/locker/redis-client/lua-scripts/

# cjs
tsc -p ./tsconfig.cjs.json
Expand All @@ -27,8 +27,8 @@ cat >dist/cjs/package.json <<!EOF
"type": "commonjs"
}
!EOF
cp -r src/redis-client/lua dist/cjs/src/redis-client/
cp -r src/locker/redis-client dist/cjs/src/locker/
cp -r src/redis-client/lua-scripts/scripts dist/cjs/src/redis-client/lua-scripts/
cp -r src/locker/redis-client/lua-scripts/scripts dist/cjs/src/locker/redis-client/lua-scripts/

# types
tsc -p ./tsconfig.types.json
2 changes: 1 addition & 1 deletion src/async/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* in the root directory of this source tree.
*/

import { ICallback, TFunction } from '../../types/index.js';
import { ICallback, TFunction } from '../common/index.js';

const eachOf = <T>(
collection: T[],
Expand Down
10 changes: 10 additions & 0 deletions src/async/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

export * from './async.js';
10 changes: 10 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

export * from './types/index.js';
File renamed without changes.
10 changes: 10 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

export * from './environment.js';
12 changes: 12 additions & 0 deletions src/errors/abort.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { RedisSMQError } from './redis-smq.error.js';

export class AbortError extends RedisSMQError {}
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { CallbackEmptyReplyError } from './callback-empty-reply.error.js';
export { CallbackInvalidReplyError } from './callback-invalid-reply.error.js';
export { PanicError } from './panic.error.js';
export { RedisSMQError } from './redis-smq.error.js';
export { AbortError } from './abort.error.js';
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

import { EventBusError } from './event-bus.error.js';

export class EventBusConnectionError extends EventBusError {}
export class EventBusNotConnectedError extends EventBusError {}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
*/

export { EventBusError } from './event-bus.error.js';
export { EventBusConnectionError } from './event-bus-connection.error.js';
export { EventBusNotConnectedError } from './event-bus-not-connected.error.js';
175 changes: 175 additions & 0 deletions src/event-bus/event-bus-redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { async } from '../async/index.js';
import { ICallback } from '../common/index.js';
import {
createRedisClient,
IRedisConfig,
IRedisClient,
} from '../redis-client/index.js';
import { EventEmitter, IEventBus, TEventBusEvent } from '../event/index.js';
import { EventBusNotConnectedError } from './errors/index.js';

export class EventBusRedis<Events extends TEventBusEvent>
extends EventEmitter<Events>
implements IEventBus<Events>
{
protected connected = false;
protected pubClient;
protected subClient;

protected constructor(pubClient: IRedisClient, subClient: IRedisClient) {
super();

//
this.pubClient = pubClient;
this.pubClient.on('error', (err: Error) => {
this.eventEmitter.emit('error', err);
});

//
this.subClient = subClient;
this.subClient.on('message', (channel: keyof Events, message: string) => {
this.eventEmitter.emit(String(channel), ...JSON.parse(message));
});
this.subClient.on('error', (err: Error) => {
this.eventEmitter.emit('error', err);
});

this.connected = true;
}

override emit<E extends keyof Events>(
event: E,
...args: Parameters<Events[E]>
): boolean {
if (!this.connected) {
this.eventEmitter.emit('error', new EventBusNotConnectedError());
return false;
}
this.pubClient.publish(String(event), JSON.stringify(args), () => void 0);
return true;
}

override on<E extends keyof Events>(event: E, listener: Events[E]): this {
if (event === 'error') {
super.on('error', listener);
return this;
}
if (!this.connected) {
this.eventEmitter.emit('error', new EventBusNotConnectedError());
return this;
}
this.subClient.subscribe(String(event));
super.on(event, listener);
return this;
}

override once<E extends keyof Events>(event: E, listener: Events[E]): this {
if (event === 'error') {
super.once('error', listener);
return this;
}
if (!this.connected) {
this.eventEmitter.emit('error', new EventBusNotConnectedError());
return this;
}
this.subClient.subscribe(String(event));
super.once(event, listener);
return this;
}

override removeAllListeners<E extends keyof Events>(
event?: Extract<E, string>,
): this {
if (event === 'error') {
super.removeAllListeners('error');
return this;
}
if (!this.connected) {
this.eventEmitter.emit('error', new EventBusNotConnectedError());
return this;
}
if (event) this.subClient.unsubscribe(String(event));
else this.subClient.unsubscribe();
super.removeAllListeners(event);
return this;
}

override removeListener<E extends keyof Events>(
event: E,
listener: Events[E],
): this {
if (event === 'error') {
super.removeListener('error', listener);
return this;
}
if (!this.connected) {
this.eventEmitter.emit('error', new EventBusNotConnectedError());
return this;
}
super.removeListener(event, listener);
return this;
}

shutDown(cb: ICallback<void>) {
if (this.connected) {
async.waterfall(
[
(cb: ICallback<void>) => this.subClient.halt(() => cb()),
(cb: ICallback<void>) => this.pubClient.halt(() => cb()),
],
() => {
this.connected = false;
cb();
},
);
} else cb();
}

static createInstance<T extends TEventBusEvent>(
config: IRedisConfig,
cb: ICallback<IEventBus<T>>,
): void {
let pubClient: IRedisClient | null | undefined = null;
let subClient: IRedisClient | null | undefined = null;
async.waterfall(
[
(cb: ICallback<void>) =>
createRedisClient(config, (err, client) => {
if (err) cb(err);
else {
pubClient = client;
cb();
}
}),
(cb: ICallback<void>) =>
createRedisClient(config, (err, client) => {
if (err) cb(err);
else {
subClient = client;
cb();
}
}),
],
(err) => {
if (err) {
if (pubClient) pubClient.halt(() => void 0);
if (subClient) subClient.halt(() => void 0);
cb(err);
} else if (!pubClient || !subClient) cb(new Error());
else {
const instance = new EventBusRedis<T>(pubClient, subClient);
cb(null, instance);
}
},
);
}
}

0 comments on commit 71cfe06

Please sign in to comment.