Skip to content

Commit

Permalink
fix: rename interfaces by dropping prefix I
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 14, 2022
1 parent df9330b commit d1d9dba
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 136 deletions.
52 changes: 29 additions & 23 deletions lib/DataHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetStream, CommandItem, ICommand } from "./types";
import { NetStream, CommandItem, Respondable } from "./types";
import Deque = require("denque");
import { EventEmitter } from "events";
import Command from "./command";
Expand All @@ -10,35 +10,30 @@ const debug = Debug("dataHandler");

type ReplyData = string | Buffer | number | Array<string | Buffer | number>;

export interface IDataHandlerOptions {
stringNumbers: boolean;
dropBufferSupport: boolean;
}

interface ICondition {
interface Condition {
select: number;
auth: string;
subscriber: false | SubscriptionSet;
}

interface IDataHandledable extends EventEmitter {
interface DataHandledable extends EventEmitter {
stream: NetStream;
status: string;
condition: ICondition;
condition: Condition;
commandQueue: Deque<CommandItem>;

disconnect(reconnect: boolean): void;
recoverFromFatalError(commandError: Error, err: Error, options: any): void;
handleReconnection(err: Error, item: CommandItem): void;
}

interface IParserOptions {
interface ParserOptions {
stringNumbers: boolean;
dropBufferSupport: boolean;
}

export default class DataHandler {
constructor(private redis: IDataHandledable, parserOptions: IParserOptions) {
constructor(private redis: DataHandledable, parserOptions: ParserOptions) {
const parser = new RedisParser({
stringNumbers: parserOptions.stringNumbers,
returnBuffers: !parserOptions.dropBufferSupport,
Expand Down Expand Up @@ -230,32 +225,43 @@ export default class DataHandler {
}
}

function fillSubCommand(command: ICommand, count: number) {
// TODO: use WeakMap here
if (typeof (command as any).remainReplies === "undefined") {
(command as any).remainReplies = command.args.length;
}
if (--(command as any).remainReplies === 0) {
const remainingRepliesMap = new WeakMap<Respondable, number>();

function fillSubCommand(command: Respondable, count: number) {
let remainingReplies = remainingRepliesMap.has(command)
? remainingRepliesMap.get(command)
: command.args.length;

remainingReplies -= 1;

if (remainingReplies <= 0) {
command.resolve(count);
remainingRepliesMap.delete(command);
return true;
}
remainingRepliesMap.set(command, remainingReplies);
return false;
}

function fillUnsubCommand(command: ICommand, count: number) {
if (typeof (command as any).remainReplies === "undefined") {
(command as any).remainReplies = command.args.length;
}
if ((command as any).remainReplies === 0) {
function fillUnsubCommand(command: Respondable, count: number) {
let remainingReplies = remainingRepliesMap.has(command)
? remainingRepliesMap.get(command)
: command.args.length;

if (remainingReplies === 0) {
if (count === 0) {
remainingRepliesMap.delete(command);
command.resolve(count);
return true;
}
return false;
}
if (--(command as any).remainReplies === 0) {

remainingReplies -= 1;
if (remainingReplies <= 0) {
command.resolve(count);
return true;
}
remainingRepliesMap.set(command, remainingReplies);
return false;
}
12 changes: 3 additions & 9 deletions lib/ScanStream.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Readable, ReadableOptions } from "stream";
/**
* Options for ScanStream
*
* @export
* @interface IScanStreamOptions
* @extends {ReadableOptions}
*/
export interface IScanStreamOptions extends ReadableOptions {

export interface ScanStreamOptions extends ReadableOptions {
key?: string;
match?: string;
type?: string;
Expand All @@ -26,7 +20,7 @@ export default class ScanStream extends Readable {
private _redisCursor = "0";
private _redisDrained = false;

constructor(private opt: IScanStreamOptions) {
constructor(private opt: ScanStreamOptions) {
super(opt);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/SubscriptionSet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ICommandNameFlags } from "./command";
import { CommandNameFlags } from "./command";

type AddSet = ICommandNameFlags["ENTER_SUBSCRIBER_MODE"][number];
type DelSet = ICommandNameFlags["EXIT_SUBSCRIBER_MODE"][number];
type AddSet = CommandNameFlags["ENTER_SUBSCRIBER_MODE"][number];
type DelSet = CommandNameFlags["EXIT_SUBSCRIBER_MODE"][number];

/**
* Tiny class to simplify dealing with subscription set
Expand Down
4 changes: 2 additions & 2 deletions lib/cluster/ClusterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type DNSLookupFunction = (
family?: number
) => void
) => void;
export interface INatMap {
export interface NatMap {
[key: string]: { host: string; port: number };
}

Expand Down Expand Up @@ -166,7 +166,7 @@ export interface ClusterOptions extends CommanderOptions {
* @default require('dns').lookup
*/
dnsLookup?: DNSLookupFunction;
natMap?: INatMap;
natMap?: NatMap;

/**
* See Redis class.
Expand Down
12 changes: 3 additions & 9 deletions lib/cluster/DelayQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import Deque = require("denque");

const debug = Debug("delayqueue");

/**
* Options for DelayQueue
*
* @export
* @interface IDelayQueueOptions
*/
export interface IDelayQueueOptions {
export interface DelayQueueOptions {
callback?: Function;
timeout: number;
}
Expand All @@ -29,13 +23,13 @@ export default class DelayQueue {
*
* @param {string} bucket bucket name
* @param {Function} item function that will run later
* @param {IDelayQueueOptions} options
* @param {DelayQueueOptions} options
* @memberof DelayQueue
*/
public push(
bucket: string,
item: Function,
options: IDelayQueueOptions
options: DelayQueueOptions
): void {
const callback = options.callback || process.nextTick;
if (!this.queues[bucket]) {
Expand Down
10 changes: 5 additions & 5 deletions lib/cluster/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export interface RedisOptions {
[key: string]: any;
}

export interface ISrvRecordsGroup {
export interface SrvRecordsGroup {
totalWeight: number;
records: SrvRecord[];
}

export interface IGroupedSrvRecords {
[key: number]: ISrvRecordsGroup;
export interface GroupedSrvRecords {
[key: number]: SrvRecordsGroup;
}

export function getNodeKey(node: RedisOptions): NodeKey {
Expand Down Expand Up @@ -80,7 +80,7 @@ export function getUniqueHostnamesFromOptions(nodes: RedisOptions[]): string[] {
return Object.keys(uniqueHostsMap).filter((host) => !isIP(host));
}

export function groupSrvRecords(records: SrvRecord[]): IGroupedSrvRecords {
export function groupSrvRecords(records: SrvRecord[]): GroupedSrvRecords {
const recordsByPriority = {};
for (const record of records) {
if (!recordsByPriority.hasOwnProperty(record.priority)) {
Expand All @@ -97,7 +97,7 @@ export function groupSrvRecords(records: SrvRecord[]): IGroupedSrvRecords {
return recordsByPriority;
}

export function weightSrvRecords(recordsGroup: ISrvRecordsGroup): SrvRecord {
export function weightSrvRecords(recordsGroup: SrvRecordsGroup): SrvRecord {
if (recordsGroup.records.length === 1) {
recordsGroup.totalWeight = 0;
return recordsGroup.records.shift();
Expand Down
29 changes: 13 additions & 16 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import {
convertMapToArray,
convertObjectToArray,
} from "./utils";
import { CallbackFunction, ICommand, CommandParameter } from "./types";
import { CallbackFunction, Respondable, CommandParameter } from "./types";

export type ArgumentType =
| string
| Buffer
| number
| (string | Buffer | number | any[])[];

interface ICommandOptions {
interface CommandOptions {
/**
* Set the encoding of the reply, by default buffer will be returned.
*
* @type {(string | null)}
* @memberof ICommandOptions
*/
replyEncoding?: BufferEncoding | null;
errorStack?: Error;
Expand All @@ -34,11 +31,11 @@ interface ICommandOptions {

type ArgumentTransformer = (args: any[]) => any[];
type ReplyTransformer = (reply: any) => any;
interface IFlagMap {
interface FlagMap {
[flag: string]: { [command: string]: true };
}

export interface ICommandNameFlags {
export interface CommandNameFlags {
// Commands that can be processed when client is in the subscriber mode
VALID_IN_SUBSCRIBER_MODE: [
"subscribe",
Expand Down Expand Up @@ -83,9 +80,9 @@ export interface ICommandNameFlags {
* ```
* @see {@link Redis#sendCommand} which can send a Command instance to Redis
*/
export default class Command implements ICommand {
export default class Command implements Respondable {
public static FLAGS: {
[key in keyof ICommandNameFlags]: ICommandNameFlags[key];
[key in keyof CommandNameFlags]: CommandNameFlags[key];
} = {
VALID_IN_SUBSCRIBER_MODE: [
"subscribe",
Expand All @@ -101,12 +98,12 @@ export default class Command implements ICommand {
WILL_DISCONNECT: ["quit"],
};

private static flagMap?: IFlagMap;
private static flagMap?: FlagMap;

private static getFlagMap(): IFlagMap {
private static getFlagMap(): FlagMap {
if (!this.flagMap) {
this.flagMap = Object.keys(Command.FLAGS).reduce(
(map: IFlagMap, flagName: string) => {
(map: FlagMap, flagName: string) => {
map[flagName] = {};
Command.FLAGS[flagName].forEach((commandName: string) => {
map[flagName][commandName] = true;
Expand All @@ -126,10 +123,10 @@ export default class Command implements ICommand {
* @param {string} commandName
* @return {boolean}
*/
public static checkFlag<T extends keyof ICommandNameFlags>(
public static checkFlag<T extends keyof CommandNameFlags>(
flagName: T,
commandName: string
): commandName is ICommandNameFlags[T][number] {
): commandName is CommandNameFlags[T][number] {
return !!this.getFlagMap()[flagName][commandName];
}

Expand Down Expand Up @@ -177,15 +174,15 @@ export default class Command implements ICommand {
* Creates an instance of Command.
* @param {string} name Command name
* @param {(Array<string | Buffer | number>)} [args=[]] An array of command arguments
* @param {ICommandOptions} [options={}]
* @param {CommandOptions} [options={}]
* @param {CallbackFunction} [callback] The callback that handles the response.
* If omit, the response will be handled via Promise
* @memberof Command
*/
constructor(
public name: string,
args: Array<ArgumentType> = [],
options: ICommandOptions = {},
options: CommandOptions = {},
callback?: CallbackFunction
) {
this.replyEncoding = options.replyEncoding;
Expand Down
6 changes: 3 additions & 3 deletions lib/connectors/SentinelConnector/FailoverDetector.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Debug } from "../../utils";
import SentinelConnector from "./index";
import { ISentinel } from "./types";
import { Sentinel } from "./types";

const debug = Debug("FailoverDetector");

const CHANNEL_NAME = "+switch-master";

export class FailoverDetector {
private connector: SentinelConnector;
private sentinels: ISentinel[];
private sentinels: Sentinel[];
private isDisconnected = false;

// sentinels can't be used for regular commands after this
constructor(connector: SentinelConnector, sentinels: ISentinel[]) {
constructor(connector: SentinelConnector, sentinels: Sentinel[]) {
this.connector = connector;
this.sentinels = sentinels;
}
Expand Down
15 changes: 8 additions & 7 deletions lib/connectors/SentinelConnector/SentinelIterator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ISentinelAddress } from "./types";
import { SentinelAddress } from "./types";

function isSentinelEql(
a: Partial<ISentinelAddress>,
b: Partial<ISentinelAddress>
a: Partial<SentinelAddress>,
b: Partial<SentinelAddress>
): boolean {
return (
(a.host || "127.0.0.1") === (b.host || "127.0.0.1") &&
Expand All @@ -11,11 +11,12 @@ function isSentinelEql(
}

export default class SentinelIterator
implements Iterator<Partial<ISentinelAddress>> {
implements Iterator<Partial<SentinelAddress>>
{
private cursor = 0;
private sentinels: Array<Partial<ISentinelAddress>>;
private sentinels: Array<Partial<SentinelAddress>>;

constructor(sentinels: Array<Partial<ISentinelAddress>>) {
constructor(sentinels: Array<Partial<SentinelAddress>>) {
this.sentinels = sentinels.slice(0);
}

Expand All @@ -35,7 +36,7 @@ export default class SentinelIterator
this.cursor = 0;
}

add(sentinel: ISentinelAddress): boolean {
add(sentinel: SentinelAddress): boolean {
for (let i = 0; i < this.sentinels.length; i++) {
if (isSentinelEql(sentinel, this.sentinels[i])) {
return false;
Expand Down
Loading

0 comments on commit d1d9dba

Please sign in to comment.