mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-04 04:23:02 +00:00
17 lines
3.2 KiB
JSON
17 lines
3.2 KiB
JSON
{
|
|
"id": "3079bf71aa5a8a0987573ed9",
|
|
"source": "solid:signals",
|
|
"type": "github-document",
|
|
"title": "run-with-owner",
|
|
"content": "---\ntitle: runWithOwner\norder: 5\nuse_cases: \u003e-\n async operations, manual cleanup, context access, ownership control,\n setTimeout callbacks, lifecycle management\ntags:\n - async\n - ownership\n - context\n - cleanup\n - lifecycle\n - effects\nversion: '1.0'\ndescription: \u003e-\n Execute code under a specific owner in SolidJS for proper cleanup and context\n access, especially in async operations and setTimeout callbacks.\n---\n\n```ts\nimport { runWithOwner } from \"solid-js\"\nimport type { Owner } from \"solid-js\"\n\nfunction runWithOwner\u003cT\u003e(owner: Owner, fn: (() =\u003e void) =\u003e T): T\n```\n\nExecutes the given function under the provided owner, instead of (and without affecting) the owner of the outer scope.\nBy default, computations created by `createEffect`, `createMemo`, etc. are owned by the owner of the currently executing code (the return value of `getOwner`), so in particular these will get disposed when their owner does.\nCalling `runWithOwner` provides a way to override this default to a manually specified owner (typically, the return value from a previous call to `getOwner`), enabling more precise control of when computations get disposed.\n\nHaving a (correct) owner is important for two reasons:\n\n- Computations without an owner cannot be cleaned up.\nFor example, if you call `createEffect` without an owner (e.g., in the global scope), the effect will continue running forever, instead of being disposed when its owner gets disposed.\n\n- `useContext` obtains context by walking up the owner tree to find the nearest ancestor providing the desired context.\nSo without an owner you cannot look up any provided context (and with the wrong owner, you might obtain the wrong context).\n\nManually setting the owner is especially helpful when doing reactivity outside of any owner scope.\nIn particular, asynchronous computation (via either `async` functions or callbacks like `setTimeout`) lose their automatically set owner, so remembering the original owner via `getOwner` and restoring it via `runWithOwner` is necessary in these cases.\nFor example:\n\n```ts\nconst owner = getOwner()\nsetTimeout(() =\u003e {\n\t// This callback gets run without owner.\n\t// Restore owner via runWithOwner:\n\trunWithOwner(owner, () =\u003e {\n\t\tconst foo = useContext(FooContext)\n\t\tcreateEffect(() =\u003e {\n\t\t\tconsole.log(foo)\n\t\t})\n\t})\n}, 1000)\n```\n\n**Note:** that owners are not what determines dependency tracking, so `runWithOwner` does not help with tracking in asynchronous functions; use of reactive state in the asynchronous part (e.g. after the first `await`) will not be tracked as a dependency.",
|
|
"url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/reference/reactive-utilities/run-with-owner.mdx",
|
|
"metadata": {
|
|
"path": "src/routes/reference/reactive-utilities/run-with-owner.mdx",
|
|
"repo": "solidjs/solid-docs",
|
|
"repo_url": "https://github.com/solidjs/solid-docs.git",
|
|
"size": 2575,
|
|
"source_type": "github"
|
|
},
|
|
"hash": "f215d66fa3e193d010dd0555f4ceabea75be90eb9764d8b7fff1b52ff5b70919",
|
|
"timestamp": "2026-02-23T11:43:00.190095683+01:00"
|
|
} |