feat(native): NotifyEvent types

This commit is contained in:
LongYinan
2023-05-10 18:06:26 +08:00
parent e54a5b6128
commit 8c84daec2b
5 changed files with 50 additions and 8 deletions

View File

@@ -57,7 +57,7 @@ test('fs watch', { concurrency: false }, async t => {
const defer = new Subject<void>(); const defer = new Subject<void>();
const subscription = watcher.subscribe( const subscription = watcher.subscribe(
event => { event => {
if (event.type.remove) { if (typeof event.type === 'object' && 'rename' in event.type) {
assert.deepEqual(event.paths, [fixture]); assert.deepEqual(event.paths, [fixture]);
assert.deepEqual(event.type, { assert.deepEqual(event.type, {
remove: { remove: {

43
packages/native/event.d.ts vendored Normal file
View File

@@ -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';
};
};

View File

@@ -27,6 +27,7 @@ export function watch(
options?: WatchOptions | undefined | null options?: WatchOptions | undefined | null
): FSWatcher; ): FSWatcher;
export class Subscription { export class Subscription {
toString(): string;
unsubscribe(): void; unsubscribe(): void;
} }
export type FSWatcher = FsWatcher; export type FSWatcher = FsWatcher;
@@ -34,11 +35,8 @@ export class FsWatcher {
get kind(): WatcherKind; get kind(): WatcherKind;
toString(): string; toString(): string;
subscribe( subscribe(
callback: (value: any) => any, callback: (event: import('./event').NotifyEvent) => void,
errorCallback?: ( errorCallback?: (err: Error) => void
err: Error | null,
value: undefined
) => any | undefined | null
): Subscription; ): Subscription;
close(): void; close(): void;
} }

View File

@@ -32,7 +32,7 @@
"build": "napi build --platform --release", "build": "napi build --platform --release",
"build:debug": "napi build --platform", "build:debug": "napi build --platform",
"universal": "napi universal", "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": "napi version"
}, },
"version": "0.6.0-canary.0" "version": "0.6.0-canary.0"

View File

@@ -122,8 +122,9 @@ impl FSWatcher {
#[napi] #[napi]
pub fn subscribe( pub fn subscribe(
&mut self, &mut self,
#[napi(ts_arg_type = "(event: import('./event').NotifyEvent) => void")]
callback: ThreadsafeFunction<serde_json::Value, ErrorStrategy::Fatal>, callback: ThreadsafeFunction<serde_json::Value, ErrorStrategy::Fatal>,
error_callback: Option<ThreadsafeFunction<()>>, #[napi(ts_arg_type = "(err: Error) => void")] error_callback: Option<ThreadsafeFunction<()>>,
) -> Subscription { ) -> Subscription {
let uuid = uuid::Uuid::new_v4(); let uuid = uuid::Uuid::new_v4();
let mut event_emitter = self.event_emitter.lock(); let mut event_emitter = self.event_emitter.lock();