Skip to content

Commit

Permalink
Merge pull request #1839 from floccusaddon/refactor/partial-set-data-…
Browse files Browse the repository at this point in the history
…with-lock

refactor(Account#setData): Accept partial data and use lock to set data
  • Loading branch information
marcelklehr authored Jan 22, 2025
2 parents 4f00062 + ccc9c86 commit bcca19b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 61 deletions.
12 changes: 6 additions & 6 deletions ios/App/App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "org.handmadeideas.floccus.new-bookmark";
Expand Down Expand Up @@ -397,7 +397,7 @@
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks";
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "org.handmadeideas.floccus.new-bookmark";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -459,7 +459,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -511,7 +511,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand All @@ -537,7 +537,7 @@
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" \"-DDEBUG\"";
PRODUCT_BUNDLE_IDENTIFIER = org.handmadeideas.floccus;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -567,7 +567,7 @@
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 5.4.0;
MARKETING_VERSION = 5.4.1;
PRODUCT_BUNDLE_IDENTIFIER = org.handmadeideas.floccus;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
17 changes: 17 additions & 0 deletions ios/App/PrivacyInfo.xcprivacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>CA92.1</string>
</array>
</dict>
</array>
</dict>
</plist>
27 changes: 15 additions & 12 deletions src/lib/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Mappings from './Mappings'
import { isTest } from './isTest'
import CachingAdapter from './adapters/Caching'
import * as Sentry from '@sentry/vue'
import AsyncLock from 'async-lock'

declare const DEBUG: boolean

Expand All @@ -28,6 +29,8 @@ AdapterFactory.register('fake', async() => (await import('./adapters/Fake')).def
// 2h
const LOCK_TIMEOUT = 1000 * 60 * 60 * 2

const dataLock = new AsyncLock()

export default class Account {
static cache = {}
static singleton : IAccount
Expand Down Expand Up @@ -96,7 +99,7 @@ export default class Account {
}

getData():IAccountData {
const defaults = {
const data = {
localRoot: null,
strategy: 'default' as TAccountStrategy,
syncInterval: 15,
Expand All @@ -106,9 +109,9 @@ export default class Account {
label: '',
errorCount: 0,
clickCountEnabled: false,
...this.server.getData()
}
const data = Object.assign(defaults, this.server.getData())
if (data.type === 'nextcloud-folders') {
if ('type' in data && data.type === 'nextcloud-folders') {
data.type = 'nextcloud-bookmarks'
}
return data
Expand All @@ -122,9 +125,12 @@ export default class Account {
return this.server
}

async setData(data:IAccountData):Promise<void> {
await this.storage.setAccountData(data, null)
this.server.setData(data)
async setData(data:Partial<IAccountData>):Promise<void> {
await dataLock.acquire(this.id, async() => {
const d = {...this.server.getData(), ...data}
await this.storage.setAccountData(d, null)
this.server.setData(d)
})
}

async updateFromStorage():Promise<void> {
Expand Down Expand Up @@ -163,7 +169,7 @@ export default class Account {
Logger.log('Starting sync process for account ' + this.getLabel())
Sentry.setUser({ id: this.id })
this.syncing = true
await this.setData({ ...this.getData(), syncing: 0.05, scheduled: false, error: null })
await this.setData({ syncing: 0.05, scheduled: false, error: null })

if (!(await this.isInitialized())) {
await this.init()
Expand All @@ -184,7 +190,6 @@ export default class Account {
status = await this.server.onSyncStart(false, true)
} else {
await this.setData({
...this.getData(),
error: null,
syncing: false,
scheduled: strategy || this.getData().strategy
Expand Down Expand Up @@ -277,7 +282,7 @@ export default class Account {
await this.syncProcess.sync()
}

await this.setData({ ...this.getData(), scheduled: false, syncing: 1 })
await this.setData({ scheduled: false, syncing: 1 })

// update cache
const cache = (await localResource.getBookmarksTree()).clone(false)
Expand All @@ -297,7 +302,6 @@ export default class Account {

await this.storage.setCurrentContinuation(null)
await this.setData({
...this.getData(),
error: null,
errorCount: 0,
syncing: false,
Expand Down Expand Up @@ -326,7 +330,6 @@ export default class Account {
}

await this.setData({
...this.getData(),
error: message,
errorCount: this.getData().errorCount + 1,
syncing: false,
Expand Down Expand Up @@ -365,7 +368,7 @@ export default class Account {
if (!this.syncing) {
return
}
await this.setData({ ...this.getData(), syncing: progress })
await this.setData({ syncing: progress })
if (!this.syncProcess) {
return
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib/browser/BrowserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export default class BrowserController {

const status = await this.getStatus()
if (status === STATUS_SYNCING) {
await account.setData({ ...account.getData(), scheduled: account.getData().scheduled || true })
await account.setData({ scheduled: account.getData().scheduled || true })
return
}

Expand All @@ -316,7 +316,7 @@ export default class BrowserController {
async scheduleAll() {
const accounts = await Account.getAllAccounts()
for (const account of accounts) {
await account.setData({...account.getData(), scheduled: true})
await account.setData({ scheduled: true })
}
this.updateStatus()
}
Expand All @@ -325,7 +325,7 @@ export default class BrowserController {
let account = await Account.get(accountId)
// Avoid starting it again automatically
if (!keepEnabled) {
await account.setData({ ...account.getData(), enabled: false })
await account.setData({ enabled: false })
}
await account.cancelSync()
}
Expand Down Expand Up @@ -421,7 +421,6 @@ export default class BrowserController {
accounts.map(async acc => {
if (acc.getData().syncing) {
await acc.setData({
...acc.getData(),
syncing: false,
scheduled: acc.getData().enabled,
})
Expand Down
5 changes: 2 additions & 3 deletions src/lib/native/NativeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class NativeController {

const status = await this.getStatus()
if (status === STATUS_SYNCING) {
await account.setData({ ...account.getData(), scheduled: account.getData().scheduled || true })
await account.setData({ scheduled: account.getData().scheduled || true })
return
}

Expand All @@ -135,7 +135,7 @@ export default class NativeController {
let account = await Account.get(accountId)
// Avoid starting it again automatically
if (!keepEnabled) {
await account.setData({ ...account.getData(), enabled: false })
await account.setData({ enabled: false })
}
await account.cancelSync()
}
Expand Down Expand Up @@ -202,7 +202,6 @@ export default class NativeController {
accounts.map(async acc => {
if (acc.getData().syncing) {
await acc.setData({
...acc.getData(),
syncing: false,
scheduled: false,
})
Expand Down
Loading

0 comments on commit bcca19b

Please sign in to comment.