This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnvironmentImpl.ts
103 lines (78 loc) · 3.38 KB
/
EnvironmentImpl.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 Peter Beverloo & AnimeCon. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.
import { ApiRequestManager, ApiRequestObserver } from './ApiRequestManager';
import type { Environment, EnvironmentEvent } from './Environment';
import type { IEnvironmentResponse } from '../api/IEnvironment';
import type { Invalidatable } from './Invalidatable';
/**
* Message to include with the exception thrown when data is being accessed before the Environment
* has been initialized properly.
*/
const kExceptionMessage = 'The Environment object has not been successfully initialized yet.';
/**
* Implementation of the Environment interface, shared across the entire Volunteer Portal.
*/
export class EnvironmentImpl implements ApiRequestObserver<'IEnvironment'>, Environment {
private requestManager: ApiRequestManager<'IEnvironment'>;
private responseData?: IEnvironmentResponse;
private observer?: Invalidatable;
constructor(observer?: Invalidatable) {
this.requestManager = new ApiRequestManager('IEnvironment', this);
this.observer = observer;
}
/**
* Initializes the content by issuing an API call request, and returns when that request has
* been completed successfully. The initial content may be sourced from the local cache.
*/
async initialize(): Promise<boolean> {
return this.requestManager.issue();
}
// ---------------------------------------------------------------------------------------------
// ApiRequestObserver interface implementation
// ---------------------------------------------------------------------------------------------
onFailedResponse(error: Error) { /* handled in the App */ }
onSuccessResponse(response: IEnvironmentResponse) {
this.responseData = response;
if (this.observer)
this.observer.invalidate();
}
// ---------------------------------------------------------------------------------------------
// Environment interface implementation
// ---------------------------------------------------------------------------------------------
get title(): Readonly<string> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.title;
}
get themeColor(): Readonly<string> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.themeColor;
}
get themeColorDarkMode(): Readonly<string | undefined> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.themeColorDarkMode
}
get themeTitle(): Readonly<string> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.themeTitle;
}
get events(): Readonly<EnvironmentEvent>[] {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.events;
}
get contactName(): Readonly<string> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.contactName;
}
get contactTarget(): undefined | Readonly<string> {
if (!this.responseData)
throw new Error(kExceptionMessage);
return this.responseData.contactTarget;
}
}