# Side Effect Isolation
The [signal](./signals.md) and [lifecycle](./lifecycle.md) APIs can cause side effects such as tracking accesses or registering a teardown hook.

The `isolate` function can be used to run arbitrary code in isolation from access tracking and the current lifecycle.

=== "JSX"
	```jsx
	import { isolate } from "rvx";

	isolate(() => {
		// Some code with side effects...
	});
	```

=== "No Build"
	```jsx
	import { isolate } from "./rvx.js";

	isolate(() => {
		// Some code with side effects...
	});
	```

## Lifecycle
Registering teardown hooks throws an [error](../error-handling.md#g5) as if [`teardown`](./lifecycle.md#teardown) was called outside of any context:

=== "JSX"
	```jsx
	import { capture, teardown, isolate } from "rvx";

	capture(() => {
		// This will be captured:
		teardown(() => {});

		isolate(() => {
			// This throws:
			teardown(() => {});
		});
	});
	```

=== "No Build"
	```jsx
	import { capture, teardown, isolate } from "./rvx.js";

	capture(() => {
		// This will be captured:
		teardown(() => {});

		isolate(() => {
			// This throws:
			teardown(() => {});
		});
	});
	```

## Signals
Accesses are not tracked as if they were made outside of any observer.

=== "JSX"
	```jsx
	import { isolate, watch } from "rvx";

	watch(() => {
		// This is tracked:
		signalA.access();

		isolate(() => {
			// This is ignored:
			signalB.access();
		});
	});
	```

=== "No Build"
	```jsx
	import { isolate, watch } from "./rvx.js";

	watch(() => {
		// This is tracked:
		signalA.access();

		isolate(() => {
			// This is ignored:
			signalB.access();
		});
	});
	```

To only isolate accesses, use [`untrack`](./signals.md#untrack) instead.

## Non Isolated APIs
The `isolate` function is transparent to all [contexts](./context.md) for performance reasons:

=== "JSX"
	```jsx
	import { Context, isolate } from "rvx";

	const EXAMPLE = new Context(42);

	EXAMPLE.provide(77, () => {
		isolate(() => {
			EXAMPLE.current; // 77
		});
	});
	```

=== "No Build"
	```jsx
	import { Context, isolate } from "./rvx.js";

	const EXAMPLE = new Context(42);

	EXAMPLE.provide(77, () => {
		isolate(() => {
			EXAMPLE.current; // 77
		});
	});
	```

In case you also need to isolate all contexts, `isolate` can be combined with `Context.isolate`:

=== "JSX"
	```jsx
	import { Context, isolate } from "rvx";

	isolate(Context.isolate, [], () => {
		// ...
	});
	```

=== "No Build"
	```jsx
	import { Context, isolate } from "./rvx.js";

	isolate(Context.isolate, [], () => {
		// ...
	});
	```
