Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
angular-async-cache
Advanced tools
A simple utility to help with caching of promises and observables to enable an easy offline first approach in angular 6+ apps
A simple utility to help with caching of promises and observables to enable an easy offline first approach in angular 6+ apps
There is a demo app here that shows the power of this library. Subscribe to some TV shows, turn off your internet and refresh the page and everything should still work (static assets are handled by the fantastic webpack offline plugin)
Install through npm:
npm install --save angular-async-cache
This setup will first emit the cached data (for faster load times + offline first support), then will find the live data, re-emit it and update the cache for future requests
import { NgModule, Component, Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import {
AsyncCache,
LocalStorageDriver,
MemoryDriver,
AsyncCacheModule,
AsyncCacheOptions,
CachedHttp
} from 'angular-async-cache';
export function asyncCacheOptionsFactory(): AsyncCacheOptions {
return new AsyncCacheOptions({
// Default cache driver to use. Default in memory.
// You can also roll your own by implementing the CacheDriver interface
driver: new LocalStorageDriver(),
// this is the special sauce - first emit the data from localstorage,
// then re-fetch the live data from the API and emit a second time.
// The async pipe will then re-render and update the UI. Default: false
fromCacheAndReplay: true
});
}
// declare in your module
@NgModule({
imports: [
HttpClientModule,
// this configures the default options. Just using `AsyncCacheModule.forRoot()` will use
// the defaults of an in memory cache and not replaying from the api
AsyncCacheModule.forRoot({
provide: AsyncCacheOptions,
useFactory: asyncCacheOptionsFactory
})
]
})
class MyModule {}
// finally use with the async pipe in your components template
@Component({
template: `
<div *ngFor="let car of cars | async">
{{ car.model }}
</div>
`
})
class MyComponent {
cars: Observable<Car[]>;
constructor(private cachedHttp: CachedHttp) {
// only the get method is supported (as other http verbs are destructive)
// The second argument can be any options you would pass to a normal http get call
// The third argument is a `AsyncCacheOptions` subset
this.cars = this.cachedHttp.get('/cars');
}
}
There is also a lower level
AsyncCache
service that you can use to manually control caching of observables or promises
@Injectable()
class CarService {
constructor(
private http: Http,
private asyncCache: AsyncCache,
private memoryDriver: MemoryDriver
) {}
getCars(): Observable<Car[]> {
const cars$: Observable<Car[]> = this.http.get('/cars');
return asyncCache.wrap(cars$, '/cars', {
driver: this.memoryDriver // override the default and cache the data in memory
});
}
}
There is also a pipe you can use to instantiate the caching in your template
@Component({
template: `
<div *ngFor="let car of cars | asyncCache:'/cars' | async">
{{ car.model }}
</div>
`
})
class MyComponent {
cars: Observable<Car[]>;
constructor(http: Http) {
this.cars = http.get('/cars');
}
}
npm install
while current directory is this repoRun npm start
to start a development server on port 8000 with auto reload + tests.
Run npm test
to run tests once or npm run test:watch
to continually run tests.
npm run release
MIT
FAQs
A simple utility to help with caching of promises and observables to enable an easy offline first approach in angular 6+ apps
We found that angular-async-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.