{"id":15679544,"url":"https://github.com/tuchk4/with-contexts","last_synced_at":"2025-10-15T07:58:08.374Z","repository":{"id":35780583,"uuid":"191574014","full_name":"tuchk4/with-contexts","owner":"tuchk4","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-04T21:48:19.000Z","size":1208,"stargazers_count":12,"open_issues_count":14,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-18T13:52:39.878Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuchk4.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":"2019-06-12T13:14:32.000Z","updated_at":"2019-12-10T14:58:22.000Z","dependencies_parsed_at":"2023-01-16T06:30:22.608Z","dependency_job_id":null,"html_url":"https://github.com/tuchk4/with-contexts","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tuchk4/with-contexts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fwith-contexts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fwith-contexts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fwith-contexts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fwith-contexts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuchk4","download_url":"https://codeload.github.com/tuchk4/with-contexts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fwith-contexts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279059857,"owners_count":26095047,"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","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-03T16:32:57.016Z","updated_at":"2025-10-15T07:58:08.358Z","avatar_url":"https://github.com/tuchk4.png","language":"TypeScript","readme":"# Contexts\n\nContexts for javascript. Inspired by React contexts and hooks\n\n## Usage\n\n```js\nimport {\n  withProvider,\n  attachContexts,\n  duplicateContext,\n  withContext,\n  withValue,\n  createScope,\n} from 'with-contexts';\n```\n\n## Idea\n\nValues of factory functions are the same for each `withContext` call inside one `withProvider` function.\n\n```js\nconst Counter = (value = 0) =\u003e {\n  let count = value;\n\n  return {\n    inc() {\n      count++;\n    },\n    get count() {\n      return count;\n    },\n  };\n};\n\nwithProvider(() =\u003e {\n  const counter = withContext(Counter);\n  counter.inc();\n  console.log(counter.value); // 1\n\n  innerCall();\n});\n\nfunction innerCall() {\n  const counter = withContext(Counter);\n  counter.inc();\n\n  console.log(counter.value); // 2\n}\n```\n\n## Initial value\n\n```js\nconst Counter = (value = 0) =\u003e {\n  let count = value;\n\n  return {\n    inc() {\n      count++;\n    },\n    get count() {\n      return count;\n    },\n  };\n};\n\nwithValue(Counter, 10);\n```\n\n```js\nconst Database = (url, port) =\u003e {\n  // ...\n};\n\nwithValue(Database, 'localhost', 6000);\n```\n\n## Duplicate context\n\nWill not share value and initial value.\n\n```js\nconst Counter = () =\u003e {\n  let value = 0;\n  return {\n    inc() {\n      value++;\n    }\n    get count() {\n      return value;\n    }\n  }\n}\n\nconst Counter2 = duplicateContext(Counter);\n\nwithProvider(() =\u003e {\n  const counter1 = withContext(Counter);\n  const counter2 = withContext(Counter2);\n\n  counter1.inc();\n  counter2.inc();\n\n  console.log(counter1.count); //\n  console.log(counter2.count); //\n});\n```\n\n## Attach contexts for async / later calls\n\n```js\nconst Database = (url, port) =\u003e {};\n\nconst api = withProvider(() =\u003e {\n  return {\n    query: attachContext(() =\u003e {\n      const db = withContext(Database);\n      db.query();\n    }),\n  };\n});\n\napi.query();\n```\n\n## Scope\n\nCreate context with value and clear it after end of function execution.\n\n```js\nconst User = () =\u003e {\n  return {};\n};\n\nfunction setName() {\n  const user = withContext(User);\n  user.name = 'John';\n}\n\nfunction setLastName() {\n  const user = withContext(User);\n  user.lastName = 'Doe';\n}\n\nfunction saveUser() {\n  const user = withContext(User);\n  const db = withContext(Database);\n\n  // ...\n}\n\nconst withUser = createScope(User);\n\nwithProvider(() =\u003e {\n  withUser(() =\u003e {\n    setName();\n    setLastName();\n\n    save();\n  });\n});\n```\n\n## Typings\n\n```\ncreateContext\u003cICountersInterface, InitialValueType\u003e\n```\n\n```ts\nconst Counter = (value = 0) =\u003e {\n  let count = value;\n  return {\n    inc() {\n      count++;\n    },\n    get count() {\n      return state;\n    },\n  };\n};\n\n// Actually it will work correctly without generics\n// const CounterContext = createContext(counter);\nwithProvider(() =\u003e {\n  // Will detect \"counter\" type and show all available methods and props\n  const counter = withContext(Counter);\n\n  counter.inc();\n  counter.count;\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fwith-contexts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuchk4%2Fwith-contexts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fwith-contexts/lists"}