{"name":"@solid-primitives/immutable","version":"0.1.3","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":"^3.1.0"},"devDependencies":{"solid-js":"^1.5.0"},"peerDependencies":{"solid-js":"^1.5.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.3","_integrity":"sha512-T82Dy/i9QmK4/iSfbEo0bgdbW9PSOTnS0V2Y5G9L9uW/4TfpQmo67gWptcJTJsTbLn1QgTy3RPBTWwn8j4qIRw==","_resolved":"/tmp/be437bf23db9d1d385d261f76d90672e/solid-primitives-immutable-0.1.3.tgz","_from":"file:solid-primitives-immutable-0.1.3.tgz","_nodeVersion":"16.17.1","_npmVersion":"8.15.0","dist":{"integrity":"sha512-T82Dy/i9QmK4/iSfbEo0bgdbW9PSOTnS0V2Y5G9L9uW/4TfpQmo67gWptcJTJsTbLn1QgTy3RPBTWwn8j4qIRw==","shasum":"a1a39a2772d67d48ff4dc75445e1fe0df0079abf","tarball":"http://registry-npm.ywwl.com/@solid-primitives/immutable/download/@solid-primitives/immutable-0.1.3.tgz","fileCount":6,"unpackedSize":29421,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDk0UmljjGKuvhZh9hm0LD7w6d6aA8NcC2vVTsFQMBu7AIhAMDEqOB0fLWErHDVtgC/0eNb71OUwid2YUrf9n2nqblY"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjUXCcACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoXPw/+MiESTU7dMjCADkVLbm/+eR9/Ow99wIOl0yK/ysjmsVPwBipH\r\n0Nc9N7huIdnrWi/0Ir+T7xXG3gk+Nnkmht8immwfMtjiALEk4SGmpNeX8rhG\r\nGKVww1C/aojFzLz9IJ0sIH11iBB8qEBcZhoVh/4CNPhC+VrnbcAo3J4dHlB6\r\ngV8f2dmntTt9AZUJK/Jjy+1NDJyR34n93JcS+VVq6LiAp87mEjWxoZNcl82/\r\n4fD8hNg2wWElofSjxhy9cCb6oV+ceuYhWIoz66shWAEYtv7y4H13XGteBg+c\r\nQTMws7Fx+NIU4e6xWs4gyUfk7Rx9IjtsLgtgqVjuHhrSWFtGowNwWd3UUz+J\r\nFD5alFRuL4YUN0RfLv9gOOwoF1ufX3/ytwOK+ebpGdqzra+JSSjZj05qUt/V\r\ndwmNdCjC2gdB0vH1uVLwV1hFmmYzLoLQn2+8SGTzMQHUtIx/uSJ26nnNp6f2\r\nu81W4W7OC3sfo3XcUKPZlQVM5fZuD5IPBTGY5krN2ld1VGdmFv2S7mWdk7Co\r\nU9lAFQqdXYA/Scb7zPKXUJYI3JpkqamUB2z4BHQfZ2zpnEHR4aUenoGveU81\r\n+IYeMlOP9bTEWhiVLy7YfmuLxhCXiElp5nssOoMI/R+/CmdkTcj1oSWrcpwV\r\nogzhshomeEaQaUIExr2y+QzVM2+sd5sFsPQ=\r\n=Oegs\r\n-----END PGP SIGNATURE-----\r\n","size":6684,"noattachment":false,"key":"/@solid-primitives/immutable/-/@solid-primitives/immutable-0.1.3.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.3_1666281628171_0.9556436963829513"},"_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":1666281628411,"_cnpm_publish_time":1666281628411,"dist-tags":{"beta":"0.1.8-beta.0","latest":"1.1.5","next":"2.0.0-next.0"}}