{"id":20022297,"url":"https://github.com/lucifier129/coeffect","last_synced_at":"2025-09-26T16:43:09.772Z","repository":{"id":57202630,"uuid":"322846285","full_name":"Lucifier129/coeffect","owner":"Lucifier129","description":"An effect-management library for React","archived":false,"fork":false,"pushed_at":"2020-12-19T13:52:44.000Z","size":6,"stargazers_count":14,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-20T20:18:08.387Z","etag":null,"topics":["effect-driven"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/Lucifier129.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}},"created_at":"2020-12-19T12:47:25.000Z","updated_at":"2023-05-01T11:14:26.000Z","dependencies_parsed_at":"2022-09-15T13:22:29.342Z","dependency_job_id":null,"html_url":"https://github.com/Lucifier129/coeffect","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/Lucifier129%2Fcoeffect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2Fcoeffect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2Fcoeffect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2Fcoeffect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lucifier129","download_url":"https://codeload.github.com/Lucifier129/coeffect/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252423167,"owners_count":21745553,"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":["effect-driven"],"created_at":"2024-11-13T08:39:51.159Z","updated_at":"2025-09-26T16:43:04.737Z","avatar_url":"https://github.com/Lucifier129.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Coeffect\n\nAn effect-management library for React\n\n`Coeffect` 让我们在 React 组件外部管理 `Effect`，尽情使用 `side-effect` 和 `mutable-state`，不受 `React` 组件的 `immutable` 和 `pure` 的限制。\n\n`Coeffect` 里的副作用遵循 `useEffect(effectFunction)` 的规则，是渲染安全的，但能够很好地和 `React` 组件协同工作。\n\n## Usage\n\n```typescript\nimport { createEffect } from 'coeffect'\n\n/**\n * 定义你的 Effect 类型\n * options 描述数据参数\n * consumer 描述回调方法\n * handler 描述控制方法\n */\nexport type TimeoutEffect = {\n  options: {\n    duration: number\n  }\n  consumer: {\n    onStart?: () =\u003e any\n    onStop?: () =\u003e any\n    onEnd?: () =\u003e any\n    onUpdate?: (currDuration: number, prevDuration: number) =\u003e any\n  }\n  handler: {\n    start: (duration?: number) =\u003e void\n    stop: () =\u003e void\n    update: (duration: number) =\u003e void\n    isStart: () =\u003e boolean\n  }\n}\n\n/**\n * 根据 Effect 类型，创建 Effect 对象\n */\nexport const Timeout = createEffect\u003cTimeoutEffect\u003e((consumer, options) =\u003e {\n  let tid: number | null = null\n  let duration = options.duration // 消费 options 参数\n\n  // 实现 handler 方法\n  return {\n    start() {\n      if (tid !== null) return\n      tid = setTimeout(() =\u003e {\n        tid = null\n        consumer.onEnd?.() // 调用 consumer 方法\n      }, duration)\n      consumer.onStart?.()\n    },\n    stop() {\n      if (tid === null) return\n      clearTimeout(tid)\n      tid = null\n      consumer.onStop?.()\n    },\n    update(newDuration) {\n      duration = newDuration\n      this.stop()\n    },\n    isStart() {\n      return tid !== null\n    }\n  }\n})\n\n// 在 React 组件中使用\n\nconst CopyButton: FC\u003cProps\u003e = ({ text, children }) =\u003e {\n  const [noticing, setNoticing] = useState(false)\n\n  /**\n   * 使用时，传递 consumer 回调方法，传递 options 参数\n   */\n  const notifier = Timeout.useEffects(\n    {\n      onStart: () =\u003e {\n        // 在 consumer 方法中，将数据同步到 state\n        setNoticing(true)\n      },\n      onEnd: () =\u003e {\n        setNoticing(false)\n      },\n      onStop: () =\u003e {\n        setNoticing(false)\n      }\n    },\n    {\n      duration: 1500\n    }\n  )\n\n  const handleCopy = (text: string) =\u003e {\n    // 可以按需调用，而非被动 re-render\n    if (!text.includes('*')) {\n      notifier.start() // 调用 handler 控制方法\n    }\n  }\n\n  return (\n    \u003cTooltip visible={noticing} title=\"已复制至剪贴板\"\u003e\n      \u003cCopyToClipboard text={text} onCopy={handleCopy}\u003e\n        {children}\n      \u003c/CopyToClipboard\u003e\n    \u003c/Tooltip\u003e\n  )\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucifier129%2Fcoeffect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucifier129%2Fcoeffect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucifier129%2Fcoeffect/lists"}