-
Notifications
You must be signed in to change notification settings - Fork 10
/
ProfileProviderDefault.ts
57 lines (48 loc) · 1.82 KB
/
ProfileProviderDefault.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { inject, injectable } from "inversify";
import Profile from "../../entities/profile/Profile";
import TYPES from "../../types";
import IProfileFetcher from "./IProfileFetcher";
import IProfileProvider from "./IProfileProvider";
interface IProfileMap {
[label: string]: Promise<Profile>;
}
@injectable()
export default class ProfileProviderDefault implements IProfileProvider {
// todo, fetcher that loads a profile from a URI
// todo, profiles per location
// e.g. bicycle near a specific station
private profiles: IProfileMap;
private fetcher: IProfileFetcher;
private developmentProfile: Profile; // does not have a persistent id, will change often
private developmentProfileCounter: number;
constructor(
@inject(TYPES.ProfileFetcher) fetcher: IProfileFetcher,
) {
this.profiles = {};
this.fetcher = fetcher;
this.developmentProfile = undefined;
this.developmentProfileCounter = 0;
}
public async parseDevelopmentProfile(blob: object): Promise<string> {
this.developmentProfileCounter += 1;
const newProfile = await this.fetcher.parseProfileBlob(blob, "" + this.developmentProfileCounter);
if (this.developmentProfile) {
// delete this.profiles[this.developmentProfile.getID()];
}
this.developmentProfile = newProfile;
this.profiles[this.developmentProfile.getID()] = Promise.resolve(newProfile);
return newProfile.getID();
}
public addProfile(profile: Profile) {
this.profiles[profile.getID()] = Promise.resolve(profile);
}
public getProfile(profileId: string): Promise<Profile> {
if (this.profiles[profileId] === undefined) {
this.profiles[profileId] = this.fetcher.get(profileId);
}
return this.profiles[profileId];
}
public getProfiles(): Promise<Profile[]> {
return Promise.all(Object.values(this.profiles));
}
}