{"name":"@solid-primitives/immutable","version":"1.0.10","keywords":["solid","primitives","immutable","store"],"author":{"name":"Damian Tarnawski","email":"gthetarnav@gmail.com"},"license":"MIT","_id":"@solid-primitives/immutable@1.0.10","maintainers":[{"name":"thetarnav.","email":"gthetarnav@gmail.com"},{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"contributors":[],"homepage":"https://primitives.solidjs.community/package/immutable","bugs":{"url":"https://github.com/solidjs-community/solid-primitives/issues"},"dist":{"shasum":"f9374b02f20d0399d254895fcb63b053235fbae3","tarball":"http://registry-npm.ywwl.com/@solid-primitives/immutable/download/@solid-primitives/immutable-1.0.10.tgz","fileCount":7,"integrity":"sha512-ASsqT77m8jxQ2RSqpop2uNC9wbwcrhQidnKVWX8K+DmGH3LunQqq8vgIVNDVB+BPgwpMLIShJqmV59afpV6uzQ==","signatures":[{"sig":"MEUCICSCE5wV7drKlWtOnz//Op7U7u6gHoP8f7cZkFS69uzJAiEA09d3QiPnOos5a1a4K1PNZ7uyp8SxTctmg8+INhbkZdY=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":22706,"size":5920,"noattachment":false,"key":"/@solid-primitives/immutable/-/@solid-primitives/immutable-1.0.10.tgz"},"main":"./dist/index.cjs","type":"module","_from":"file:solid-primitives-immutable-1.0.10.tgz","types":"./dist/index.d.ts","module":"./dist/index.js","browser":{},"exports":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}},"private":false,"scripts":{"dev":"tsx ../../scripts/dev.ts","test":"pnpm run vitest","build":"tsx ../../scripts/build.ts","vitest":"vitest -c ../../configs/vitest.config.ts","test:ssr":"pnpm run vitest --mode ssr"},"_npmUser":{"name":"thetarnav.","email":"gthetarnav@gmail.com"},"_resolved":"/tmp/6d3b52ea8bccd4b8d1dac5ea8f9283c6/solid-primitives-immutable-1.0.10.tgz","primitive":{"list":["createImmutable"],"name":"immutable","stage":1,"category":"Reactivity"},"_integrity":"sha512-ASsqT77m8jxQ2RSqpop2uNC9wbwcrhQidnKVWX8K+DmGH3LunQqq8vgIVNDVB+BPgwpMLIShJqmV59afpV6uzQ==","repository":{"url":"git+https://github.com/solidjs-community/solid-primitives.git","type":"git"},"_npmVersion":"10.8.2","description":"Primitive for rectifying immutable values and dealing with immutability in Solid.","directories":{},"sideEffects":false,"_nodeVersion":"20.18.0","dependencies":{"@solid-primitives/keyed":"^1.2.3","@solid-primitives/utils":"^6.2.3"},"typesVersions":{},"_hasShrinkwrap":false,"devDependencies":{"redux":"^4.2.1","solid-js":"^1.8.7","@reduxjs/toolkit":"^1.9.7","solid-transition-group":"^0.2.3"},"peerDependencies":{"solid-js":"^1.6.12"},"_npmOperationalInternal":{"tmp":"tmp/immutable_1.0.10_1729806028910_0.0858639580533358","host":"s3://npm-registry-packages"},"readme":"<p>\n  <img width=\"100%\" src=\"https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=immutable\" alt=\"Solid Primitives Immutable\">\n</p>\n\n# @solid-primitives/immutable\n\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/immutable?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/immutable)\n[![version](https://img.shields.io/npm/v/@solid-primitives/immutable?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/immutable)\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-1.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)\n\nPrimitive for rectifying immutable values and dealing with immutability in Solid.\n\n- [`createImmutable`](#createimmutable) - Creates a store derived from the given immutable source.\n\n## Installation\n\n```bash\nnpm install @solid-primitives/immutable\n# or\nyarn add @solid-primitives/immutable\n# or\npnpm add @solid-primitives/immutable\n```\n\n## `createImmutable`\n\nCreates a store _(deeply nested reactive object)_ derived from the given immutable source. The source can be any signal that is updated in an immutable fashion.\n\nIt's an **experimental** primitive, a proof of concept of derived nested reactivity. It's not meant to be used in production, but rather as a playground for experimenting with new ideas.\n\n### How to use it\n\n`createImmutable` is a function that takes a reactive function as the first param, and an optional configuration object as the second param:\n\n- `source` reactive function returning an immutable object\n- `options` optional configuration\n  - `key` property name to use as unique identifier for objects when their reference changes\n  - `merge` controls how objects witohut a unique identifier are identified when reconciling an array. If `true` the index is used, otherwise the object reference itself is used.\n\n```ts\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\n// source - can be any reactive function returning an immutable object\nconst [data, setData] = createSignal({ a: 1, b: 2 });\n\n// reactive state derived from the source\nconst state = createImmutable(data);\n\n// just like in Solid stores, the updates are fine-grained - only the changed values are updated\ncreateEffect(() => console.log(state.a, state.b));\n// logs 1 2\n\nsetData({ a: 2, b: 3 });\n// logs 2 3\n```\n\n### Usage with Redux Toolkit\n\nThere are many state management libraries that provide immutable data structures, such as [Immer](https://immerjs.github.io/immer/), [Redux Toolkit](https://redux-toolkit.js.org/), [XState](https://xstate.js.org/docs/), etc.\n\n`createImmutable` can help you turn them into reactive objects, only updating the changed values.\n\n> **Warning** `createStore` with `reconcile` will give you the similar result, while being more efficient.\n\n```tsx\nimport { createSlice, configureStore } from \"@reduxjs/toolkit\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst slice = createSlice({\n  initialState: [\n    { id: 1, title: \"Learn Solid\", completed: false },\n    { id: 2, title: \"Learn Redux\", completed: false },\n  ],\n  reducers: {\n    /* ... (immutable actions) */\n  },\n});\n\nconst store = configureStore({\n  reducer: slice.reducer,\n});\n\nconst [source, setSource] = createSignal(store.getState());\nstore.subscribe(() => setSource(store.getState()));\n\nconst todos = createImmutable(source);\n\n// the references of todos will be preserved, even though they were destructured in the store\n<For each={todos}>\n  {todo => (\n    <div>\n      <input\n        type=\"checkbox\"\n        checked={todo.completed}\n        onClick={() => store.dispatch(slice.actions.toggleTodo(todo.id))}\n      />\n      {todo.title}\n    </div>\n  )}\n</For>;\n```\n\n### Usage with XState\n\n`createImmutable` doesn't mutate the source objects, as opposed to `createStore` with `reconcile`. This makes it a good fit for XState, which uses relies on diffing the previous and next state to determine the changes.\n\n```tsx\nimport { onCleanup, createSignal } from \"solid-js\";\nimport { createMachine, createActor } from \"xstate\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst toggleMachine = createMachine({\n  id: \"toggle\",\n  initial: \"inactive\",\n  states: {\n    inactive: {\n      on: { TOGGLE: \"active\" },\n    },\n    active: {\n      on: { TOGGLE: \"inactive\" },\n    },\n  },\n});\n\nexport const Toggler = () => {\n  const actor = x.createActor(toggleMachine).start();\n  onCleanup(() => actor.stop());\n\n  const [snapshot, setSnapshot] = createSignal(actor.getSnapshot());\n  actor.subscribe(setSnapshot);\n\n  const state = createImmutable(snapshot);\n\n  return (\n    <button onclick={() => actor.send({ type: \"TOGGLE\" })}>\n      {state.value === \"inactive\" ? \"Click to activate\" : \"Active! Click to deactivate\"}\n    </button>\n  );\n};\n```\n\n### Usage with `createResource`\n\nData fetched from the server is immutable, so `createImmutable` can help you turn it into a reactive object, only updating the changed values.\n\n> **Warning** `createResource` provides an experimental `storage` option that can be used together with `createStore` and `reconcile` to achieve the similar result, while being more efficient\n> https://www.solidjs.com/docs/latest/api#createresource\n\n```ts\nimport { createResource } from \"solid-js\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst [data, { refetch }] = createResource(() =>\n  fetch(\"https://jsonplaceholder.typicode.com/todos/1\").then(res => res.json()),\n);\n\nconst state = createImmutable(data);\n\ncreateEffect(() => console.log(state.title, state.completed));\n\n// newely fetched data will be merged with the previous state\nrefetch();\n```\n\n## Demo\n\nYou can see the live demo [here](https://primitives.solidjs.community/playground/immutable).\n\n[Source code](./dev/index.tsx)\n\n## Changelog\n\nSee [CHANGELOG.md](./CHANGELOG.md)\n","publish_time":1729806029101,"_cnpm_publish_time":1729806029101,"dist-tags":{"beta":"0.1.8-beta.0","latest":"1.1.5","next":"2.0.0-next.0"}}