Skip to content

Commit dac7dc6

Browse files
committed
Trigger refresh automatically in the background after teh editor is loaded
1 parent 9e4a8de commit dac7dc6

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

ios/Sources/GutenbergKit/Sources/EditorViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
248248

249249
Task { @MainActor in
250250
do {
251-
self.dependencies = try await service.setup(configuration: configuration)
251+
self.dependencies = try await service.dependencies(for: configuration)
252252
} catch {
253253
print("Failed to setup editor environment, continuing with the default or cached configuration:", error)
254254
}

ios/Sources/GutenbergKit/Sources/Service/EditorService.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ actor EditorService {
5050
URL.applicationDirectory.appendingPathComponent("GutenbergKit", isDirectory: true)
5151
}
5252

53-
/// Set up the editor for the given site.
53+
/// Returns the editor dependencies for the given configuration.
5454
///
5555
/// - warning: The request make take a significant amount of time the first
5656
/// time you open the editor.
57-
func setup(configuration: EditorConfiguration) async throws -> EditorDependencies {
57+
func dependencies(for configuration: EditorConfiguration) async throws -> EditorDependencies {
5858
if !isEditorLoaded {
5959
await refresh(configuration: configuration)
60+
} else {
61+
// Trigger a background refresh after a delay to avoid interfering with editor loading
62+
Task {
63+
log(.info, "Registering background refresh to be performed later")
64+
try? await Task.sleep(for: .seconds(7))
65+
await refresh(configuration: configuration)
66+
}
6067
}
6168
var dependencies = EditorDependencies()
6269
if let data = try? Data(contentsOf: editorSettingsFileURL),
@@ -117,7 +124,7 @@ actor EditorService {
117124
log(.error, "Failed to fetch editor settings: \(error)")
118125
}
119126

120-
guard case .success(let manifestData) = manifestResult else {
127+
guard case .success(let manifest) = manifestResult else {
121128
if case .failure(let error) = manifestResult {
122129
log(.error, "Failed to fetch manifest: \(error)")
123130
}
@@ -131,7 +138,7 @@ actor EditorService {
131138
// Fetch all assets for the new manifest
132139
let assetsStartTime = CFAbsoluteTimeGetCurrent()
133140
do {
134-
try await fetchAssets(manifestData: manifestData)
141+
try await fetchAssets(manifestData: manifest)
135142
} catch {
136143
log(.error, "Failed to fetch assets: \(error)")
137144
log(.error, "Editor refresh aborted: asset fetching failed")
@@ -142,7 +149,7 @@ actor EditorService {
142149
// Only write manifest to disk after all assets are successfully fetched
143150
do {
144151
FileManager.default.createDirectoryIfNeeded(at: storeURL)
145-
try manifestData.write(to: manifestFileURL)
152+
try manifest.write(to: manifestFileURL)
146153
log(.info, "Manifest saved to disk")
147154
} catch {
148155
log(.error, "Failed to save manifest: \(error)")
@@ -152,9 +159,7 @@ actor EditorService {
152159
// Save state to indicate successful refresh
153160
do {
154161
let state = State(refreshDate: Date())
155-
let stateData = try JSONEncoder().encode(state)
156-
try stateData.write(to: stateFileURL)
157-
log(.info, "State saved to disk")
162+
try JSONEncoder().encode(state).write(to: stateFileURL)
158163
} catch {
159164
log(.error, "Failed to save state: \(error)")
160165
}

0 commit comments

Comments
 (0)