1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-11-25 19:17:39 +03:00

ref(core): profiles.services config saving should be the caller's responsibility

This commit is contained in:
Clem 2023-08-25 15:26:44 +02:00
parent 49f9a10372
commit 06859de2de
2 changed files with 14 additions and 49 deletions

View File

@ -93,10 +93,9 @@ export class ProfilesService {
/** /**
* Insert a new Profile in config * Insert a new Profile in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
* arg: genId (default: true) -> generate uuid in before pushing Profile into config * arg: genId (default: true) -> generate uuid in before pushing Profile into config
*/ */
async newProfile (profile: PartialProfile<Profile>, saveConfig = true, genId = true): Promise<void> { async newProfile (profile: PartialProfile<Profile>, genId = true): Promise<void> {
if (genId) { if (genId) {
profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}` profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}`
} }
@ -107,17 +106,12 @@ export class ProfilesService {
} }
this.config.store.profiles.push(profile) this.config.store.profiles.push(profile)
if (saveConfig) {
return this.config.save()
}
} }
/** /**
* Write a Profile in config * Write a Profile in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
*/ */
async writeProfile (profile: PartialProfile<Profile>, saveConfig = true): Promise<void> { async writeProfile (profile: PartialProfile<Profile>): Promise<void> {
const cProfile = this.config.store.profiles.find(p => p.id === profile.id) const cProfile = this.config.store.profiles.find(p => p.id === profile.id)
if (cProfile) { if (cProfile) {
if (!profile.group) { if (!profile.group) {
@ -125,18 +119,13 @@ export class ProfilesService {
} }
Object.assign(cProfile, profile) Object.assign(cProfile, profile)
if (saveConfig) {
return this.config.save()
}
} }
} }
/** /**
* Delete a Profile from config * Delete a Profile from config
* arg: saveConfig (default: true) -> invoke after the Profile was deleted
*/ */
async deleteProfile (profile: PartialProfile<Profile>, saveConfig = true): Promise<void> { async deleteProfile (profile: PartialProfile<Profile>): Promise<void> {
this.providerForProfile(profile)?.deleteProfile(this.getConfigProxyForProfile(profile)) this.providerForProfile(profile)?.deleteProfile(this.getConfigProxyForProfile(profile))
this.config.store.profiles = this.config.store.profiles.filter(p => p.id !== profile.id) this.config.store.profiles = this.config.store.profiles.filter(p => p.id !== profile.id)
@ -147,18 +136,13 @@ export class ProfilesService {
delete profileHotkeys[profileHotkeyName] delete profileHotkeys[profileHotkeyName]
this.config.store.hotkeys.profile = profileHotkeys this.config.store.hotkeys.profile = profileHotkeys
} }
if (saveConfig) {
return this.config.save()
}
} }
/** /**
* Delete all Profiles from config using option filter * Delete all Profiles from config using option filter
* arg: options { group: string } -> options used to filter which profile have to be deleted * arg: options { group: string } -> options used to filter which profile have to be deleted
* arg: saveConfig (default: true) -> invoke after the Profile was deleted
*/ */
async deleteBulkProfiles (options: { group: string }, saveConfig = true): Promise<void> { async deleteBulkProfiles (options: { group: string }): Promise<void> {
for (const profile of this.config.store.profiles.filter(p => p.group === options.group)) { for (const profile of this.config.store.profiles.filter(p => p.group === options.group)) {
this.providerForProfile(profile)?.deleteProfile(this.getConfigProxyForProfile(profile)) this.providerForProfile(profile)?.deleteProfile(this.getConfigProxyForProfile(profile))
@ -172,10 +156,6 @@ export class ProfilesService {
} }
this.config.store.profiles = this.config.store.profiles.filter(p => p.group !== options.group) this.config.store.profiles = this.config.store.profiles.filter(p => p.group !== options.group)
if (saveConfig) {
return this.config.save()
}
} }
async openNewTabForProfile <P extends Profile> (profile: PartialProfile<P>): Promise<BaseTabComponent|null> { async openNewTabForProfile <P extends Profile> (profile: PartialProfile<P>): Promise<BaseTabComponent|null> {
@ -464,10 +444,9 @@ export class ProfilesService {
/** /**
* Insert a new ProfileGroup in config * Insert a new ProfileGroup in config
* arg: saveConfig (default: true) -> invoke after the Profile was updated
* arg: genId (default: true) -> generate uuid in before pushing Profile into config * arg: genId (default: true) -> generate uuid in before pushing Profile into config
*/ */
async newProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true, genId = true): Promise<void> { async newProfileGroup (group: PartialProfileGroup<ProfileGroup>, genId = true): Promise<void> {
if (genId) { if (genId) {
group.id = `${uuidv4()}` group.id = `${uuidv4()}`
} }
@ -478,47 +457,33 @@ export class ProfilesService {
} }
this.config.store.groups.push(group) this.config.store.groups.push(group)
if (saveConfig) {
return this.config.save()
}
} }
/** /**
* Write a ProfileGroup in config * Write a ProfileGroup in config
* arg: saveConfig (default: true) -> invoke after the ProfileGroup was updated
*/ */
async writeProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true): Promise<void> { async writeProfileGroup (group: PartialProfileGroup<ProfileGroup>): Promise<void> {
delete group.profiles delete group.profiles
delete group.editable delete group.editable
const cGroup = this.config.store.groups.find(g => g.id === group.id) const cGroup = this.config.store.groups.find(g => g.id === group.id)
if (cGroup) { if (cGroup) {
Object.assign(cGroup, group) Object.assign(cGroup, group)
if (saveConfig) {
return this.config.save()
}
} }
} }
/** /**
* Delete a ProfileGroup from config * Delete a ProfileGroup from config
* arg: saveConfig (default: true) -> invoke after the ProfileGroup was deleted
*/ */
async deleteProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true, deleteProfiles = true): Promise<void> { async deleteProfileGroup (group: PartialProfileGroup<ProfileGroup>, deleteProfiles = true): Promise<void> {
this.config.store.groups = this.config.store.groups.filter(g => g.id !== group.id) this.config.store.groups = this.config.store.groups.filter(g => g.id !== group.id)
if (deleteProfiles) { if (deleteProfiles) {
await this.deleteBulkProfiles({ group: group.id }, false) await this.deleteBulkProfiles({ group: group.id })
} else { } else {
for (const profile of this.config.store.profiles.filter(x => x.group === group.id)) { for (const profile of this.config.store.profiles.filter(x => x.group === group.id)) {
delete profile.group delete profile.group
} }
} }
if (saveConfig) {
return this.config.save()
}
} }
/** /**

View File

@ -88,7 +88,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
const cfgProxy = this.profilesService.getConfigProxyForProfile(profile) const cfgProxy = this.profilesService.getConfigProxyForProfile(profile)
profile.name = this.profilesService.providerForProfile(profile)?.getSuggestedName(cfgProxy) ?? this.translate.instant('{name} copy', base) profile.name = this.profilesService.providerForProfile(profile)?.getSuggestedName(cfgProxy) ?? this.translate.instant('{name} copy', base)
} }
this.profilesService.newProfile(profile) this.profilesService.newProfile(profile).then(() => this.config.save())
} }
async editProfile (profile: PartialProfile<Profile>): Promise<void> { async editProfile (profile: PartialProfile<Profile>): Promise<void> {
@ -97,7 +97,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
return return
} }
Object.assign(profile, result) Object.assign(profile, result)
await this.profilesService.writeProfile(profile) await this.profilesService.writeProfile(profile).then(() => this.config.save())
} }
async showProfileEditModal (profile: PartialProfile<Profile>): Promise<PartialProfile<Profile>|null> { async showProfileEditModal (profile: PartialProfile<Profile>): Promise<PartialProfile<Profile>|null> {
@ -140,7 +140,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
cancelId: 1, cancelId: 1,
}, },
)).response === 0) { )).response === 0) {
this.profilesService.deleteProfile(profile) this.profilesService.deleteProfile(profile).then(() => this.config.save())
} }
} }
@ -149,7 +149,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
modal.componentInstance.prompt = this.translate.instant('New group name') modal.componentInstance.prompt = this.translate.instant('New group name')
const result = await modal.result.catch(() => null) const result = await modal.result.catch(() => null)
if (result?.value.trim()) { if (result?.value.trim()) {
await this.profilesService.newProfileGroup({ id: '', name: result.value }) await this.profilesService.newProfileGroup({ id: '', name: result.value }).then(() => this.config.save())
} }
} }
@ -159,7 +159,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
return return
} }
Object.assign(group, result) Object.assign(group, result)
await this.profilesService.writeProfileGroup(ProfilesSettingsTabComponent.collapsableIntoPartialProfileGroup(group)) await this.profilesService.writeProfileGroup(ProfilesSettingsTabComponent.collapsableIntoPartialProfileGroup(group)).then(() => this.config.save())
} }
async showProfileGroupEditModal (group: PartialProfileGroup<CollapsableProfileGroup>): Promise<PartialProfileGroup<CollapsableProfileGroup>|null> { async showProfileGroupEditModal (group: PartialProfileGroup<CollapsableProfileGroup>): Promise<PartialProfileGroup<CollapsableProfileGroup>|null> {
@ -239,7 +239,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
deleteProfiles = true deleteProfiles = true
} }
await this.profilesService.deleteProfileGroup(group, true, deleteProfiles) await this.profilesService.deleteProfileGroup(group, deleteProfiles).then(() => this.config.save())
} }
} }