Skip to content

Commit

Permalink
feat: batch notification events (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Oct 22, 2023
1 parent 9b23446 commit 25ea737
Show file tree
Hide file tree
Showing 7 changed files with 814 additions and 1,116 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.github/ export-ignore
.gitignore export-ignore
pyrightconfig.json export-ignore
tox.ini export-ignore
37 changes: 7 additions & 30 deletions chokidar/chokidar-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,7 @@ const readline = require('readline');
const debounce = require('lodash.debounce');
const chokidar = require('chokidar');

/**
* @typedef {'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'} ChokidarEventType
* @typedef {'create' | 'change' | 'delete'} LspEventType
*
* @typedef {{
* cwd: string
* debounceChanges: number
* debug: boolean
* events: LspEventType[]
* followSymlinks: boolean
* ignores: string[] | null
* initial: boolean
* patterns: string[]
* polling: boolean
* pollInterval: number
* pollIntervalBinary: number
* uid: number
* }} RegisterWatcherOptions
*
* @typedef {{
* register?: RegisterWatcherOptions
* unregister?: RegisterWatcherOptions['uid']
* }} InputCommand
*/

/** @type {Record<ChokidarEventType, LspEventType | null>} */
/** @type {Record<ChokidarCli.ChokidarEventType, ChokidarCli.LspEventType | null>} */
const CHOKIDAR_EVENT_TYPE_TO_LSP = {
add: 'create',
addDir: null,
Expand All @@ -39,7 +14,7 @@ const CHOKIDAR_EVENT_TYPE_TO_LSP = {
unlinkDir: null,
};

/** @type {Partial<RegisterWatcherOptions>} */
/** @type {Partial<ChokidarCli.RegisterWatcherOptions>} */
const defaultOpts = {
debounceChanges: 400,
debug: false,
Expand Down Expand Up @@ -77,7 +52,7 @@ function handleInput(line) {
return;
}

/** @type {InputCommand} */
/** @type {ChokidarCli.InputCommand} */
let data;

try {
Expand Down Expand Up @@ -113,7 +88,7 @@ function handleInput(line) {
}
}

/** @param {RegisterWatcherOptions} opts */
/** @param {ChokidarCli.RegisterWatcherOptions} opts */
function registerWatcher(opts) {
const chokidarOpts = createChokidarOpts(opts);
const watcher = chokidar.watch(opts.patterns, chokidarOpts);
Expand All @@ -130,6 +105,7 @@ function registerWatcher(opts) {
function reportDebouncedChanges() {
if (debouncedChangesQueue.length) {
console.log(debouncedChangesQueue.join('\n'));
console.log('<flush>');
debouncedChangesQueue = [];
}
}
Expand All @@ -151,6 +127,7 @@ function registerWatcher(opts) {
debouncedChangesRun();
} else {
console.log(eventString);
console.log('<flush>');
}
});

Expand All @@ -168,7 +145,7 @@ function registerWatcher(opts) {
}

/**
* @param {RegisterWatcherOptions} opts
* @param {ChokidarCli.RegisterWatcherOptions} opts
* @return {chokidar.WatchOptions}
*/
function createChokidarOpts(opts) {
Expand Down
24 changes: 24 additions & 0 deletions chokidar/chokidar-cli/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
declare namespace ChokidarCli {
type ChokidarEventType = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'

type InputCommand = {
register?: RegisterWatcherOptions
unregister?: RegisterWatcherOptions['uid']
}
type LspEventType = 'create' | 'change' | 'delete'

type RegisterWatcherOptions = {
cwd: string
debounceChanges: number
debug: boolean
events: LspEventType[]
followSymlinks: boolean
ignores: string[] | null
initial: boolean
patterns: string[]
polling: boolean
pollInterval: number
pollIntervalBinary: number
uid: number
}
}
Loading

0 comments on commit 25ea737

Please sign in to comment.