]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/util/anon-cache.service.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / util / anon-cache.service.ts
1 /**
2  * Service for communicating with the server-side "anonymous" cache.
3  */
4 import {Injectable} from '@angular/core';
5 import {Observable} from 'rxjs';
6 import {StoreService} from '@eg/core/store.service';
7 import {NetService} from '@eg/core/net.service';
8
9 // All anon-cache data is stored in a single blob per user session.
10 // Value is generated on the server with the first call to set_value
11 // and stored locally as a LoginSession item (cookie).
12
13 @Injectable()
14 export class AnonCacheService {
15
16     constructor(private store: StoreService, private net: NetService) {}
17
18     getItem(cacheKey: string, attr: string): Promise<any> {
19         return this.net.request(
20             'open-ils.actor',
21             'open-ils.actor.anon_cache.get_value', cacheKey, attr
22         ).toPromise();
23     }
24
25     // Apply 'value' to field 'attr' in the object cached at 'cacheKey'.
26     // If no cacheKey is provided, the server will generate one.
27     // Returns a promised resolved with the cache key.
28     setItem(cacheKey: string, attr: string, value: any): Promise<string> {
29         return this.net.request(
30             'open-ils.actor',
31             'open-ils.actor.anon_cache.set_value',
32             cacheKey, attr, value
33         ).toPromise().then(key => {
34             if (key) {
35                 return key;
36             } else {
37                 return Promise.reject(
38                     `Could not apply a value for attr=${attr} cacheKey=${key}`);
39             }
40         });
41     }
42
43     removeItem(cacheKey: string, attr: string): Promise<string> {
44         return this.net.request(
45             'open-ils.actor',
46             'open-ils.actor.anon_cache.set_value',
47             cacheKey, attr, null
48         ).toPromise();
49     }
50
51     clear(cacheKey: string): Promise<string> {
52         return this.net.request(
53             'open-ils.actor',
54             'open-ils.actor.anon_cache.delete_session', cacheKey
55         ).toPromise();
56     }
57 }
58
59