Skip to content

Commit 478ee05

Browse files
committed
better caching cookies
1 parent c34ccb1 commit 478ee05

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

api/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as httpModule from 'http';
22
import * as httpsModule from 'https';
33
import { outputConsole, currentWorkspaceFolder } from '../utils';
4-
import { config } from '../extension';
4+
const Cache = require('vscode-cache');
5+
import { config, extensionContext } from '../extension';
56

67
const DEFAULT_API_VERSION: number = 3;
78

89
export class AtelierAPI {
9-
private cookies: string[] = [];
1010
private _config: any;
1111
private _namespace: string;
12+
private _cache;
1213

1314
private get ns(): string {
1415
return this._namespace || this._config.ns;
@@ -20,22 +21,30 @@ export class AtelierAPI {
2021

2122
constructor() {
2223
this.setConnection(currentWorkspaceFolder());
24+
const { name, host, port } = this._config;
25+
this._cache = new Cache(extensionContext, `API:${name}:${host}:${port}`);
2326
}
2427

2528
setNamespace(namespace: string) {
2629
this._namespace = namespace;
2730
}
2831

29-
updateCookies(cookies: string[]) {
30-
cookies.forEach(cookie => {
32+
get cookies(): string[] {
33+
return this._cache.get('cookies', []);
34+
}
35+
36+
updateCookies(newCookies: string[]) {
37+
let cookies = this._cache.get('cookies', []);
38+
newCookies.forEach(cookie => {
3139
let [cookieName] = cookie.split('=');
32-
let index = this.cookies.findIndex(el => el.startsWith(cookieName));
33-
if (index) {
34-
this.cookies[index] = cookie;
40+
let index = cookies.findIndex(el => el.startsWith(cookieName));
41+
if (index >= 0) {
42+
cookies[index] = cookie;
3543
} else {
36-
this.cookies.push(cookie);
44+
cookies.push(cookie);
3745
}
3846
});
47+
this._cache.put('cookies', cookies);
3948
}
4049

4150
setConnection(workspaceFolderName: string) {

0 commit comments

Comments
 (0)