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

feat: Iterable RPC calls. #47

Closed
saul-jb opened this issue Jun 12, 2023 · 2 comments
Closed

feat: Iterable RPC calls. #47

saul-jb opened this issue Jun 12, 2023 · 2 comments

Comments

@saul-jb
Copy link
Contributor

saul-jb commented Jun 12, 2023

Would you be interested in a PR for adding iterable RPC methods?

I have just been calling them tasks (Perhaps there is a better name?) but they are just build on top of the existing API and therefore I can see that it is questionable if it should be a part of this library or not.

This would look like the following:

rpc.addTask("async-task", () => {
	return (async function* () {
		for (const value of [1, 2, 3, 4]) {
			yield value;
			await new Promise(resolve => setTimeout(resolve, 500));
		}
	})();
});

for await (const value of rpc.runTask("async-task", {})) {
	console.log(value);
}
@shogowada
Copy link
Owner

Thank you for sharing the idea! Hum but yeah I think it makes more sense as an extension of this library because it seems the use case is uncommon (I think returning an array suffices the most use cases) and it's outside of JSON-RPC specs.

@saul-jb
Copy link
Contributor Author

saul-jb commented Jun 12, 2023

Thanks for considering it.

it seems the use case is uncommon

I don't think the use case is uncommon - it is essentially just a stream which is obviously needed for a lot of things but it allows running multiple simultaneous streams inside the same RPC protocol.

Perhaps my example was a bit basic there but a more concrete example would be returning a file stream which itself is iterable and hence would allow the file to converted back to a stream easily on the remote end.

Here is a simple example of downloading a file from a server:

// Remote
rpc.addTask("read-file", ({ path }) => {
	return fs.createReadStream(path);
});

// Local
// (This could be simplified further using streaming iterables.) 
const stream = fs.createWriteStream("file.txt");

for await (const value of rpc.runTask("read-file", { path: "/remote/file.txt" })) {
	stream.write(value);
}

it's outside of JSON-RPC specs

It would unfortunately have to fall under system extensions. Regardless I will make an extension that provides this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants