{"id":13496225,"url":"https://github.com/Kelin2025/effector-history","last_synced_at":"2025-03-28T18:31:44.952Z","repository":{"id":57219814,"uuid":"441231342","full_name":"Kelin2025/effector-history","owner":"Kelin2025","description":"Utility library that implements undo/redo feature for you","archived":false,"fork":false,"pushed_at":"2023-03-13T12:26:38.000Z","size":3002,"stargazers_count":27,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-28T10:23:15.903Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kelin2025.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-12-23T16:06:04.000Z","updated_at":"2024-10-13T05:14:03.000Z","dependencies_parsed_at":"2024-01-07T18:24:32.165Z","dependency_job_id":null,"html_url":"https://github.com/Kelin2025/effector-history","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/Kelin2025%2Feffector-history","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kelin2025%2Feffector-history/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kelin2025%2Feffector-history/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kelin2025%2Feffector-history/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kelin2025","download_url":"https://codeload.github.com/Kelin2025/effector-history/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222403048,"owners_count":16978783,"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":[],"created_at":"2024-07-31T19:01:44.155Z","updated_at":"2024-10-31T11:31:02.806Z","avatar_url":"https://github.com/Kelin2025.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Packages"],"sub_categories":[],"readme":"# Effector History\n\nUtility library that implements undo/redo feature for you\n\n## Installation\n\n```bash\npnpm add effector-history\n```\n\n## Usage\n\n```ts\nimport { createHistory } from \"effector-history\";\n\nconst right = createEvent();\nconst up = createEvent();\n\nconst $x = createStore(0);\nconst $y = createStore(0);\n\n$x.on(right, (x) =\u003e x + 1);\n\n$y.on(up, (y) =\u003e y + 1);\n\nconst history = createHistory({\n  source: {\n    x: $x,\n    y: $y,\n  },\n  maxLength: 20,\n});\n\ncombine([$x, $y]).watch(console.log);\n\nup(); // [0, 1]\nup(); // [0, 2]\nright(); // [1, 2]\nup(); // [1, 3]\n\nhistory.undo(); // [1, 2]\nhistory.undo(); // [0, 2]\nhistory.redo(); // [1, 2]\n```\n\n## Customization\n\n### `clock` property\n\nSometimes you need to add records only on a specific event.  \nFor example, you have draggable blocks, and you want to push to history only whenever drag is ended.  \nFor such cases you can use `clock` property:\n\n```ts\ncreateHistory({\n  source: {\n    x: $x,\n    y: $y,\n  },\n  // Only these events will trigger history\n  clock: [dragEnded],\n  maxLength: 20,\n});\n```\n\nKeep in mind that store updates **won't** push to history in this case, so it can cause data incosistency\n\n### `serialize` property\n\nIf you don't want to serialize history (e.g. you store custom instances and use SSR), you can add `serialize` property:\n\n```ts\ncreateHistory({\n  source: {\n    socketInstance: $socketInstance,\n  },\n  serialize: \"ignore\",\n});\n```\n\n### Strategies\n\nImagine that you have a custom editor with its own history logic. And you want to treat typing in text field as a single history record.\n\nYou can setup custom strategy for a certain `clock`\n\n```ts\nimport { createHistory, replaceRepetitiveStrategy } from \"effector-history\";\n\nconst history = createHistory({\n  source: {\n    text: $text,\n    attachments: $attachments,\n  },\n  clock: [textChanged, attachmentAdded, attachmentRemoved],\n  strategies: new Map().set(textChanged, replaceRepetitiveStrategy),\n});\n\nattachmentAdded(file);\ntextChanged(\"f\");\ntextChanged(\"fo\");\ntextChanged(\"foo\");\n\nhistory.$history.getState();\n/* \n  [\n    { text: '', file: null },\n    { text: '', file: file },\n    { text: 'foo', file: file } // \u003c- Batched in one\n  ]\n*/\n```\n\nBuilt-in strategies:\n\n- `pushStrategy` (default) - always pushes to history, no matter what\n- `replaceRepetitiveStrategy` - if the current record came from the same trigger, the current record will be replaced. Otherwise, the new one will be pushed\n- `skipDuplicatesStrategy` - if the trigger \u0026 payload are the same, don't do anything. Push new record, otherwise\n\nIf you want to make a custom strategy, you can use `customStrategy` helper\n\n```ts\nimport { createHistory, customStrategy } from \"effector-history\";\n\nconst buttonTextChanged = createEvent\u003c{ idx: number; text: string }\u003e();\n\n// List of parameters\n// trigger - Unit that caused history trigger\n// payload - Payload of `trigger`\n// curTrigger - Unit that caused currently active history record\n// curPayload - Payload of `curTrigger`\n// curRecord - Currently active history record\nconst buttonTextChangedStrategy = customStrategy\u003c{ idx: number; text: string }\u003e({\n  check: ({ trigger, curTrigger, payload, curPayload }) =\u003e {\n    // If trigger is different, push a new record\n    if (trigger !== curTrigger) {\n      return \"push\";\n    }\n    // If changing different button, push a new record\n    if (payload.idx !== curPayload.idx) {\n      return \"push\";\n    }\n    // Otherwise replace the current one\n    return \"replace\";\n  },\n});\n\nconst history = createHistory({\n  source: {\n    buttons: $buttons,\n  },\n  clock: [buttonTextChanged],\n  strategies: new Map().set(butonTextChanged, buttonTextChangedStrategy),\n});\n```\n\nIf you want to merge multiple maps of strategies, you can use `mergeStrategiesMaps` helper:\n\n```ts\nimport { pushStrategy, skipDuplicatesStrategy, mergeStrategiesMaps } from \"effector-history\";\n\nconst textFieldStrategies = new Map().set(textFieldBlurred, skipDuplicatesStrategy);\n\nconst buttonsStrategies = new Map().set(buttonAdded, pushStrategy).set(buttonRemoved, pushStrategy);\n\nconst optionsStrategy = mergeStrategiesMaps([textFieldStrategies, buttonsStrategies]);\n```\n\n## API Reference\n\n```ts\nhistory.$history; // All history records\nhistory.$canUndo; // `true` if can undo, `false` otherwise\nhistory.$canRedo; // `true` if can redo, `false` otherwise\nhistory.$curIndex; // Index of currently active history index\nhistory.$curRecord; // Current history record\nhistory.$length; // Amount of records in history (min. 1)\n\nhistory.undo(); // Undo\nhistory.redo(); // Redo\nhistory.clear(); // Clear history\nhistory.push(data); // Manually push something to history\nhistory.replace(data); // Manually replace something in history\n```\n\n## TODO\n\n- [ ] Support `createHistory` without `source`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKelin2025%2Feffector-history","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKelin2025%2Feffector-history","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKelin2025%2Feffector-history/lists"}