Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch features #6

Open
dr-vortex opened this issue Oct 25, 2023 · 3 comments
Open

Watch features #6

dr-vortex opened this issue Oct 25, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@dr-vortex
Copy link
Member

dr-vortex commented Oct 25, 2023

Originally jvilk/BrowserFS#163

By @amir-arad

@dr-vortex dr-vortex added the enhancement New feature or request label Oct 25, 2023
@dr-vortex dr-vortex added this to the 1.0 milestone Oct 25, 2023
@dr-vortex dr-vortex self-assigned this Oct 25, 2023
@dr-vortex dr-vortex added feature request enhancement New feature or request and removed enhancement New feature or request feature request labels Oct 25, 2023
@dr-vortex
Copy link
Member Author

Closing this since it is part of #24

@dr-vortex
Copy link
Member Author

Reopening since this will be done separately from #24, due to the internal API changes that will be needed.

@dr-vortex dr-vortex reopened this Apr 22, 2024
@dr-vortex dr-vortex changed the title filesystem watch Watch features Apr 23, 2024
@dr-vortex
Copy link
Member Author

dr-vortex commented Apr 23, 2024

Noting my thoughts on how the watch features can be implemented, keeping in mind that the goal is to implement the various watch functions:

File changes must be detected and an event dispatched to the listener passed to a watch function.

One easy way to do this would be to have FileSystem extend EventEmitter. Then, some events could be defined.

Perhaps:

interface FileSystemEvent {
	path: string;
	// more can be added
}

abstract class FileSystem extends EventEmitter<Record<'change' | 'rename' | 'create' | 'delete', FileSystemEvent>> {
	//...
}

// Then, an implementation can emit the event

class SomeFS extends FileSystem {
	//...
	async rename(path, /*...*/) {
		this.emit('rename', { path });
		//...
	}
	//...
}

// Finally, the watch functions can listen for these events

interface Watcher {
	path: string
	listener: WatchListener
}

const watchers: Set<Watcher>

function watch(path, listener) {
	watchers.add({ path, listener })
}

// And they will be caught

function watchListener(eventType, event) {
	for(const { path, listener } of watchers) {
		if(event.path != path) { // or some other way to match it
			continue;
		}
		
		listener({
			eventType,
			filename: path
		});
	}
}

function mount(point, fs) {
	//...
	fs.on('change', (event) => watchListener('change', event));
	fs.on('rename', (event) => watchListener('change', event));
	//...
}

@dr-vortex dr-vortex pinned this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant