{"id":13452066,"url":"https://github.com/zenparsing/zen-observable","last_synced_at":"2025-10-04T22:28:27.716Z","repository":{"id":1212494,"uuid":"41718927","full_name":"zenparsing/zen-observable","owner":"zenparsing","description":"An Implementation of Observables for Javascript","archived":false,"fork":false,"pushed_at":"2023-01-04T21:36:34.000Z","size":502,"stargazers_count":891,"open_issues_count":2,"forks_count":45,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-09-12T21:42:03.767Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/zenparsing.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":"2015-09-01T05:35:20.000Z","updated_at":"2025-08-31T02:45:42.000Z","dependencies_parsed_at":"2023-01-13T11:01:25.814Z","dependency_job_id":null,"html_url":"https://github.com/zenparsing/zen-observable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/zenparsing/zen-observable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenparsing%2Fzen-observable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenparsing%2Fzen-observable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenparsing%2Fzen-observable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenparsing%2Fzen-observable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zenparsing","download_url":"https://codeload.github.com/zenparsing/zen-observable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zenparsing%2Fzen-observable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278385667,"owners_count":25978106,"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-04T02:00:05.491Z","response_time":63,"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-07-31T07:01:11.775Z","updated_at":"2025-10-04T22:28:27.674Z","avatar_url":"https://github.com/zenparsing.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Repository","Packages","包","Control flow","目录"],"sub_categories":["Control flow","Unsorted","流程控制","Others"],"readme":"# zen-observable\n\nAn implementation of Observables for JavaScript. Requires Promises or a Promise polyfill.\n\n## Install\n\n```sh\nnpm install zen-observable\n```\n\n## Usage\n\n```js\nimport Observable from 'zen-observable';\n\nObservable.of(1, 2, 3).subscribe(x =\u003e console.log(x));\n```\n\n## API\n\n### new Observable(subscribe)\n\n```js\nlet observable = new Observable(observer =\u003e {\n  // Emit a single value after 1 second\n  let timer = setTimeout(() =\u003e {\n    observer.next('hello');\n    observer.complete();\n  }, 1000);\n\n  // On unsubscription, cancel the timer\n  return () =\u003e clearTimeout(timer);\n});\n```\n\nCreates a new Observable object using the specified subscriber function.  The subscriber function is called whenever the `subscribe` method of the observable object is invoked.  The subscriber function is passed an *observer* object which has the following methods:\n\n- `next(value)` Sends the next value in the sequence.\n- `error(exception)` Terminates the sequence with an exception.\n- `complete()` Terminates the sequence successfully.\n- `closed` A boolean property whose value is `true` if the observer's subscription is closed.\n\nThe subscriber function can optionally return either a cleanup function or a subscription object.  If it returns a cleanup function, that function will be called when the subscription has closed.  If it returns a subscription object, then the subscription's `unsubscribe` method will be invoked when the subscription has closed.\n\n### Observable.of(...items)\n\n```js\n// Logs 1, 2, 3\nObservable.of(1, 2, 3).subscribe(x =\u003e {\n  console.log(x);\n});\n```\n\nReturns an observable which will emit each supplied argument.\n\n### Observable.from(value)\n\n```js\nlet list = [1, 2, 3];\n\n// Iterate over an object\nObservable.from(list).subscribe(x =\u003e {\n  console.log(x);\n});\n```\n\n```js\n// Convert something 'observable' to an Observable instance\nObservable.from(otherObservable).subscribe(x =\u003e {\n  console.log(x);\n});\n```\n\nConverts `value` to an Observable.\n\n- If `value` is an implementation of Observable, then it is converted to an instance of Observable as defined by this library.\n- Otherwise, it is converted to an Observable which synchronously iterates over `value`.\n\n### observable.subscribe([observer])\n\n```js\nlet subscription = observable.subscribe({\n  next(x) { console.log(x) },\n  error(err) { console.log(`Finished with error: ${ err }`) },\n  complete() { console.log('Finished') }\n});\n```\n\nSubscribes to the observable.  Observer objects may have any of the following methods:\n\n- `next(value)` Receives the next value of the sequence.\n- `error(exception)` Receives the terminating error of the sequence.\n- `complete()` Called when the stream has completed successfully.\n\nReturns a subscription object that can be used to cancel the stream.\n\n### observable.subscribe(nextCallback[, errorCallback, completeCallback])\n\n```js\nlet subscription = observable.subscribe(\n  x =\u003e console.log(x),\n  err =\u003e console.log(`Finished with error: ${ err }`),\n  () =\u003e console.log('Finished')\n);\n```\n\nSubscribes to the observable with callback functions. Returns a subscription object that can be used to cancel the stream.\n\n### observable.forEach(callback)\n\n```js\nobservable.forEach(x =\u003e {\n  console.log(`Received value: ${ x }`);\n}).then(() =\u003e {\n  console.log('Finished successfully')\n}).catch(err =\u003e {\n  console.log(`Finished with error: ${ err }`);\n})\n```\n\nSubscribes to the observable and returns a Promise for the completion value of the stream.  The `callback` argument is called once for each value in the stream.\n\n### observable.filter(callback)\n\n```js\nObservable.of(1, 2, 3).filter(value =\u003e {\n  return value \u003e 2;\n}).subscribe(value =\u003e {\n  console.log(value);\n});\n// 3\n```\n\nReturns a new Observable that emits all values which pass the test implemented by the `callback` argument.\n\n### observable.map(callback)\n\nReturns a new Observable that emits the results of calling the `callback` argument for every value in the stream.\n\n```js\nObservable.of(1, 2, 3).map(value =\u003e {\n  return value * 2;\n}).subscribe(value =\u003e {\n  console.log(value);\n});\n// 2\n// 4\n// 6\n```\n\n### observable.reduce(callback [,initialValue])\n\n```js\nObservable.of(0, 1, 2, 3, 4).reduce((previousValue, currentValue) =\u003e {\n  return previousValue + currentValue;\n}).subscribe(result =\u003e {\n  console.log(result);\n});\n// 10\n```\n\nReturns a new Observable that applies a function against an accumulator and each value of the stream to reduce it to a single value.\n\n### observable.concat(...sources)\n\n```js\nObservable.of(1, 2, 3).concat(\n  Observable.of(4, 5, 6),\n  Observable.of(7, 8, 9)\n).subscribe(result =\u003e {\n  console.log(result);\n});\n// 1, 2, 3, 4, 5, 6, 7, 8, 9\n```\n\nMerges the current observable with additional observables.\n\n### observable.all()\n\n```js\nlet observable = Observable.of(1, 2, 3);\nfor (let value of await observable.all()) {\n  console.log(value);\n}\n// 1, 2, 3\n```\n\nReturns a `Promise` for an array containing all of the values produced by the observable.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzenparsing%2Fzen-observable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzenparsing%2Fzen-observable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzenparsing%2Fzen-observable/lists"}