{"name":"@solid-primitives/immutable","version":"0.1.4","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":{"browser":{"import":"./dist/index.js","require":"./dist/index.cjs"},"import":"./dist/index.js","require":"./dist/index.cjs"},"keywords":["fp","immutable","solid","primitives"],"dependencies":{"@solid-primitives/utils":"^4.0.0"},"devDependencies":{"solid-js":"^1.6.0"},"peerDependencies":{"solid-js":"^1.6.0"},"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.4","_integrity":"sha512-9oLK8ihIjG5FZv74KoXXyKErxgGxGZsdevsIKB0ugTreBBmozHPcYTjoYFL/sHoqs2ZNMlmfNQ3kduvrvKG2RQ==","_resolved":"/tmp/38fa566b37fb24789fbcbf65d8495772/solid-primitives-immutable-0.1.4.tgz","_from":"file:solid-primitives-immutable-0.1.4.tgz","_nodeVersion":"16.17.1","_npmVersion":"8.15.0","dist":{"integrity":"sha512-9oLK8ihIjG5FZv74KoXXyKErxgGxGZsdevsIKB0ugTreBBmozHPcYTjoYFL/sHoqs2ZNMlmfNQ3kduvrvKG2RQ==","shasum":"8876331fc31be3e7250700174d714cc9ee59bd95","tarball":"http://registry-npm.ywwl.com/@solid-primitives/immutable/download/@solid-primitives/immutable-0.1.4.tgz","fileCount":6,"unpackedSize":29421,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBwvucVzMrBcRLTIk4gaGjYIkYL7AHctXE/EWNq1XtViAiAF0dCoAmUrxv+85DHyURrNES8GVkardCvt9sbCMXV1Fw=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjWtprACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmpxrg//eWykizrqC8gFHlShcxtb2EYCRFKpZw0RpCfNW+GbhJV0eUJn\r\n9FEHiOUoh7UmwMJulNk8sE3m/ZdcAXjI4PS5YFH5UWT41ujGhYkfjX7GHkp0\r\n7rq4JJegOujJB7XSADHDug1K8FKXAcSrQKH4LPENTYWbAIfb5PRZ+kebg2SS\r\n3IGeMHAUT9ld09HuL4aXCBb80tyLMhxjiHwaYaGrxcZ+8cKxePWPTplJ4NzV\r\nbWCNZ2EuwuYURJQubq7f8ATKzj0LClATXrlQ9GdLusj52jmBmOYqey7I0Ku0\r\nhUZnujfkCVupR2nxHFakHsKilB/PdRvUZfwjA90+2etMLemny3XLt7y1t1Pe\r\nzOCyu2MxMc3f/xVF2Sgs6p2p3i76/gCi6Db8+19wh1dTDVsTROYI8eGN2myl\r\nlSiB3q44PVS52rdAhfK7rSqpssFp8ks6ACF1qw+zHmyMIR07oD7a8AB0lHki\r\nNAgFLjFGNdzuv3vxOAS3LjYY+0Tgca+ImaxhASn/fI1stKlqzjR9/Y0cBpHI\r\nMJvxiPCuXq5VdBTf6fXFqx4iLSD3dUuGM5MpE7NEqE0KeuxGkYbOsIfCiQ4X\r\n9AUw2nXAJPJecy+MCAlomOn7aZNJR4V/VhMSANoGm2d10T492//Rc9W/sdLl\r\nAM/ixs+g5+LAz0tNaZ1xKl8L6JvwWFQCAOI=\r\n=uFsw\r\n-----END PGP SIGNATURE-----\r\n","size":6685,"noattachment":false,"key":"/@solid-primitives/immutable/-/@solid-primitives/immutable-0.1.4.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.4_1666898539028_0.5141203567783135"},"_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":1666898539227,"_cnpm_publish_time":1666898539227,"dist-tags":{"beta":"0.1.8-beta.0","latest":"1.1.5","next":"2.0.0-next.0"}}