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 2 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
100 changes: 75 additions & 25 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 @@ -68,14 +71,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
/// Set document source location
case loadEditor(
address: Slashlink?,
Expand Down Expand Up @@ -210,11 +215,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 @@ -226,6 +226,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 .loadEditor(address, fallback, autofocus):
return loadEditor(
state: state,
Expand Down Expand Up @@ -400,18 +410,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 @@ -424,16 +422,68 @@ 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)
}

return update(
state: state,
action: .loadEditor(
address: description.address,
fallback: description.fallback
),
state: model,
actions: [
.loadEditor(
address: description.address,
fallback: description.fallback
),
.startPolling
],
environment: environment
)
}

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 loadEditor(
state: Self,
address: Slashlink?,
Expand Down
4 changes: 2 additions & 2 deletions xcode/Subconscious/Subconscious.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2981,8 +2981,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/gordonbrander/ObservableStore";
requirement = {
kind = exactVersion;
version = 0.6.0;
branch = "2023-12-18-manual-fx-management";
kind = branch;
};
};
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"
"branch" : "2023-12-18-manual-fx-management",
"revision" : "380b614fdaf6d9f86d352d550e13a7c8492253eb"
}
},
{
Expand Down
Loading