1
>
/*---------------------------------------------------------------------------------------------
enablement.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { Disposable } from '../../../../base/common/lifecycle.js';
7
>
import { IObservable, IReader, ITransaction, transaction } from '../../../../base/common/observable.js';
8
>
import { ObservableMemento, observableMemento } from '../../../../platform/observable/common/observableMemento.js';
9
>
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
10
>
11
>
export const enum ContributionEnablementState {
12
>
DisabledProfile,
13
>
DisabledWorkspace,
14
>
EnabledProfile,
15
>
EnabledWorkspace,
16
>
}
17
>
18
>
export function isContributionEnabled(state: ContributionEnablementState): boolean {
19
return state === ContributionEnablementState.EnabledProfile || state === ContributionEnablementState.EnabledWorkspace;
20
}
22
>
export function isContributionDisabled(state: ContributionEnablementState): boolean {
23
return !isContributionEnabled(state);
24
}
26
>
export interface IEnablementModel {
27
>
readEnabled(key: string, reader?: IReader): ContributionEnablementState;
28
>
setEnabled(key: string, state: ContributionEnablementState, tx?: ITransaction): void;
29
>
remove(key: string): void;
30
>
}
31
>
32
>
type EnablementMap = ReadonlyMap<string, boolean>;
33
>
34
function mapToStorage(value: EnablementMap): string {
35
return JSON.stringify([...value]);
36
}
38
function mapFromStorage(value: string): EnablementMap {
39
const parsed = JSON.parse(value);
40
return new Map(Array.isArray(parsed) ? parsed : []);
41
}
43
>
/**
44
>
* A reusable enablement model for string-keyed contributions. Uses
45
>
* `observableMemento` to persist enable/disable state in both profile-scoped
46
>
* and workspace-scoped storage.
47
>
*
48
>
* Resolution order: if a workspace-scoped entry exists for a key, it wins.
49
>
* Otherwise, the profile-scoped entry is used. The default (absence of any
50
>
* entry) is {@link ContributionEnablementState.EnabledProfile}.
51
>
*/
52
>
export class EnablementModel extends Disposable implements IEnablementModel {
53
>
private readonly _profileState: ObservableMemento<EnablementMap>;
54
>
private readonly _workspaceState: ObservableMemento<EnablementMap>;
55
>
56
>
constructor(
57
storageKey: string,
58
@IStorageService storageService: IStorageService,