codes
// 获取当前窗口或请求关联的服务实例。
const service = () => {
// Keep the synchronization engine out of the startup path. Reading the
// lightweight timer setting below does not initialize a repository client.
servicePromise ||= Promise.resolve().then(() => {
const { SyncManager } = (require('./sync/manager') as typeof import('./sync/manager'));
return new SyncManager({ userDataPath, safeStorage, fetchImpl });
});
return servicePromise;
};
// 笔记库全局状态:保存当前修改,供后续读取与恢复。
async save() {
// Multiple windows share this store. Serialize snapshots so an older write
// cannot finish after a newer mutation and roll recent-workspace metadata
// back on disk.
this.savePromise = this.savePromise.catch(() => {
}).then(async () => {
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
await atomicWriteJson(this.filePath, this.state);
});
return this.savePromise;
}
// 保存仅本地使用的配置;整个 localSettings 对象均不进入云同步快照。
async updateLocalSettings(input: Partial<LocalSettings>) {
return this.queueSettingsMutation(async () => {
const state = await this.load();
if ('hotkeysEnabled' in input && typeof input.hotkeysEnabled !== 'boolean') {
throw new Error('启用热键必须是布尔值');
}
const transaction = this.prepareLocalSettings?.(input);
const previous = state.localSettings;
state.localSettings = { ...previous, ...input };
try {
await this.save();
transaction?.commit();
} catch (error) {
state.localSettings = previous;
transaction?.rollback();
throw error;
}
for (const listener of this.localSettingsListeners) {
try {
listener({ ...state.localSettings });
} catch {
// A closing renderer must not turn a committed save into a failure.
}
}
return state.localSettings;
});
}
// 串行合并设置修改,避免异步保存覆盖较新的设置。
queueSettingsMutation<T>(mutate: () => Promise<T>): Promise<T> {
const task = this.settingsPromise.catch(() => {
}).then(mutate);
this.settingsPromise = task;
return task;
}
document.documentElement.style.setProperty('--editor-font-size', `${fontSize}px`);
let hotkeysEnabled = !(await stateStore.publicState()).localSettings.hotkeysEnabled;
await stateStore.updateLocalSettings({ hotkeysEnabled });