{"name":"@solid-primitives/rootless","version":"1.2.6-beta.0","description":"A collection of helpers that aim to simplify using reactive primitives outside of reactive roots, and managing disposal of reactive roots.","author":{"name":"Damian Tarnawski @thetarnav","email":"gthetarnav@gmail.com"},"license":"MIT","homepage":"https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#readme","repository":{"type":"git","url":"git+https://github.com/solidjs-community/solid-primitives.git"},"primitive":{"name":"rootless","stage":2,"list":["createSubRoot","createCallback","createDisposable","createSharedRoot"],"category":"Reactivity"},"private":false,"sideEffects":false,"type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","browser":{},"exports":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":"./dist/index.cjs"},"keywords":["root","solid","primitives"],"dependencies":{"@solid-primitives/utils":"^5.4.0-beta.0"},"peerDependencies":{"solid-js":"^1.6.11"},"typesVersions":{},"scripts":{"build":"jiti ../../scripts/build.ts","test":"vitest -c ../../configs/vitest.config.ts","test:ssr":"pnpm run test --mode ssr"},"readme":"<p>\r\n  <img width=\"100%\" src=\"https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Rootless\" alt=\"Solid Primitives Rootless\">\r\n</p>\r\n\r\n# @solid-primitives/rootless\r\n\r\n[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)\r\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/rootless?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/rootless)\r\n[![version](https://img.shields.io/npm/v/@solid-primitives/rootless?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/rootless)\r\n[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-2.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)\r\n\r\nA collection of helpers that aim to simplify using reactive primitives outside of reactive roots, and managing disposal of reactive roots.\r\n\r\n- [`createSubRoot`](#createSubRoot) - Creates a reactive **sub root**, that will be automatically disposed when it's owner does.\r\n- [`createCallback`](#createCallback) - A wrapper for creating callbacks with `runWithOwner`.\r\n- [`createDisposable`](#createDisposable) - For disposing computations early – before the root cleanup.\r\n- [`createSharedRoot`](#createSharedRoot) - Share \"global primitives\" across multiple reactive scopes.\r\n\r\n## Installation\r\n\r\n```bash\r\nnpm install @solid-primitives/rootless\r\n# or\r\nyarn add @solid-primitives/rootless\r\n```\r\n\r\n## `createSubRoot`\r\n\r\n###### Previously `createSubRoot`\r\n\r\nCreates a reactive **root branch**, that will be automatically disposed when it's owner does.\r\n\r\n### How to use it\r\n\r\nUse it for nested roots _(literally nested, or provided manually to dependency array)_ that should be disposed before or with their owner.\r\n\r\n```ts\r\nimport { createSubRoot } from \"@solid-primitives/rootless\";\r\n\r\ncreateRoot(dispose => {\r\n  createSubRoot(disposeBranch => {\r\n    // computations will be disposed with branch\r\n    createEffect(() => {...});\r\n\r\n    // disposes only the branch\r\n    disposeBranch();\r\n  });\r\n\r\n  // disposes the outer root, AND all the nested branches\r\n  dispose();\r\n});\r\n```\r\n\r\n### Definition\r\n\r\n```ts\r\nfunction createSubRoot<T>(fn: (dispose: VoidFunction) => T, ...owners: (Owner | null)[]): T;\r\n```\r\n\r\n## `createCallback`\r\n\r\nA wrapper for creating callbacks with `runWithOwner`.\r\nIt gives you the option to use reactive primitives after root setup and outside of effects.\r\n\r\n**Why?**\r\n\r\nAll of the callback-based (in opposite to effect-based) events is Solid don't have a root, because the root is changed synchronously after initialization finishes. So normally that would prevent you from using reactive primitives there.\r\n\r\n`runWithOwner` attatches whatever computations created inside, to the owner passed to it.\r\n\r\n### How to use it\r\n\r\n```tsx\r\n// in component body\r\nconst handleClick = createCallback(() => {\r\n   // the effect will be created normally, attached to the component's reactive root\r\n   createEffect(() => {...})\r\n})\r\n\r\n// in jsx\r\n<button onClick={handleClick} />\r\n```\r\n\r\n### Definition\r\n\r\n```ts\r\nfunction createCallback<T extends AnyFunction>(callback: T, owner?: Owner | null): T;\r\n```\r\n\r\n## `createDisposable`\r\n\r\n###### Previously `runWithSubRoot`\r\n\r\nFor disposing computations early – before the root cleanup.\r\n\r\n### How to use it\r\n\r\nExecutes provided function in a [`createSubRoot`](#createSubRoot) _(auto-disposing root)_, and returns a dispose function, to dispose computations used inside before automatic cleanup.\r\n\r\n```ts\r\nconst dispose = createDisposable(dispose => {\r\n   createEffect(() => {...})\r\n});\r\n\r\n// dispose later (if not, will dispose automatically)\r\ndispose()\r\n```\r\n\r\n### Definition\r\n\r\n```ts\r\ntype runWithRootReturn<T> = T extends void | undefined | null\r\n  ? Dispose\r\n  : [returns: T, dispose: Dispose];\r\nconst createDisposable = <T>(fn: () => T, detachedOwner?: Owner): runWithRootReturn<T>\r\n```\r\n\r\n## `createSharedRoot`\r\n\r\n###### Added in `@1.1.0`\r\n\r\nCreates a reactive root that is shared across every instance it was used in. Shared root gets created when the returned function gets first called, and disposed when last reactive context listening to it gets disposed. Only to be recreated again when a new listener appears.\r\n\r\nDesigned to make \"global primitives\" shareable, without instanciating them (recreating, state, computations, event listeners, etc.) every time they're used. For example a `createLocationState` primitive would work the same for every instance and provide the same data, so reinitializeing it every time is wastefull.\r\n\r\n### How to use it\r\n\r\n`createSharedRoot` primitive takes a single argument:\r\n\r\n- `factory` - a function where can you initialize some reactive primitives, returned value will be shared across instances.\r\n\r\nAnd returns a function registering reactive owner as one of the listeners, returns the value `factory` function returned.\r\n\r\n```ts\r\nimport { createSharedRoot } from \"@solid-primitives/rootless\";\r\n\r\nconst useState = createSharedRoot(() => {\r\n   return createMemo(() => {...})\r\n});\r\n\r\n// later in a component:\r\nconst state = useState();\r\nstate()\r\n\r\n// in another component\r\n// previously created primitive would get reused\r\nconst state = useState();\r\n...\r\n```\r\n\r\n### Type Definition\r\n\r\n```ts\r\nfunction createSharedRoot<T>(factory: (dispose: Fn) => T): () => T;\r\n```\r\n\r\n### Demo\r\n\r\nUsage of combining `createSharedRoot` with `createMousePosition`: https://codesandbox.io/s/shared-root-demo-fjl1l9?file=/index.tsx\r\n\r\n## Changelog\r\n\r\nSee [CHANGELOG.md](./CHANGELOG.md)\r\n","readmeFilename":"README.md","bugs":{"url":"https://github.com/solidjs-community/solid-primitives/issues"},"_id":"@solid-primitives/rootless@1.2.6-beta.0","_integrity":"sha512-o7itR55k1bb5j0f6bOBCP6cUjuEsrXsssDDLxAEuhHVDAhm7fYYDPXEmyc2FJdcYanmbP6poOcu9EcdOsfe0Mw==","_resolved":"C:\\Users\\gthet\\AppData\\Local\\Temp\\847a1db7ff6591b16de448873e0ef77d\\solid-primitives-rootless-1.2.6-beta.0.tgz","_from":"file:solid-primitives-rootless-1.2.6-beta.0.tgz","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-o7itR55k1bb5j0f6bOBCP6cUjuEsrXsssDDLxAEuhHVDAhm7fYYDPXEmyc2FJdcYanmbP6poOcu9EcdOsfe0Mw==","shasum":"080e622632ed1cb7178a2796cf2bc8f2ffa904b2","tarball":"http://registry-npm.ywwl.com/@solid-primitives/rootless/download/@solid-primitives/rootless-1.2.6-beta.0.tgz","fileCount":7,"unpackedSize":15546,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICKmnouuxJEaN/8XGRofBaA2cggy3XeL5zAj+NgDDfJmAiEAynfK49yg7s/oKVRI9v7f72KSgH8D2cLq7bB7pNE33Yc="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkAbSQACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo6CA//dmJoIR/616gOHS0hj+mg8JWA9uXKxWHgWbdoTJcCHQZbm7sT\r\nhP3kFg55qIRPxYS8HaJIdyoqGfWOCY6iBNB+85hjLAd9oGJDOxl4/o1za5oC\r\nMk23TZ3N14sbiyDgmKFbrlkTsEJC1c/owwm8TlT9UqOlYIBdn6l/om+U8US5\r\nC/bMqgnIey59oYs6ttzwQoSZdRyoVyc6TJtO395SNmZoVk30Y4l0gLc3LBBc\r\nss5Fyj/Ib3vD0f65wTr6W1i7D2BZMufmxF15TdPuW48uxGz0OI31FfIYqMEy\r\nbhcZWhfo/09dJSKlnKHirePBE4G5DNX3axOcm4+txe7RBX27+kFhpf5QOP+p\r\n+NXUPyU9Tz57sncePJdbFkiSXN3hBIJTh4fXibK2wPfVJDtDQev4S6ezKEG8\r\nORjDvFUKdUu6w8Ey9OzLUhcnDqoom1+dcJMvMDtMcY2qB8HuWhdJeGhvBnRk\r\nr1V3kEJTk8tk0E6kgwExIG1bQ3e/hD3Y5z77BmEMtrbryDTgC0yWsztP3W8j\r\naIieHYMW2Hfn8bHRjNdysIGdsZgoennyly6/ej/8TwlpLT2bYBvB3eiF7FRh\r\nT1Mt0r9Fo1BTI19+pYD+hXdhkRY65zfA9QouVPKfWEY8TGFFXExkmC1rf5YC\r\n/Wwp9fKXbnGtN3G2YrsSibqBWN6FuZ6eXvA=\r\n=tirL\r\n-----END PGP SIGNATURE-----\r\n","size":4655,"noattachment":false,"key":"/@solid-primitives/rootless/-/@solid-primitives/rootless-1.2.6-beta.0.tgz"},"_npmUser":{"name":"thetarnav.","email":"gthetarnav@gmail.com"},"directories":{},"maintainers":[{"name":"thetarnav.","email":"gthetarnav@gmail.com"},{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/rootless_1.2.6-beta.0_1677833360668_0.9787177719079097"},"_hasShrinkwrap":false,"publish_time":1677833360808,"_cnpm_publish_time":1677833360808,"dist-tags":{"beta":"1.2.6-beta.0","latest":"2.0.0","next":"2.0.0-next.4"}}