Skip to content

Commit

Permalink
improve internal types a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Sep 20, 2024
1 parent 4242091 commit 856d5f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 14 additions & 14 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const STR_DATA = 'data';
export const STR_END = 'end';
export const STR_CLOSE = 'close';
export const EMPTY_FN = () => {};
export const IDENTITY_FN = (val: any) => val;
export const IDENTITY_FN = (val: unknown) => val;

const pl = process.platform;
export const isWindows = pl === 'win32';
Expand Down Expand Up @@ -82,23 +82,23 @@ const isBinaryPath = (filePath: string) =>
binaryExtensions.has(sysPath.extname(filePath).slice(1).toLowerCase());

// TODO: emit errors properly. Example: EMFILE on Macos.
const foreach = (val: any, fn: any) => {
const foreach = <V extends unknown>(val: V, fn: (arg: V) => unknown) => {
if (val instanceof Set) {
val.forEach(fn);
} else {
fn(val);
}
};

const addAndConvert = (main: any, prop: any, item: any) => {
let container = main[prop];
const addAndConvert = (main: Record<string, unknown>, prop: string, item: unknown) => {
let container = (main as Record<string, unknown>)[prop];
if (!(container instanceof Set)) {
main[prop] = container = new Set([container]);
(main as Record<string, unknown>)[prop] = container = new Set([container]);
}
container.add(item);
(container as Set<unknown>).add(item);
};

const clearItem = (cont: any) => (key: string) => {
const clearItem = (cont: Record<string, unknown>) => (key: string) => {
const set = cont[key];
if (set instanceof Set) {
set.clear();
Expand All @@ -116,7 +116,7 @@ const delFromSet = (main: any, prop: any, item: any) => {
}
};

const isEmptySet = (val: any) => (val instanceof Set ? val.size === 0 : !val);
const isEmptySet = (val: unknown) => (val instanceof Set ? val.size === 0 : !val);

// fs_watch helpers

Expand Down Expand Up @@ -180,9 +180,9 @@ function createFsWatchInstance(
const fsWatchBroadcast = (
fullPath: Path,
listenerType: string,
val1?: any,
val2?: any,
val3?: any
val1?: unknown,
val2?: unknown,
val3?: unknown
) => {
const cont = FsWatchInstances.get(fullPath);
if (!cont) return;
Expand Down Expand Up @@ -283,7 +283,7 @@ const setFsWatchListener = (

// object to hold per-process fs_watchFile instances
// (may be shared across chokidar FSWatcher instances)
const FsWatchFileInstances = new Map();
const FsWatchFileInstances = new Map<string, any>();

/**
* Instantiates the fs_watchFile interface or binds listeners
Expand Down Expand Up @@ -330,12 +330,12 @@ const setFsWatchFileListener = (
rawEmitters: rawEmitter,
options,
watcher: watchFile(fullPath, options, (curr, prev) => {
foreach(cont.rawEmitters, (rawEmitter: any) => {
foreach(cont.rawEmitters, (rawEmitter) => {
rawEmitter(EV.CHANGE, fullPath, { curr, prev });
});
const currmtime = curr.mtimeMs;
if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
foreach(cont.listeners, (listener: any) => listener(path, curr));
foreach(cont.listeners, (listener) => listener(path, curr));
}
}),
};
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ const normalizePathToUnix = (path: Path) => toUnix(sysPath.normalize(toUnix(path
// TODO: refactor
const normalizeIgnored =
(cwd = '') =>
(path: any): any => {
(path: unknown): string => {
if (typeof path === 'string') {
return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
} else {
return path;
return path as string;
}
};

Expand All @@ -207,10 +207,10 @@ const EMPTY_SET = Object.freeze(new Set<string>());
*/
class DirEntry {
path: Path;
_removeWatcher: any;
_removeWatcher: (dir: string, base: string) => void;
items: Set<Path>;

constructor(dir: Path, removeWatcher: any) {
constructor(dir: Path, removeWatcher: (dir: string, base: string) => void) {
this.path = dir;
this._removeWatcher = removeWatcher;
this.items = new Set<Path>();
Expand Down Expand Up @@ -270,7 +270,7 @@ export class WatchHelper {
followSymlinks: boolean;
statMethod: 'stat' | 'lstat';

constructor(path: string, follow: boolean, fsw: any) {
constructor(path: string, follow: boolean, fsw: FSWatcher) {
this.fsw = fsw;
const watchPath = path;
this.path = path = path.replace(REPLACER_RE, '');
Expand Down

0 comments on commit 856d5f3

Please sign in to comment.