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

Unsubscribe nested subscriptions #37

Closed
TimoBechtel opened this issue Nov 21, 2022 · 0 comments · Fixed by #74
Closed

Unsubscribe nested subscriptions #37

TimoBechtel opened this issue Nov 21, 2022 · 0 comments · Fixed by #74
Labels
enhancement New feature or request

Comments

@TimoBechtel
Copy link
Owner

TimoBechtel commented Nov 21, 2022

what

Add feature to automatically unsubscribe all nested subscriptions.

why

Currently, unsubscribing nested subscriptions is very verbose and error prone when forgetting to unsubscribe a nested subscribe call.

This is especially annoying when using something like react that needs cleanup.

let cleanupFunctions = [];
const unsubscribeA = db.get("books").each(bookRef => {
  const unsubscribeB = bookRef.get("pages").each(pageRef => {
    const unsubscribeC = pageRef.on(console.log);
    cleanupFunctions.push(unsubscribeC);
  });
  cleanupFunctions.push(unsubscribeB);
});
cleanupFunctions.push(unsubscribeA);

// Cleanup
cleanupFunctions.forEach(fn => fn());
cleanupFunctions = [];

We could make this a lot simpler by allowing to unsubscribe all nested subscriptions:

const unsubscribe = db.get("books").each(bookRef => {
  bookRef.get("pages").each(pageRef => {
    pageRef.on(console.log);
  });
});

// Cleanup 
unsubscribe({ nested: true });

Or we can add a subscriptionContext that automatically unsubscribes every subscription inside the context:

const unsubscribe = db.subscriptionContext(ctx => {
	ctx.get("books").each(bookRef => {
		bookRef.get("pages").each(pageRef => {
			pageRef.on(console.log);
		});
	});
});

This should probably be the default behavior.

We might add a flag to enable this as default behavior until the next major version is released, as this might be a breaking change.

@TimoBechtel TimoBechtel added idea rough idea for a new feature enhancement New feature or request and removed idea rough idea for a new feature labels Nov 21, 2022
@TimoBechtel TimoBechtel added idea rough idea for a new feature and removed enhancement New feature or request labels Apr 2, 2023
@TimoBechtel TimoBechtel added enhancement New feature or request and removed idea rough idea for a new feature labels Jul 13, 2023
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

Successfully merging a pull request may close this issue.

1 participant