Skip to content

Commit

Permalink
fixed fmt errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ardabeyazoglu committed Dec 7, 2024
1 parent 1588f02 commit 0247e82
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
50 changes: 37 additions & 13 deletions connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ export interface Connection {
close(): void;
connect(): Promise<void>;
reconnect(): Promise<void>;
on<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void): void;
once<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void): void;
on<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
): void;
once<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
): void;
sendCommand(
command: string,
args?: Array<RedisValue>,
Expand Down Expand Up @@ -85,10 +91,16 @@ export interface RedisConnectionOptions {

export const kEmptyRedisArgs: Array<RedisValue> = [];

export type ConnectionEventType = "error" | "connect" | "reconnecting" | "ready" | "close" | "end";
export type ConnectionEventArg<T extends ConnectionEventType> =
T extends "error" ? Error :
T extends "reconnecting" ? number
export type ConnectionEventType =
| "error"
| "connect"
| "reconnecting"
| "ready"
| "close"
| "end";
export type ConnectionEventArg<T extends ConnectionEventType> = T extends
"error" ? Error
: T extends "reconnecting" ? number
: undefined;

interface PendingCommand {
Expand All @@ -111,8 +123,11 @@ export class RedisConnection implements Connection {
#conn!: Deno.Conn;
#protocol!: Protocol;

private events: {
[K in ConnectionEventType]?: Map<(arg: ConnectionEventArg<K>) => void, string>
private events: {
[K in ConnectionEventType]?: Map<
(arg: ConnectionEventArg<K>) => void,
string
>;
} = {};

get isClosed(): boolean {
Expand Down Expand Up @@ -238,7 +253,7 @@ export class RedisConnection implements Connection {
this.#conn = conn;
this.#protocol = this.options?.[kUnstableCreateProtocol]?.(conn) ??
new DenoStreamsProtocol(conn);

this._isClosed = false;
this._isConnected = true;
this.fireEvent("connect", undefined);
Expand Down Expand Up @@ -284,7 +299,7 @@ export class RedisConnection implements Connection {

#close(canReconnect = false) {
const isClosedAlready = this._isClosed;

this._isClosed = true;
this._isConnected = false;
try {
Expand Down Expand Up @@ -383,7 +398,10 @@ export class RedisConnection implements Connection {
setTimeout(ping, healthCheckInterval);
}

private fireEvent<T extends ConnectionEventType>(eventType: T, eventArg: ConnectionEventArg<T>) {
private fireEvent<T extends ConnectionEventType>(
eventType: T,
eventArg: ConnectionEventArg<T>,
) {
const callbacks = this.events[eventType];
if (callbacks !== undefined && callbacks.size > 0) {
for (const [fn, mode] of callbacks) {
Expand All @@ -395,14 +413,20 @@ export class RedisConnection implements Connection {
}
}

on<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void) {
on<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
) {
if (this.events[eventType] === undefined) {
this.events[eventType] = new Map();
}
this.events[eventType].set(callback, "on");
}

once<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void) {
once<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
) {
if (this.events[eventType] === undefined) {
this.events[eventType] = new Map();
}
Expand Down
17 changes: 14 additions & 3 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ import type {
ZUnionstoreOpts,
} from "./command.ts";
import { RedisConnection } from "./connection.ts";
import type { Connection, ConnectionEventArg, ConnectionEventType, SendCommandOptions } from "./connection.ts";
import type {
Connection,
ConnectionEventArg,
ConnectionEventType,
SendCommandOptions,
} from "./connection.ts";
import type { RedisConnectionOptions } from "./connection.ts";
import type { CommandExecutor } from "./executor.ts";
import { DefaultExecutor } from "./executor.ts";
Expand Down Expand Up @@ -159,11 +164,17 @@ class RedisImpl implements Redis {
return this.close();
}

on<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void) {
on<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
) {
this.executor.connection.on(eventType, callback);
}

once<T extends ConnectionEventType>(eventType: T, callback: (_: ConnectionEventArg<T>) => void) {
once<T extends ConnectionEventType>(
eventType: T,
callback: (_: ConnectionEventArg<T>) => void,
) {
this.executor.connection.once(eventType, callback);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function connectionTests(
readyEventFired = true;
readyEventFiredTimes++;
});

client.on("close", () => {
closeEventFired = true;
});
Expand Down

0 comments on commit 0247e82

Please sign in to comment.