-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a simple state machine to identify incorrect transitions (#3660)
Task/Issue URL: https://app.asana.com/0/0/1208878879182791/f Tech Design URL: https://app.asana.com/0/481882893211075/1208859623176995/f CC: @bwaresiak **Description**: 1. Implement simple state machine with empty states and AppDelegate lifecycle event triggers 2. Fire a pixel on incorrect transition **Steps to test this PR**: 1. Duplicate any event, e.g. write appStateMachine.handle(.backgrounding(application)) twice 2. Go to background and see the pixel `m_debug_app-did-transition-to-unexpected-state` is being sent
- Loading branch information
Showing
11 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// AppStateMachine.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import UIKit | ||
|
||
enum AppEvent { | ||
|
||
case launching(UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]?) | ||
case activating(UIApplication) | ||
case backgrounding(UIApplication) | ||
case suspending(UIApplication) | ||
|
||
case openURL(URL) | ||
|
||
} | ||
|
||
protocol AppState { | ||
|
||
func apply(event: AppEvent) -> any AppState | ||
|
||
} | ||
|
||
protocol AppEventHandler { | ||
|
||
func handle(_ event: AppEvent) | ||
|
||
} | ||
|
||
final class AppStateMachine: AppEventHandler { | ||
|
||
private(set) var currentState: any AppState = Init() | ||
|
||
func handle(_ event: AppEvent) { | ||
currentState = currentState.apply(event: event) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// AppStateTransitions.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import os.log | ||
import Core | ||
|
||
extension Init { | ||
|
||
func apply(event: AppEvent) -> any AppState { | ||
switch event { | ||
case .launching(let application, let launchOptions): | ||
return Launched(application: application, launchOptions: launchOptions) | ||
default: | ||
return handleUnexpectedEvent(event) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Launched { | ||
|
||
func apply(event: AppEvent) -> any AppState { | ||
switch event { | ||
case .activating(let application): | ||
return Active(application: application) | ||
case .openURL: | ||
return self | ||
case .launching, .suspending, .backgrounding: | ||
return handleUnexpectedEvent(event) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Active { | ||
|
||
func apply(event: AppEvent) -> any AppState { | ||
switch event { | ||
case .suspending(let application): | ||
return Inactive(application: application) | ||
case .launching, .activating, .backgrounding, .openURL: | ||
return handleUnexpectedEvent(event) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Inactive { | ||
|
||
func apply(event: AppEvent) -> any AppState { | ||
switch event { | ||
case .backgrounding(let application): | ||
return Background(application: application) | ||
case .activating(let application): | ||
return Active(application: application) | ||
case .launching, .suspending, .openURL: | ||
return handleUnexpectedEvent(event) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension Background { | ||
|
||
func apply(event: AppEvent) -> any AppState { | ||
switch event { | ||
case .activating(let application): | ||
return Active(application: application) | ||
case .openURL: | ||
return self | ||
case .launching, .suspending, .backgrounding: | ||
return handleUnexpectedEvent(event) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension AppEvent { | ||
|
||
var rawValue: String { | ||
switch self { | ||
case .launching: return "launching" | ||
case .activating: return "activating" | ||
case .backgrounding: return "backgrounding" | ||
case .suspending: return "suspending" | ||
case .openURL: return "openURL" | ||
} | ||
} | ||
|
||
} | ||
|
||
extension AppState { | ||
|
||
func handleUnexpectedEvent(_ event: AppEvent) -> Self { | ||
Logger.lifecycle.error("Invalid transition (\(event.rawValue)) for state (\(type(of: self)))") | ||
DailyPixel.fireDailyAndCount(pixel: .appDidTransitionToUnexpectedState, | ||
withAdditionalParameters: [PixelParameters.appState: String(describing: type(of: self)), | ||
PixelParameters.appEvent: event.rawValue]) | ||
return self | ||
} | ||
|
||
} |
Oops, something went wrong.