{"id":19753117,"url":"https://github.com/monesidn/load-n-cache-rxjs","last_synced_at":"2025-08-11T18:17:18.386Z","repository":{"id":43975081,"uuid":"248085697","full_name":"monesidn/load-n-cache-rxjs","owner":"monesidn","description":"Make it easier to use load-n-cache in a rxjs environment.","archived":false,"fork":false,"pushed_at":"2023-02-04T15:41:21.000Z","size":1192,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T08:46:05.146Z","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":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/monesidn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-17T22:29:25.000Z","updated_at":"2022-08-15T12:56:13.000Z","dependencies_parsed_at":"2023-02-08T08:46:06.446Z","dependency_job_id":null,"html_url":"https://github.com/monesidn/load-n-cache-rxjs","commit_stats":{"total_commits":14,"total_committers":2,"mean_commits":7.0,"dds":0.0714285714285714,"last_synced_commit":"6e2f6d0a6fdea70d6feb27e0fed92e5dd6576424"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/monesidn/load-n-cache-rxjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monesidn%2Fload-n-cache-rxjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monesidn%2Fload-n-cache-rxjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monesidn%2Fload-n-cache-rxjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monesidn%2Fload-n-cache-rxjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/monesidn","download_url":"https://codeload.github.com/monesidn/load-n-cache-rxjs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monesidn%2Fload-n-cache-rxjs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269477954,"owners_count":24423776,"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-08-08T02:00:09.200Z","response_time":72,"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-11-12T02:51:38.165Z","updated_at":"2025-08-11T18:17:18.340Z","avatar_url":"https://github.com/monesidn.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Load 'n' Cache rxjs\r\n\r\n-   Main Project: https://github.com/monesidn/load-n-cache\r\n-   Example usage Project: https://github.com/monesidn/load-n-cache-test-app\r\n\r\nOk so you like load 'n' cache but prefer Observables instead of Promises? No problem at all! This small package provides a tiny layer to provide native Observable support. Internally it still uses Promises and `async/await` code but you don't need to worry about.\r\n\r\nIf you can see a reason to fork the project to cut out promises entirely please let me know.\r\n\r\n## What it does\r\n\r\nThis project provides an rxjs compliant operator that:\r\n\r\n-   Subscribes to the inner observable;\r\n-   Waits for the inner observable to complete and store the last value emitted, called \"stored value\" from now on.\r\n-   On subscription the stored value is emitted.\r\n\r\nThese steps can be achieved easily using a `ReplaySubject` and the `share()` operator but, using this package you can also:\r\n\r\n-   Persist the \"stored value\". While a persisted \"stored value\" exists the inner observable is never subscribed.\r\n-   The \"stored value\" can be flushed when a secondary observable emits.\r\n\r\n## Do I really need it?\r\n\r\nThis library provides a one-stop solution to application level resource caching in a rxjs environment. You can achieve a similar result using the rxjs operators and some custom logic but it would be much harder to maintain.\r\n\r\n## Info\r\n\r\nThe main class of this project is `LoadNCacheObservable` that can be used directly or, in more `.pipe()` friendly way, using the `loadNCache()` function. Piping any observable through the `loadNCache()` function result in caching its last emitted value. Any subsequent subscription will receive the cached value and the inner subscriber is left alone (unless a flush occurs).\r\n\r\n### Flushing\r\n\r\nWhen using a LoadNCache instance directly you can call the `flush()` method to clear the cache value. For better rxjs integration the `loadNCache()` function accepts an option: `flushOn`. `flushOn` is a function that should return an `Observable` that will emit when the value must be flushed.\r\n\r\nSee the usage section for an example.\r\n\r\n## API\r\n\r\nCheck out https://monesidn.github.io/load-n-cache-rxjs/\r\n\r\n## Usage\r\n\r\n## Installation\r\n\r\n```\r\nnpm install --save load-n-cache-rxjs\r\n```\r\n\r\nand then:\r\n\r\n```typescript\r\nimport { loadNCache } from \"load-n-cache-rxjs\";\r\n```\r\n\r\n### Basic usage\r\n\r\n```typescript\r\npublic randStr$ = new Observable\u003cstring\u003e((sub) =\u003e {\r\n            sub.next(randomStr());\r\n            sub.complete();\r\n        })\r\n        .pipe(loadNCache());\r\n```\r\n\r\nJust pipe you source observable into the loadNCache function to start caching.\r\n\r\n### Flush value after one second\r\n\r\n```typescript\r\npublic randStr$ = new Observable\u003cstring\u003e((sub) =\u003e {\r\n            sub.next(randomStr());\r\n            sub.complete();\r\n        })\r\n        .pipe(loadNCache({ flushOn: () =\u003e timer(1000) }));\r\n```\r\n\r\nThe `timer(1000)` observable is subscribed after fetching a new value. After 1000ms from the fetching it emits and the value is flushed.\r\n\r\n### Save value into localStorage (persistence)\r\n\r\n```typescript\r\npublic randStr$ = new Observable\u003cstring\u003e((sub) =\u003e {\r\n            sub.next(randomStr());\r\n            sub.complete();\r\n        })\r\n        .pipe(loadNCache({\r\n            persistence: 'localStorage',\r\n            persistenceKey: 'my-ls-key'\r\n        }));\r\n```\r\n\r\nYou can supply a persistence manager or use one of the supported out of the box. See below for more.\r\n\r\n## The `LoadNCacheObservable` class\r\n\r\n`LoadNCacheObservable` is a custom subclass of Observable that generates a LoadNCache instance and uses it to cache the last value emitted from the inner observable. This observable is very similar to a `ReplaySubject(1)` with a some key differences:\r\n\r\n-   Even if the observable emitted in the past you can't assume that new subscribers will immediately receive a value.\r\n    -   This happen when a flush occurred and the inner observable haven't emitted yet.\r\n-   If no flush observable is provided each subscription is completed after receiving the first value. Without a mean to flush the cached value no value beside the first can be emitted.\r\n\r\nPlease don't interact directly with the LoadNCache instance from this class as it may result in undefined behaviour.\r\n\r\n## Persistence\r\n\r\nTo the loadNCache function or to the LoadNCacheObservable constructor you can pass persistence options that work like those from the main project. See https://github.com/monesidn/load-n-cache for details.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonesidn%2Fload-n-cache-rxjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmonesidn%2Fload-n-cache-rxjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonesidn%2Fload-n-cache-rxjs/lists"}