Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async functionality to ActionManager Queue #560

Merged
merged 3 commits into from
Apr 7, 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 @@ -578,12 +578,16 @@ + (void)sortByPriority:(NSMutableArray *)actionContexts

- (void)actionDismissed
{
self.actionDidDismiss();
if (self.actionDidDismiss) {
self.actionDidDismiss();
} else {
LPLog(LPError, @"%@: actionDidDismiss not set for context %@", [self class], self);
}
}

-(NSString *)description
{
return [NSString stringWithFormat:@"%@:%@", self.name, self.messageId];
return [NSString stringWithFormat:@"<%p>%@:%@", self, self.name, self.messageId];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Leanplum
//
// Created by Milos Jakovljevic on 2.01.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation

Expand All @@ -29,7 +29,7 @@ extension ActionManager {
}
return
}

// gets the next action from the queue
state.currentAction = queue.pop()
guard let action = state.currentAction else {
Expand All @@ -45,71 +45,85 @@ extension ActionManager {
performAvailableActions()
return
}

// decide if we are going to display the message
// by calling delegate and let it decide what are we supposed to do
let messageDisplayDecision = shouldDisplayMessage?(action.context)

// if message is discarded, early exit
if case .discard = messageDisplayDecision?.decision {
state.currentAction = nil
performAvailableActions()
return
}

// if message is delayed, add it to the scheduler to be delayed
// by the amount of seconds, and exit
if case .delay(let amount) = messageDisplayDecision?.decision {
Log.debug("[ActionManager]: delaying action: \(action.context) for \(amount)s.")

if amount > 0 {
// Schedule for delayed time
scheduler.schedule(action: action, delay: amount)
} else {
// Insert in delayed queue
delayedQueue.pushBack(action)
shouldDisplayMessage(context: action.context) { [weak self] messageDisplayDecision in
// if message is discarded, early exit
if case .discard = messageDisplayDecision?.decision {
self?.state.currentAction = nil
self?.performAvailableActions()
return
}
state.currentAction = nil
performAvailableActions()
return
}

// logic:
// 1) ask client to show view controller
// 2) wait for client to execute action
// 3) ask and wait for client to dismiss view controller

// get the action definition
let definition = definitions.first { $0.name == action.context.name }

// 2) set the execute block which will be called by client
action.context.actionDidExecute = { [weak self] context in
Log.debug("[ActionManager]: actionDidExecute: \(context).")
self?.onMessageAction?(context.name, context)
}

// 3) set the dismiss block which will be called by client
action.context.actionDidDismiss = { [weak self] in
Log.debug("[ActionManager]: actionDidDismiss: \(action.context).")
self?.onMessageDismissed?(action.context)
self?.state.currentAction = nil
self?.performAvailableActions()

// if message is delayed, add it to the scheduler to be delayed
// by the amount of seconds, and exit
if case .delay(let amount) = messageDisplayDecision?.decision {
Log.debug("[ActionManager]: delaying action: \(action.context) for \(amount)s.")

if amount > 0 {
// Schedule for delayed time
self?.scheduler.schedule(action: action, delay: amount)
} else {
// Insert in delayed queue
self?.delayedQueue.pushBack(action)
}
self?.state.currentAction = nil
self?.performAvailableActions()
return
}

// logic:
// 1) ask client to show view controller
// 2) wait for client to execute action
// 3) ask and wait for client to dismiss view controller

// get the action definition
let definition = self?.definitions.first { $0.name == action.context.name }

// 2) set the execute block which will be called by client
action.context.actionDidExecute = { [weak self] context in
Log.debug("[ActionManager]: actionDidExecute: \(context).")
self?.onMessageAction?(context.name, context)
}

// 3) set the dismiss block which will be called by client
action.context.actionDidDismiss = { [weak self] in
Log.debug("[ActionManager]: actionDidDismiss: \(action.context).")
self?.onMessageDismissed?(action.context)
self?.state.currentAction = nil
self?.performAvailableActions()
}

// 1) ask to present, return if its not
guard let handled = definition?.presentAction?(action.context), handled else {
Log.debug("[ActionManager]: action NOT presented: \(action.context).")
self?.state.currentAction = nil
self?.performAvailableActions()
return
}
Log.info("[ActionManager]: action presented: \(action.context).")

// iff handled track that message has been displayed
// propagate event that message is displayed
self?.onMessageDisplayed?(action.context)

// record the impression
self?.recordImpression(action: action)
}

// 1) ask to present, return if its not
guard let handled = definition?.presentAction?(action.context), handled else {
Log.debug("[ActionManager]: action NOT presented: \(action.context).")
state.currentAction = nil
performAvailableActions()
return
}

func shouldDisplayMessage(context: ActionContext, callback: @escaping (MessageDisplayChoice?) -> ()) {
if useAsyncDecisionHandlers {
DispatchQueue.global(qos: .background).async { [weak self] in
let messageDisplayDecision = self?.shouldDisplayMessage?(context)
DispatchQueue.main.async {
callback(messageDisplayDecision)
}
}
} else {
let messageDisplayDecision = self.shouldDisplayMessage?(context)
callback(messageDisplayDecision)
}
Log.info("[ActionManager]: action presented: \(action.context).")

// iff handled track that message has been displayed
// propagate event that message is displayed
onMessageDisplayed?(action.context)

// record the impression
recordImpression(action: action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Leanplum
//
// Created by Milos Jakovljevic on 2.01.22.
// Copyright © 2022 Leanplum. All rights reserved.
// Copyright © 2023 Leanplum. All rights reserved.

import Foundation

Expand All @@ -15,14 +15,14 @@ extension ActionManager {

var name: String {
switch self {
case .high:
return "high"
default:
return "default"
case .high:
return "high"
default:
return "default"
}
}
}

@objc public func trigger(contexts: [Any], priority: Priority = .default, trigger: ActionsTrigger? = nil) {
guard let contexts = contexts as? [ActionContext] else {
return
Expand All @@ -32,21 +32,36 @@ extension ActionManager {
guard let firstContext = contexts.first else {
return
}

// By default, add only one message to queue if `prioritizeMessages` is not implemented
// This ensures backwards compatibility
let filteredActions = prioritizeMessages?(contexts, trigger) ?? [firstContext]
let actions: [Action] = filteredActions.map {
.action(context: $0)
}

Log.debug("[ActionManager]: triggering actions with priority: \(priority.name).")

switch priority {
prioritizeMessages(contexts: contexts, defaultContexts: [firstContext], trigger: trigger) { [self] filteredActions in
let actions: [Action] = filteredActions.map {
.action(context: $0)
}
Log.debug("[ActionManager]: triggering actions with priority: \(priority.name).")
switch priority {
case .high:
insertActions(actions: actions)
self.insertActions(actions: actions)
default:
appendActions(actions: actions)
self.appendActions(actions: actions)
}
}
}

func prioritizeMessages(contexts: [ActionContext], defaultContexts: [ActionContext], trigger: ActionsTrigger? = nil, callback: @escaping ([ActionContext]) -> ()) {
if useAsyncDecisionHandlers {
DispatchQueue.global(qos: .background).async { [weak self] in
let filteredActions = self?.prioritizeMessages?(contexts, trigger) ?? defaultContexts
DispatchQueue.main.async {
callback(filteredActions)
}
}
} else {
let filteredActions = self.prioritizeMessages?(contexts, trigger) ?? defaultContexts
callback(filteredActions)
}
}

Expand Down Expand Up @@ -90,4 +105,3 @@ extension ActionManager {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Foundation
/// `ActionManager.Configuration` of the `ActionManager`
/// Set a new configuration to override a configuration option
public var configuration: Configuration = .default

public var useAsyncDecisionHandlers = false

lazy var queue: Queue = Queue()
lazy var delayedQueue: Queue = Queue()
Expand Down