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 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: {

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
): 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;
}

View File

@ -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"

View File

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