|
6 | 6 |
|
7 | 7 | import { TPromise } from 'vs/base/common/winjs.base'; |
8 | 8 | import { ICredentialsService } from 'vs/platform/credentials/common/credentials'; |
9 | | - |
10 | 9 | import * as keytarType from 'keytar'; |
11 | | -let keytar: typeof keytarType; |
12 | | -require(['keytar'], ([k]) => { |
13 | | - keytar = k; |
14 | | -}, err => { |
15 | | - // TODO chrmarti |
16 | | -}); |
17 | 10 |
|
18 | 11 | export class CredentialsService implements ICredentialsService { |
19 | 12 |
|
20 | 13 | _serviceBrand: any; |
21 | 14 |
|
| 15 | + private keytarPromise: TPromise<typeof keytarType>; |
| 16 | + |
22 | 17 | readSecret(service: string, account: string): TPromise<string | undefined> { |
23 | | - return TPromise.wrap(keytar.getPassword(service, account) |
24 | | - .then(result => result === null ? undefined : result)); |
| 18 | + return this.getKeytar() |
| 19 | + .then(keytar => TPromise.wrap(keytar.getPassword(service, account))) |
| 20 | + .then(result => result === null ? undefined : result); |
25 | 21 | } |
26 | 22 |
|
27 | 23 | writeSecret(service: string, account: string, secret: string): TPromise<void> { |
28 | | - return TPromise.wrap(keytar.setPassword(service, account, secret)); |
| 24 | + return this.getKeytar() |
| 25 | + .then(keytar => TPromise.wrap(keytar.setPassword(service, account, secret))); |
29 | 26 | } |
30 | 27 |
|
31 | 28 | deleteSecret(service: string, account: string): TPromise<boolean> { |
32 | | - return TPromise.wrap(keytar.deletePassword(service, account)); |
| 29 | + return this.getKeytar() |
| 30 | + .then(keytar => TPromise.wrap(keytar.deletePassword(service, account))); |
| 31 | + } |
| 32 | + |
| 33 | + private getKeytar(): TPromise<typeof keytarType> { |
| 34 | + if (!this.keytarPromise) { |
| 35 | + this.keytarPromise = new TPromise<typeof keytarType>((c, e) => { |
| 36 | + require(['keytar'], c, e); |
| 37 | + }); |
| 38 | + } |
| 39 | + return this.keytarPromise; |
33 | 40 | } |
34 | 41 | } |
0 commit comments