{"name":"@solid-primitives/immutable","version":"0.1.6","description":"Functional programming helpers for making non-mutating changes to data. Keeping it immutable.","author":{"name":"Damian Tarnawski @thetarnav","email":"gthetarnav@gmail.com"},"license":"MIT","homepage":"https://github.com/solidjs-community/solid-primitives/tree/main/packages/immutable#readme","repository":{"type":"git","url":"git+https://github.com/solidjs-community/solid-primitives.git"},"primitive":{"name":"immutable","stage":2,"list":["List of functions"],"category":"Utilities"},"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":["fp","immutable","solid","primitives"],"dependencies":{"@solid-primitives/utils":"^5.0.0"},"peerDependencies":{"solid-js":"^1.6.0"},"typesVersions":{},"scripts":{"build":"jiti ../../scripts/build.ts","test":"vitest -c ../../configs/vitest.config.ts","test:ssr":"pnpm run test --mode ssr"},"bugs":{"url":"https://github.com/solidjs-community/solid-primitives/issues"},"_id":"@solid-primitives/immutable@0.1.6","_integrity":"sha512-P4wbDogtytijTV5En+bm6jfFbf4ZOMnfmttUJ8642MQ0tJ2VS2z8iJplg9YFJIltXz7W/+2mdfw2XczlkTxeGg==","_resolved":"/tmp/e24e2c3ce2850a3096f255b5b38ec245/solid-primitives-immutable-0.1.6.tgz","_from":"file:solid-primitives-immutable-0.1.6.tgz","_nodeVersion":"18.13.0","_npmVersion":"8.19.3","dist":{"integrity":"sha512-P4wbDogtytijTV5En+bm6jfFbf4ZOMnfmttUJ8642MQ0tJ2VS2z8iJplg9YFJIltXz7W/+2mdfw2XczlkTxeGg==","shasum":"e93c7c6c59cf4c812b0dc8b90e4b55b7ee9ba009","tarball":"http://registry-npm.ywwl.com/@solid-primitives/immutable/download/@solid-primitives/immutable-0.1.6.tgz","fileCount":6,"unpackedSize":28642,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFIlD6Cz1DG9DFBrTAF54b2qhFTJCciD8b8f8Q6igFe+AiAI0VuLtXwJZmOfY+TmLLI6IhndWKOUHsXr4jOSx6/B9Q=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj1an0ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo9UBAAj6NXB6JsdsM1i+M5ywjq/0T5UCqReI05Jbq03UobdTiyZuXm\r\nEH2js87D/pMS1VPEi8I+N6MGDXnnWqccJHguujistDLDTvUIf/HThFx2J8sp\r\nL11NeRPQSnVamVKt+l4z4Kviyf2G7s1amOevqoaXl0pJ223hHMxJ0TT7zAGl\r\nLa+8BDpwGBvHYay6GG0NN0U+M1xpNBpI0UjY1je+9IaCaUemBgYQAbfl6PXK\r\nLaX823UY4cl0xaZWsynxErKQ5p6bYqBa/trnfwRogFVREvO8WMrBvI5Xh7YZ\r\njIBuMA3S5MC8AhYGUBVkRj2lA2kuTgn5a/+bmWI5wXiykMSTefqt0LVQsy1+\r\n41v+tBUnYusZoYMr6rvWozr17pWx03wC8bpks2pIIjZMqF+WB6z2tQszIGOZ\r\nOUz2/tlDfD1Mg5xdJ+Clcu+AXnlsOF/kOirRufWSI4kIVnLZcuwJ9mMYfK5w\r\nK7u+vUXuKaGAIg0pYrPT+N4RlB27Mrp39xpAhb8HwDKV01jS26tOx7+UT8mO\r\nzw9Naiz3bePc9vBWelZVKBV5VmSGTPB0DApfq9kSMvHH2wPpi/AghXHkoNfq\r\nPWDHdeUFyyIRg6kOcP8ILgEiy72+Tjs4cM9X3SkrNb0sIRJJPQELjVw1p5ra\r\nsmgOHNU5ws+zjo7prPs45MHFd0LxosJPDGE=\r\n=hqNY\r\n-----END PGP SIGNATURE-----\r\n","size":6666,"noattachment":false,"key":"/@solid-primitives/immutable/-/@solid-primitives/immutable-0.1.6.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/immutable_0.1.6_1674947060434_0.7789992180397989"},"_hasShrinkwrap":false,"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[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)\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":1674947060582,"_cnpm_publish_time":1674947060582,"dist-tags":{"beta":"0.1.8-beta.0","latest":"1.1.5","next":"2.0.0-next.0"}}