Skip to content

Commit d144fb5

Browse files
committed
reduce the number of fle reads
1 parent 7b70618 commit d144fb5

1 file changed

Lines changed: 50 additions & 18 deletions

File tree

extension/Userscripts Extension/Functions.swift

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,15 @@ func getManifest() -> Manifest {
283283
}
284284
}
285285

286-
func updateManifestMatches() -> Bool {
287-
guard let files = getAllFiles() else {return false}
286+
func updateManifestMatches(_ optionalFilesArray: [[String: Any]] = []) -> Bool {
287+
// only get all files if files were not provided
288+
var files = [[String: Any]]()
289+
if optionalFilesArray.count < 1 {
290+
guard let getFiles = getAllFiles() else {return false}
291+
files = getFiles
292+
} else {
293+
files = optionalFilesArray
294+
}
288295
var manifest = getManifest()
289296
for file in files {
290297
// can be force unwrapped because getAllFiles didn't return nil
@@ -379,8 +386,15 @@ func updatePatternDict(_ filename: String, _ filePatterns: [String], _ manifestK
379386
return returnDictionary
380387
}
381388

382-
func updateManifestRequired() -> Bool {
383-
guard let files = getAllFiles() else {return false}
389+
func updateManifestRequired(_ optionalFilesArray: [[String: Any]] = []) -> Bool {
390+
// only get all files if files were not provided
391+
var files = [[String: Any]]()
392+
if optionalFilesArray.count < 1 {
393+
guard let getFiles = getAllFiles() else {return false}
394+
files = getFiles
395+
} else {
396+
files = optionalFilesArray
397+
}
384398
var manifest = getManifest()
385399
for file in files {
386400
// can be force unwrapped because getAllFiles didn't return nil
@@ -417,17 +431,24 @@ func updateManifestRequired() -> Bool {
417431
return true
418432
}
419433

420-
func purgeManifest() -> Bool {
434+
func purgeManifest(_ optionalFilesArray: [[String: Any]] = []) -> Bool {
421435
// purge all manifest keys of any stale entries
422436
var update = false, manifest = getManifest(), allSaveLocationFilenames = [String]()
423-
let allFiles = getAllFiles() ?? []
437+
// only get all files if files were not provided
438+
var allFiles = [[String: Any]]()
439+
if optionalFilesArray.count < 1 {
440+
// if getAllFiles fails to return, ignore and pass an empty array
441+
let getFiles = getAllFiles() ?? []
442+
allFiles = getFiles
443+
} else {
444+
allFiles = optionalFilesArray
445+
}
424446
// populate array with filenames
425447
for file in allFiles {
426448
if let filename = file["filename"] as? String {
427449
allSaveLocationFilenames.append(filename)
428450
}
429451
}
430-
431452
// loop through manifest keys, if no file exists for value, remove value from manifest
432453
// if there are no more filenames in pattern, remove pattern from manifest
433454
for (pattern, filenames) in manifest.match {
@@ -664,8 +685,18 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri
664685
return true
665686
}
666687

667-
func checkForRemoteUpdates() -> [[String: String]]? {
668-
guard let files = getAllFiles() else {return nil}
688+
func checkForRemoteUpdates(_ optionalFilesArray: [[String: Any]] = []) -> [[String: String]]? {
689+
// only get all files if files were not provided
690+
var files = [[String: Any]]()
691+
if optionalFilesArray.count < 1 {
692+
guard let getFiles = getAllFiles() else {
693+
err("checkForRemoteUpdates failed at (1)")
694+
return nil
695+
}
696+
files = getFiles
697+
} else {
698+
files = optionalFilesArray
699+
}
669700
var hasUpdates = [[String: String]]()
670701
for file in files {
671702
// can be force unwrapped because getAllFiles didn't return nil
@@ -724,10 +755,10 @@ func getRemoteFileContents(_ url: String) -> String? {
724755
return contents
725756
}
726757

727-
func updateAllFiles() -> Bool {
758+
func updateAllFiles(_ optionalFilesArray: [[String: Any]] = []) -> Bool {
728759
// get names of all files with updates available
729760
guard
730-
let filesWithUpdates = checkForRemoteUpdates(),
761+
let filesWithUpdates = checkForRemoteUpdates(optionalFilesArray),
731762
let saveLocation = getSaveLocation()
732763
else {
733764
err("failed to update files (1)")
@@ -1204,9 +1235,9 @@ func getPopupMatches(_ url: String, _ subframeUrls: [String], _ shouldUpdate: Bo
12041235
}
12051236
if shouldUpdate {
12061237
guard
1207-
updateManifestMatches(),
1208-
updateManifestRequired(),
1209-
purgeManifest()
1238+
updateManifestMatches(files),
1239+
updateManifestRequired(files),
1240+
purgeManifest(files)
12101241
else {
12111242
err("getPopupMatches failed at (2)")
12121243
return nil
@@ -1240,10 +1271,11 @@ func getPopupMatches(_ url: String, _ subframeUrls: [String], _ shouldUpdate: Bo
12401271

12411272
func popupUpdateAll() -> Bool {
12421273
guard
1243-
updateAllFiles(),
1244-
updateManifestMatches(),
1245-
updateManifestRequired(),
1246-
purgeManifest()
1274+
let files = getAllFiles(),
1275+
updateAllFiles(files),
1276+
updateManifestMatches(files),
1277+
updateManifestRequired(files),
1278+
purgeManifest(files)
12471279
else {
12481280
return false
12491281
}

0 commit comments

Comments
 (0)