{"id":15379730,"url":"https://github.com/yisar/immix","last_synced_at":"2025-04-04T10:43:21.097Z","repository":{"id":101193383,"uuid":"132095026","full_name":"yisar/immix","owner":"yisar","description":"Create next immutable patch by mutating the current.","archived":false,"fork":false,"pushed_at":"2019-09-22T13:12:31.000Z","size":4,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T20:45:27.037Z","etag":null,"topics":["immer","react-state-management","state-management"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yisar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-05-04T06:04:17.000Z","updated_at":"2022-09-08T05:46:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"7c3eb072-9835-4c90-ae06-89417b6add72","html_url":"https://github.com/yisar/immix","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yisar%2Fimmix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yisar%2Fimmix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yisar%2Fimmix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yisar%2Fimmix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yisar","download_url":"https://codeload.github.com/yisar/immix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166095,"owners_count":20894652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["immer","react-state-management","state-management"],"created_at":"2024-10-01T14:19:47.821Z","updated_at":"2025-04-04T10:43:21.075Z","avatar_url":"https://github.com/yisar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Immix\n\n\u003e 一个极小的劫持方案，通过抽象 patch 用于 immutable 环境\n\n### 原理\n\n在 Proxy 劫持过程中产生 patches，在 react 等 immutable 环境中集中处理 patches\n\n#### immix vs immer\n\nimmer 的方案是写时拷贝，在劫持的过程中进行对象的拷贝，将副作用转移到备胎对象上\n\n它和 state 高度脱离，拿到的 draft 对象不再是普通的对象，而是 Proxy 实例\n\n加上拷贝的性能问题，导致它不得不做一些优化手段（freeze tree）\n\nimmix 多了一层 patches 的抽象，无需关心对象是否可变，无需生成备胎，我们只要拿到 pathes ，在 immutable 环境中集中处理 patch 即可\n\n### Use\n\n```js\nvar obj = { name: 132, age: 20 }\nvar proxy = new Immed(obj)\nvar state = proxy.getState()\nstate.name = 'yse'\n```\n\n以上，我们成功劫持并修改了对象的值，我们不关心原始对象有没有变化（当然肯定没有变化）\n\n我们只需要拿到 pathes ：\n\n```js\nvar patches = proxy.getPatches()\nconsole.log(patches) // patches [{op: \"replace\", path: \"/name\", value: \"yse\"}]\n```\n\n当我们拿到 patches 后，我们就可以对 state 进行处理了\n\n```js\nlet state = { name: 132, age: 20 }\nlet patces = [{ op: 'replace', path: '/name', value: 'yse' }]\npathches.forEach({op,path,value} =\u003e {\n  switch (op) {\n    case 'replace':\n      delve(state, path, value)\n      break\n  }\n})\n```\n\n如果我们能够事先给组件绑定 path 的话，甚至可以做 path 的匹配命中工作\n```js\nlet path = '/name'\npatches.filter((item=\u003eitem.path===path)).forEach(/*……*/)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyisar%2Fimmix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyisar%2Fimmix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyisar%2Fimmix/lists"}