UNPKG

5.24 kBTypeScriptView Raw
1import { Disposable, Event, MaybePromise } from '../../common';
2import { PreferenceService } from './preference-service';
3import { PreferenceSchema } from './preference-contribution';
4/**
5 * It is worth explaining the type for `PreferenceChangeEvent`:
6 *
7 * // Given T:
8 * type T = { a: string, b: number }
9 *
10 * // We construct a new type such as:
11 * type U = {
12 * a: {
13 * preferenceName: 'a'
14 * newValue: string
15 * oldValue?: string
16 * }
17 * b: {
18 * preferenceName: 'b'
19 * newValue: number
20 * oldValue?: number
21 * }
22 * }
23 *
24 * // Then we get the union of all values of U by selecting by `keyof T`:
25 * type V = U[keyof T]
26 *
27 * // Implementation:
28 * type PreferenceChangeEvent<T> = {
29 * // Create a mapping where each key is a key from T,
30 * // -? normalizes optional typings to avoid getting
31 * // `undefined` as part of the final union:
32 * [K in keyof T]-?: {
33 * // In this object, K will take the value of each
34 * // independent key from T:
35 * preferenceName: K
36 * newValue: T[K]
37 * oldValue?: T[K]
38 * // Finally we create the union by doing so:
39 * }[keyof T]
40 * }
41 */
42/**
43 * Union of all possible key/value pairs for a type `T`
44 */
45export declare type PreferenceChangeEvent<T> = {
46 affects(resourceUri?: string, overrideIdentifier?: string): boolean;
47} & {
48 [K in keyof T]-?: {
49 readonly preferenceName: K;
50 readonly newValue: T[K];
51 /**
52 * Undefined if the preference is set for the first time.
53 */
54 readonly oldValue?: T[K];
55 };
56}[keyof T];
57export interface PreferenceEventEmitter<T> {
58 readonly onPreferenceChanged: Event<PreferenceChangeEvent<T>>;
59 readonly ready: Promise<void>;
60}
61/**
62 * Generic interface to declare a typesafe get function based on the given
63 * configuration type.
64 *
65 * ### Illustration
66 *
67 * ```ts
68 * interface PreferenceConfiguration {
69 * 'myext.enabled': boolean,
70 * }
71 * const enabled : boolean = prefs.get('myext.enabled'); // valid
72 * const debug : string = prefs.get('myext.enabled'); // invalid
73 * prefs.get('foobar'); // invalid
74 * ```
75 */
76export interface PreferenceRetrieval<T> {
77 get<K extends keyof T>(preferenceName: K | {
78 preferenceName: K;
79 overrideIdentifier?: string;
80 }, defaultValue?: T[K], resourceUri?: string): T[K];
81}
82/**
83 * Typesafe schema-based preferences utility based on the {@link PreferenceService}.
84 * Can be used to get preferences as well as listen to preference changes.
85 *
86 * See {@link createPreferenceProxy} on how to instantiate preference proxies.
87 *
88 * ### Example usage
89 *
90 * ```ts
91 * preferences.onPreferenceChanged(({ preferenceName, newValue }) => { ... });
92 * const enabled = preferences['myext.enabled'];
93 * ```
94 */
95export declare type PreferenceProxy<T> = Readonly<T> & Disposable & PreferenceEventEmitter<T> & PreferenceRetrieval<T>;
96export declare const PreferenceProxyOptions: unique symbol;
97/**
98 * Proxy configuration parameters.
99 */
100export interface PreferenceProxyOptions {
101 /**
102 * Prefix which is transparently added to all preference identifiers.
103 */
104 prefix?: string;
105 /**
106 * The default resourceUri to use if none was specified when calling "set" or "get".
107 */
108 resourceUri?: string;
109 /**
110 * The overrideIdentifier to use with the underlying preferenceService.
111 * Useful to potentially override existing values while keeping both values in store.
112 *
113 * For example to store different editor settings, e.g. "[markdown].editor.autoIndent",
114 * "[json].editor.autoIndent" and "editor.autoIndent"
115 */
116 overrideIdentifier?: string;
117 /**
118 * Indicates whether '.' in schema properties shall be interpreted as regular names (flat),
119 * as declaring nested objects (deep) or both. Default is flat.
120 *
121 * When 'deep' or 'both' is given, nested preference proxies can be retrieved.
122 */
123 style?: 'flat' | 'deep' | 'both';
124 /**
125 * Indicates whether the proxy should be disposable. Proxies that are shared between multiple callers should not be disposable.
126 */
127 isDisposable?: boolean;
128}
129/**
130 * Creates a preference proxy for typesafe preference handling.
131 *
132 * @param preferences the underlying preference service to use for preference handling.
133 * @param promisedSchema the JSON Schema which describes which preferences are available including types and descriptions. Can be a promise.
134 * @param options configuration options.
135 *
136 * @returns the created preference proxy.
137 *
138 * ### Usage
139 *
140 * 1. Create JSON Schema specifying your preferences
141 * 2. Create Configuration type based on the JSON Schema
142 * 3. Bind the return value of `createPreferenceProxy` to make your preferences available wherever needed.
143 *
144 * See {@link CorePreferences} for an example.
145 *
146 * Note that if `schema` is a Promise, most actions will be no-ops until the promise is resolved.
147 *
148 * @deprecated @since 1.23.0 use `PreferenceProxyFactory` instead.
149 */
150export declare function createPreferenceProxy<T>(preferences: PreferenceService, promisedSchema: MaybePromise<PreferenceSchema>, options?: PreferenceProxyOptions): PreferenceProxy<T>;
151//# sourceMappingURL=preference-proxy.d.ts.map
\No newline at end of file