forked from skevetter/devpod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
126 lines (102 loc) · 3.99 KB
/
client.ts
File metadata and controls
126 lines (102 loc) · 3.99 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { FileStorageBackend, Result, ResultError, Return, Store, isEmpty } from "@/lib"
import {
TAddProviderConfig,
TCheckProviderUpdateResult,
TConfigureProviderConfig,
TProviderID,
TProviderOptions,
TProviderSource,
TProviders,
} from "@/types"
import { TDebuggable } from "../types"
import { ProviderCommands } from "./providerCommands"
// WARN: These need to match the rust `file_name` and `dangling_provider_key` constants
// for reliable cleanup!
// Make sure to update them in `src/provider.rs` if you change them here!
const PROVIDERS_STORE_FILE_NAME = "providers"
const PROVIDERS_STORE_DANGLING_PROVIDER_KEY = "danglingProviders"
type TProviderStore = Readonly<{ [PROVIDERS_STORE_DANGLING_PROVIDER_KEY]: readonly TProviderID[] }>
export class ProvidersClient implements TDebuggable {
private readonly store = new Store<TProviderStore>(
new FileStorageBackend<TProviderStore>(PROVIDERS_STORE_FILE_NAME)
)
private danglingProviderIDs: TProviderID[] = []
// Queues store operations and guarantees they will be executed in order
private storeOperationQueue: Promise<unknown> = Promise.resolve()
constructor() {}
public setDebug(isEnabled: boolean): void {
ProviderCommands.DEBUG = isEnabled
}
public async listAll(): Promise<Result<TProviders>> {
return ProviderCommands.ListProviders()
}
public async newID(rawSource: string): Promise<Result<string>> {
return ProviderCommands.GetProviderID(rawSource)
}
public async checkUpdate(id: TProviderID): Promise<Result<TCheckProviderUpdateResult>> {
return ProviderCommands.CheckProviderUpdate(id)
}
public async update(id: TProviderID, source: TProviderSource): Promise<Result<void>> {
return ProviderCommands.UpdateProvider(id, source)
}
public async add(rawSource: TProviderID, config: TAddProviderConfig): Promise<ResultError> {
return ProviderCommands.AddProvider(rawSource, config)
}
public async remove(id: TProviderID): Promise<ResultError> {
return ProviderCommands.RemoveProvider(id)
}
public async rename(id: TProviderID, newName: string): Promise<ResultError> {
return ProviderCommands.RenameProvider(id, newName)
}
public async getOptions(id: TProviderID): Promise<Result<TProviderOptions>> {
return ProviderCommands.GetProviderOptions(id)
}
public async useProvider(id: TProviderID): Promise<ResultError> {
return ProviderCommands.UseProvider(id)
}
public async setOptionsDry(
id: TProviderID,
{ options, reconfigure }: TConfigureProviderConfig
): Promise<Result<TProviderOptions | undefined>> {
return ProviderCommands.SetProviderOptions(id, options, false, true, reconfigure)
}
public async configure(
id: TProviderID,
{ useAsDefaultProvider, reuseMachine, options }: TConfigureProviderConfig
): Promise<ResultError> {
const setResult = await ProviderCommands.SetProviderOptions(id, options, !!reuseMachine)
if (setResult.err) {
return setResult as ResultError
}
if (useAsDefaultProvider) {
return ProviderCommands.UseProvider(id)
}
return Return.Ok()
}
public setDangling(id: TProviderID): void {
this.danglingProviderIDs.push(id)
const ids = this.danglingProviderIDs.slice()
this.storeOperationQueue = this.storeOperationQueue.then(() =>
this.store.set("danglingProviders", ids)
)
}
public popAllDangling(): readonly TProviderID[] {
const maybeProviderIDs = this.danglingProviderIDs.slice()
this.danglingProviderIDs.length = 0
this.storeOperationQueue = this.storeOperationQueue.then(() =>
this.store.remove("danglingProviders")
)
return maybeProviderIDs
}
public popDangling(): TProviderID | undefined {
const lastProviderID = this.danglingProviderIDs.pop()
const ids = this.danglingProviderIDs.slice()
this.storeOperationQueue = this.storeOperationQueue.then(() => {
if (isEmpty(ids)) {
return this.store.remove("danglingProviders")
}
return this.store.set("danglingProviders", ids)
})
return lastProviderID
}
}