Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Do less work when block editor is disabled #1036

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ struct MemoEditorDetailView: View {

@StateObject private var blockEditorStore = Store(
state: BlockEditor.Model.draft(),
action: .start,
environment: AppEnvironment.default,
loggingEnabled: true,
logger: blockEditorStoreLogger
Expand Down
96 changes: 74 additions & 22 deletions xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ extension BlockEditor {
)
)
}

// Is block editor enabled?
var isEnabled = false
/// Is editor in loading state?
var loadingState = LoadingState.loading
/// When was the last time the editor issued a fetch from source of truth?
var lastLoadStarted: Date? = nil
/// Is polling? We use this to drive autosaves.
var isPolling = false

/// Is editor saved?
private(set) var saveState = SaveState.saved
Expand Down Expand Up @@ -76,14 +79,16 @@ extension BlockEditor {
extension BlockEditor {
// MARK: Actions
enum Action {
/// Sent once when store is created
case start
/// View is ready for updates.
/// Sent during viewDidLoad after performing first view update for
/// initial state and subscribing to changes.
case ready
/// Sent from SwiftUI land when the wrapping SwiftUI view appears.
case appear(MemoEditorDetailDescription)
/// Start polling if needed
case startPolling
/// Perform a single polling step, optionally requesting another step
case poll
case setOurSphere(Did?)
/// Get owner Did from peer
case resolveOwnerSphere
Expand Down Expand Up @@ -237,11 +242,6 @@ extension BlockEditor.Model: ModelProtocol {
environment: Environment
) -> Update {
switch action {
case .start:
return start(
state: state,
environment: environment
)
case .ready:
return ready(
state: state,
Expand All @@ -253,6 +253,16 @@ extension BlockEditor.Model: ModelProtocol {
description: description,
environment: environment
)
case .startPolling:
return startPolling(
state: state,
environment: environment
)
case .poll:
return poll(
state: state,
environment: environment
)
case let .setOurSphere(did):
return setOurSphere(
state: state,
Expand Down Expand Up @@ -500,18 +510,6 @@ extension BlockEditor.Model: ModelProtocol {
}
}

static func start(
state: Self,
environment: Environment
) -> Update {
/// Poll and autosave until this store is destroyed.
let pollFx: Fx<Action> = AppEnvironment
.poll(every: Config.default.pollingInterval)
.map({ _ in .autosave })
.eraseToAnyPublisher()
return Update(state: state, fx: pollFx)
}

static func ready(
state: Self,
environment: Environment
Expand All @@ -524,6 +522,14 @@ extension BlockEditor.Model: ModelProtocol {
description: MemoEditorDetailDescription,
environment: Environment
) -> Update {
var model = state
model.isEnabled = AppDefaults.standard.isBlockEditorEnabled

guard model.isEnabled else {
logger.info("Block editor is disabled. Skipping appear.")
return Update(state: state)
}

let fetchOurSphere: Fx<Action> = Future.detached {
Action.setOurSphere(try? await environment.noosphere.identity())
}
Expand All @@ -537,13 +543,59 @@ extension BlockEditor.Model: ModelProtocol {
)
.eraseToAnyPublisher()

let startPolling: Fx<Action> = Just(
Action.startPolling
)
.eraseToAnyPublisher()

let fx: Fx<Action> = fetchOurSphere
.merge(with: loadEditor)
.merge(with: loadEditor, startPolling)
.eraseToAnyPublisher()

return Update(state: state, fx: fx)
return Update(state: model, fx: fx)
}

static func startPolling(
state: Self,
environment: Environment
) -> Update {
guard state.isEnabled else {
logger.info("Skipping polling start. Block editor is disabled.")
return Update(state: state)
}
guard !state.isPolling else {
logger.info("Skipping polling start. Already polling.")
return Update(state: state)
}
var model = state
model.isPolling = true

let pollFx: Fx<Action> = Just(Action.poll).delay(
for: .seconds(Config.default.pollingInterval),
scheduler: DispatchQueue.main
).eraseToAnyPublisher()

return Update(state: state, fx: pollFx)
}

static func poll(
state: Self,
environment: Environment
) -> Update {
let pollFx: Fx<Action> = Just(Action.poll).delay(
for: .seconds(Config.default.pollingInterval),
scheduler: DispatchQueue.main
).eraseToAnyPublisher()

let autosaveFx: Fx<Action> = Just(Action.autosave)
.eraseToAnyPublisher()

return Update(
state: state,
fx: pollFx.merge(with: autosaveFx).eraseToAnyPublisher()
)
}

static func setOurSphere(
state: Self,
did: Did?,
Expand Down
2 changes: 1 addition & 1 deletion xcode/Subconscious/Subconscious.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2988,7 +2988,7 @@
repositoryURL = "https://github.com/gordonbrander/ObservableStore";
requirement = {
kind = exactVersion;
version = 0.6.0;
version = 0.6.1;
};
};
B82C3A6D26F6B1C000833CC8 /* XCRemoteSwiftPackageReference "swift-collections" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/gordonbrander/ObservableStore",
"state" : {
"revision" : "371a014b535fe9941f71041f1bded9e4e7a49fba",
"version" : "0.6.0"
"revision" : "41f3356de1b7cd289d2e95fdbff0a15b960e0a6b",
"version" : "0.6.1"
}
},
{
Expand Down
Loading