Skip to content

Commit 79a78ef

Browse files
committed
feat: add set method for config ref/object
1 parent 01b2cea commit 79a78ef

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

docs/guide/config.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ enable.value = false
6565

6666
// To pass the rest of the options, you can use the `update` method.
6767
enable.update(false, ConfigurationTarget.Global)
68+
69+
// Only set the ref value without writing back to the configuration.
70+
enable.set(false)
6871
```
6972

7073
Visit the [official documentation](https://code.visualstudio.com/api/references/vscode-api#WorkspaceConfiguration.update) for more information about the rest of the options.
@@ -98,6 +101,9 @@ config.enable = false
98101

99102
// To pass the rest of the options, you can use the `$update` method.
100103
config.$update('enable', false, ConfigurationTarget.Global)
104+
105+
// Only set the ref value without writing back to the configuration.
106+
config.$set('enable', false)
101107
```
102108

103109
Visit the [official documentation](https://code.visualstudio.com/api/references/vscode-api#WorkspaceConfiguration.update) for more information about the rest of the options.

packages/core/src/utils/defineConfigObject.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export type ConfigObject<C extends object> = ShallowReactive<C & {
1212
* @see https://code.visualstudio.com/api/references/vscode-api#WorkspaceConfiguration.update
1313
*/
1414
$update: (key: keyof C, value: C[keyof C], configurationTarget?: Nullable<ConfigurationTarget>, overrideInLanguage?: boolean) => Promise<void>
15+
/**
16+
* Set the value without updating the workspace.
17+
*/
18+
$set: (key: keyof C, value: C[keyof C]) => void
1519
}>
1620

1721
/**
@@ -31,5 +35,8 @@ export function defineConfigObject(section: string, configs: Record<string, unkn
3135
$update(key, value, configurationTarget, overrideInLanguage) {
3236
return configRefs[key].update(value, configurationTarget, overrideInLanguage)
3337
},
38+
$set(key, value) {
39+
return configRefs[key].set(value)
40+
},
3441
}) satisfies ConfigObject<typeof configs>
3542
}

packages/core/src/utils/defineConfigs.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export interface ConfigRef<T> extends WritableComputedRef<T> {
1313
* @see https://code.visualstudio.com/api/references/vscode-api#WorkspaceConfiguration.update
1414
*/
1515
update: (value: T, configurationTarget?: ConfigurationTarget | boolean | null, overrideInLanguage?: boolean) => Promise<void>
16+
/**
17+
* Set the value without updating the workspace.
18+
*/
19+
set: (value: T) => void
1620
}
1721

1822
const ConfigTypeSymbol = Symbol('ConfigType')
@@ -74,6 +78,9 @@ export function defineConfigs(section: string, configs: object, scope?: Nullable
7478
await workspaceConfig.update(key, value, configurationTarget, overrideInLanguage)
7579
ref.value = value
7680
}
81+
ref.set = (value) => {
82+
data.value = value
83+
}
7784
return ref
7885
}
7986

@@ -90,7 +97,7 @@ export function defineConfigs(section: string, configs: object, scope?: Nullable
9097
const newWorkspaceConfig = workspace.getConfiguration(section)
9198
for (const key in configs) {
9299
if (e.affectsConfiguration(`${section}.${key}`))
93-
configRefs[key].value = newWorkspaceConfig.get(key) as any
100+
configRefs[key].set(newWorkspaceConfig.get(key) as any)
94101
}
95102
}))
96103
})

0 commit comments

Comments
 (0)