• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ringcentral / ringcentral-js / 5286631680

pending completion
5286631680

push

github

web-flow
chore: fix sandbox status 502 issue (#219)

260 of 335 branches covered (77.61%)

Branch coverage included in aggregate %.

766 of 792 relevant lines covered (96.72%)

41.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.5
/sdk/src/core/Cache.ts
1
import Externals from './Externals';
2

3
export interface CacheOptions {
4
    prefix?: string;
5
    externals: Externals;
6
}
7

8
export default class Cache {
1✔
9
    public static defaultPrefix = 'rc-';
1✔
10

11
    private readonly _prefix = null;
96✔
12

13
    private _externals = null;
96✔
14

15
    public constructor({prefix = Cache.defaultPrefix, externals}: CacheOptions) {
96✔
16
        this._prefix = prefix;
96✔
17
        this._externals = externals;
96✔
18
    }
19

20
    public setItemSync(key, data) {
1✔
21
        this._externals.localStorage.setItem(this._prefixKey(key), JSON.stringify(data));
159✔
22
        return this;
159✔
23
    }
24

25
    public async setItem(key, data) {
1✔
26
        this.setItemSync(key, data);
159✔
27
    }
28

29
    public removeItemSync(key) {
1✔
30
        this._externals.localStorage.removeItem(this._prefixKey(key));
3✔
31
        return this;
3✔
32
    }
33

34
    public async removeItem(key) {
1✔
35
        await this.removeItemSync(key);
6✔
36
    }
37

38
    public getItemSync(key) {
1✔
39
        const item = this._externals.localStorage.getItem(this._prefixKey(key));
401✔
40
        if (!item) return null;
401✔
41
        return JSON.parse(item);
296✔
42
    }
43

44
    public async getItem(key) {
1✔
45
        return this.getItemSync(key);
401✔
46
    }
47

48
    private async _keys(): Promise<string[]> {
1✔
49
        return 'keys' in this._externals.localStorage
155✔
50
            ? this._externals.localStorage.keys() // could be async
155!
51
            : Object.keys(this._externals.localStorage);
52
    }
53

54
    public async clean() {
1✔
55
        await Promise.all(
310✔
56
            (await this._keys()).map(async key => {
155✔
57
                if (key.startsWith(this._prefix)) {
150✔
58
                    await this._externals.localStorage.removeItem(key);
210✔
59
                }
60
            }),
61
        );
62

63
        return this;
155✔
64
    }
65

66
    private _prefixKey(key: string) {
1✔
67
        return this._prefix + key;
563✔
68
    }
69
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc