From 8c84daec2bd5dbe739e8855669fda6fb07106dd7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 10 May 2023 18:06:26 +0800 Subject: [PATCH] feat(native): NotifyEvent types --- packages/native/__tests__/fs.spec.mts | 2 +- packages/native/event.d.ts | 43 +++++++++++++++++++++++++++ packages/native/index.d.ts | 8 ++--- packages/native/package.json | 2 +- packages/native/src/fs.rs | 3 +- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 packages/native/event.d.ts diff --git a/packages/native/__tests__/fs.spec.mts b/packages/native/__tests__/fs.spec.mts index 463eb8d081..13e68d3020 100644 --- a/packages/native/__tests__/fs.spec.mts +++ b/packages/native/__tests__/fs.spec.mts @@ -57,7 +57,7 @@ test('fs watch', { concurrency: false }, async t => { const defer = new Subject(); const subscription = watcher.subscribe( event => { - if (event.type.remove) { + if (typeof event.type === 'object' && 'rename' in event.type) { assert.deepEqual(event.paths, [fixture]); assert.deepEqual(event.type, { remove: { diff --git a/packages/native/event.d.ts b/packages/native/event.d.ts new file mode 100644 index 0000000000..b7ba335486 --- /dev/null +++ b/packages/native/event.d.ts @@ -0,0 +1,43 @@ +export interface NotifyEvent { + type: EventKind; + paths: string[]; +} + +export type EventKind = + | 'any' + | 'other' + | { + remove: { + kind: 'any' | 'file' | 'folder' | 'other'; + }; + } + | { + create: { + kind: 'any' | 'file' | 'folder' | 'other'; + }; + } + | { + modify: + | { + kind: 'any' | 'other'; + } + | { + kind: 'data'; + mode: 'any' | 'size' | 'content' | 'other'; + } + | { + kind: 'metadata'; + mode: + | 'any' + | 'access-time' + | 'write-time' + | 'permissions' + | 'ownership' + | 'extended' + | 'other'; + } + | { + kind: 'rename'; + mode: 'any' | 'to' | 'from' | 'both' | 'other'; + }; + }; diff --git a/packages/native/index.d.ts b/packages/native/index.d.ts index 5ab29c2023..711134bf3e 100644 --- a/packages/native/index.d.ts +++ b/packages/native/index.d.ts @@ -27,6 +27,7 @@ export function watch( options?: WatchOptions | undefined | null ): FSWatcher; export class Subscription { + toString(): string; unsubscribe(): void; } export type FSWatcher = FsWatcher; @@ -34,11 +35,8 @@ export class FsWatcher { get kind(): WatcherKind; toString(): string; subscribe( - callback: (value: any) => any, - errorCallback?: ( - err: Error | null, - value: undefined - ) => any | undefined | null + callback: (event: import('./event').NotifyEvent) => void, + errorCallback?: (err: Error) => void ): Subscription; close(): void; } diff --git a/packages/native/package.json b/packages/native/package.json index d694f971d1..5cb90bc3f3 100644 --- a/packages/native/package.json +++ b/packages/native/package.json @@ -32,7 +32,7 @@ "build": "napi build --platform --release", "build:debug": "napi build --platform", "universal": "napi universal", - "test": "cross-env TS_NODE_PROJECT=./tsconfig.json node --test --loader ts-node/esm --experimental-specifier-resolution=node ./__tests__/**/*.mts", + "test": "cross-env TS_NODE_TRANSPILE_ONLY=1 TS_NODE_PROJECT=./tsconfig.json node --test --loader ts-node/esm --experimental-specifier-resolution=node ./__tests__/**/*.mts", "version": "napi version" }, "version": "0.6.0-canary.0" diff --git a/packages/native/src/fs.rs b/packages/native/src/fs.rs index f1b6f91332..0c31ff99a6 100644 --- a/packages/native/src/fs.rs +++ b/packages/native/src/fs.rs @@ -122,8 +122,9 @@ impl FSWatcher { #[napi] pub fn subscribe( &mut self, + #[napi(ts_arg_type = "(event: import('./event').NotifyEvent) => void")] callback: ThreadsafeFunction, - error_callback: Option>, + #[napi(ts_arg_type = "(err: Error) => void")] error_callback: Option>, ) -> Subscription { let uuid = uuid::Uuid::new_v4(); let mut event_emitter = self.event_emitter.lock();