{"id":18552419,"url":"https://github.com/andrejewski/raj-subscription","last_synced_at":"2025-06-30T00:36:34.720Z","repository":{"id":57332542,"uuid":"131789259","full_name":"andrejewski/raj-subscription","owner":"andrejewski","description":"Subscription utilities for Raj","archived":false,"fork":false,"pushed_at":"2020-05-11T17:15:16.000Z","size":79,"stargazers_count":3,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-15T21:20:08.105Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/andrejewski.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":"2018-05-02T02:46:22.000Z","updated_at":"2018-12-24T22:43:40.000Z","dependencies_parsed_at":"2022-09-21T05:05:04.736Z","dependency_job_id":null,"html_url":"https://github.com/andrejewski/raj-subscription","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/andrejewski/raj-subscription","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fraj-subscription","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fraj-subscription/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fraj-subscription/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fraj-subscription/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrejewski","download_url":"https://codeload.github.com/andrejewski/raj-subscription/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrejewski%2Fraj-subscription/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262689900,"owners_count":23349138,"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-11-06T21:14:10.030Z","updated_at":"2025-06-30T00:36:34.672Z","avatar_url":"https://github.com/andrejewski.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raj Subscription\n\u003e Subscription utilities for [Raj](https://github.com/andrejewski/raj)\n\n```sh\nnpm install raj-subscription\n```\n\n[![npm](https://img.shields.io/npm/v/raj-subscription.svg)](https://www.npmjs.com/package/raj-subscription)\n[![Build Status](https://travis-ci.org/andrejewski/raj-subscription.svg?branch=master)](https://travis-ci.org/andrejewski/raj-subscription)\n[![Greenkeeper badge](https://badges.greenkeeper.io/andrejewski/raj-subscription.svg)](https://greenkeeper.io/)\n\nThe `raj-subscription` package contains utilities to reduce the boilerplate of\nworking with subscriptions. Subscriptions are effects that can be cancelled.\nThe subscription shape is:\n\n```ts\ninterface RajSubscription\u003cT\u003e {\n  effect: Effect\u003cT\u003e,\n  cancel: Effect\u003cNever\u003e\n}\n```\n\n## Documentation\nThe package contains the following utilities:\n\n- [`mapSubscription(subscription, callback)`](#mapsubscription)\n- [`batchSubscription(subscriptions)`](#batchsubscriptions)\n- [`withSubscriptions(program)`](#withsubscriptions)\n\n### `mapSubscription`\n\n\u003e `mapSubscription(subscription: RajSubscription, callback(any): any): RajSubscription`\n\nThe `mapSubscription` function \"lifts\" a given `subscription` so that `callback` transforms\n  each message produced by that subscription before dispatch.\n\nThis function is analogous to the [`raj-compose/mapEffect`](https://github.com/andrejewski/raj-compose/blob/master/README.md#mapeffecteffect-function-callbackany-any-function) function.\n\n#### Example\nWe want to distinguish the messages dispatched by an subscription.\nWe use `mapSubscription` to wrap each message in an \"important\" wrapper.\n\n```js\nimport assert from 'assert'\nimport { mapSubscription } from 'raj-subscription'\n\nfunction tickSubscription () {\n  let timerId\n  let count = 1\n  return {\n    effect (dispatch) {\n      timerId = setInterval(() =\u003e dispatch(count++), 1000)\n    },\n    cancel () {\n      clearInterval(timerId)\n    }\n  }\n}\n\nconst importantSubscription = mapSubscription(tickSubscription(), message =\u003e ({\n  type: 'important',\n  value: message\n}))\n\nconst messages = []\nimportantSubscription.effect(message =\u003e {\n  messages.push(message)\n})\n\nsetTimeout(() =\u003e {\n  importantSubscription.cancel()\n\n  assert.deepEqual(messages, [\n    {type: 'important', value: 1},\n    {type: 'important', value: 2}\n  ])\n}, 2500)\n```\n\n### `batchSubscriptions`\n\n\u003e `batchSubscriptions(subscriptions: Array\u003cRajSubscription\u003e): RajSubscription`\n\nThe `batchSubscriptions` function takes an array of `subscriptions` and returns a new\n  subscription which will call each subscription.\n\nThis function is analogous to the [`raj-compose/batchEffects`](https://github.com/andrejewski/raj-compose/blob/master/README.md#batcheffectseffects-arrayfunction-function) function.\n\n### `withSubscriptions`\n\n\u003e `withSubscriptions(program: RajProgramWithSubscriptions): RajProgram`\n\nSubscriptions are setup and torn down during a program's lifetime.\nTo make subscriptions declarative, we can define our active subscriptions as a function of the current state.\n\n```js\nconst subscriptions = model =\u003e ({\n  tick: model.isTicking \u0026\u0026 () =\u003e tickSubscription()\n})\n```\n\nWhen `model.isTicking` is true, the `tickSubscription` will be active.\nWhen the `tick` subscription appears in the object, we start the subscription.\nWhen the `tick` subscription leaves the object, we cancel the subscription.\n\nNotice the function closure around the `tickSubscription` call.\nWe do not create new subscriptions every time `subscriptions` is called, because they may already be active.\nWhile redundant for `tickSubscription`, plain subscriptions need to be wrapped in a function.\n\nTo use this declarative style, we add a `subscriptions` function to our program's definition.\nThe program is then wrapped with the high-order-program (HOP) `withSubscription`. \nThis wrapper will manage our declarative subscriptions for us.\n\n```js\nimport { withSubscriptions } from 'raj-subscription'\n\nconst program = withSubscriptions({\n  init: [],\n  update: (msg, model) =\u003e [model],\n  subscriptions: model =\u003e ({}),\n  view (model, dispatch) {}\n})\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrejewski%2Fraj-subscription","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrejewski%2Fraj-subscription","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrejewski%2Fraj-subscription/lists"}