{"id":15890611,"url":"https://github.com/dimfeld/swr-xstate","last_synced_at":"2025-03-20T11:36:37.808Z","repository":{"id":40812754,"uuid":"278304550","full_name":"dimfeld/swr-xstate","owner":"dimfeld","description":"An implementation of SWR using XState","archived":false,"fork":false,"pushed_at":"2023-01-06T12:02:36.000Z","size":1374,"stargazers_count":31,"open_issues_count":10,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-07T07:06:58.481Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://swr-xstate.imfeld.dev","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/dimfeld.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}},"created_at":"2020-07-09T08:14:22.000Z","updated_at":"2024-05-23T10:05:41.000Z","dependencies_parsed_at":"2023-02-06T01:15:47.877Z","dependency_job_id":null,"html_url":"https://github.com/dimfeld/swr-xstate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fswr-xstate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fswr-xstate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fswr-xstate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimfeld%2Fswr-xstate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dimfeld","download_url":"https://codeload.github.com/dimfeld/swr-xstate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221756899,"owners_count":16875864,"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-10-06T07:07:03.617Z","updated_at":"2024-10-28T01:01:54.908Z","avatar_url":"https://github.com/dimfeld.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is an implementation of a stale-while-revalidate data fetcher implemented with the [XState](https://xstate.js.org) library. It implements many of the features found in other popular SWR libraries, such as periodic updates and disabling the fetcher when the browser is not focused.\n\nI wrote about the design and implementation on [my website](https://imfeld.dev/writing/swr_with_xstate), and there's also a simple [example site](https://swr-xstate.imfeld.dev).\n\n```typescript\n/** The fetcher function can return this if it tried to fetch and found the data was unchanged. */\nexport const UNMODIFIED = Symbol('unmodified');\n\nexport interface FetchResult\u003cT\u003e {\n  data?: T;\n  error?: Error;\n  /** When this data was fetched */\n  timestamp: number;\n  /** True if this is the stale data. Absent otherwise. */\n  stale? : boolean;\n}\n\nexport interface DebugMessage {\n  id: string;\n  state: State\u003cContext, AnyEventObject, any, any\u003e;\n  event: AnyEventObject;\n}\n\nexport interface InitialData\u003cT\u003e {\n  data: T;\n  timestamp?: number;\n}\n\nexport interface AutoFetcherOptions\u003c\n  T\n\u003e {\n  /** A string that can uniquely identify the resource to be fetched. This is\n   * passed as an argument to the `fetch` and `initialData` functions.\n   */\n  key: string;\n\n  /** A name for this fetcher. Will default to the vaue of `key` if not set. */\n  name?: string;\n\n  /** A function that should fetch the \"stale\" data. Called when the fetcher is created. */\n  initialData?: (key:string) =\u003e Promise\u003cInitialData\u003cT\u003e|null\u003e;\n\n  /** `fetcher` is called periodically to retrieve new data */\n  fetcher: (key: string) =\u003e Promise\u003cT|Symbol\u003e,\n  /** `receive` is called when new data has arrived. */\n  receive: (result : FetchResult\u003cT\u003e) =\u003e any;\n\n  /** Number of milliseconds between refresh attempts, unless refresh is forced. */\n  autoRefreshPeriod?: number;\n\n  /** Maximum number of milliseconds to wait between refresh attempts in case of error. Defaults to 1 minute. */\n  maxBackoff? : number;\n\n  /** True if the state machine should permit refreshes by default. False if it should wait for `setPerrmitted(true)`. Defaults to true. */\n  initialPermitted? : boolean;\n\n  /** True if the state machine should be enabled by default. False if it should wait for `setEnabled(true)`\n   * with `true` as the data before it starts refreshing. Defaults to true.\n   * Typically this is used to disable updates if nothing in the application is actually listening for changes.\n   */\n  initialEnabled? : boolean;\n\n  /** Given an object, this function should print out debug information. This can be `console.log` if you want, or something like the `debug` module. Called on every state transition. */\n  debug?: (msg : DebugMessage) =\u003e any;\n}\n\nexport interface AutoFetcher {\n  /** Set if fetching is enabled. It might be disabled if you know that nothing is using this data right now. */\n  setEnabled: (enabled : boolean) =\u003e void;\n  /** Set if fetching is permitted. Fetching might not be permitted if the user is not logged in or lacks\n   * proper permissions for this endpoint, for example. */\n  setPermitted: (permitted: boolean) =\u003e void;\n  /** Force a refresh. This will not do anything if fetching has been disabled via `setPermitted`. */\n  refresh: () =\u003e void;\n  destroy: () =\u003e void;\n}\n\n/** This function creates a fetcher */\nexport function fetcher\u003cT\u003e(options : AutoFetcherOptions\u003cT\u003e) : AutoFetcher;\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimfeld%2Fswr-xstate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimfeld%2Fswr-xstate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimfeld%2Fswr-xstate/lists"}