-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from superwall-me/develop
v3.4.1
- Loading branch information
Showing
41 changed files
with
222 additions
and
86 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
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 |
---|---|---|
|
@@ -18,5 +18,5 @@ let sdkVersion = """ | |
*/ | ||
|
||
let sdkVersion = """ | ||
3.4.0 | ||
3.4.1 | ||
""" |
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,67 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Yusuf Tör on 28/09/2023. | ||
// | ||
// https://github.com/kodecocodes/swift-algorithm-club/blob/master/Queue/README.markdown#a-more-efficient-queue | ||
|
||
import Foundation | ||
|
||
/** | ||
First-in first-out queue (FIFO) | ||
Note: This isn't threadsafe. | ||
New elements are added to the end of the queue. Dequeuing pulls elements from | ||
the front of the queue. | ||
Enqueuing and dequeuing are O(1) operations. | ||
*/ | ||
struct Queue<T> { | ||
private var array: [T?] = [] | ||
private var head = 0 | ||
|
||
var isEmpty: Bool { | ||
// swiftlint:disable:next empty_count | ||
return count == 0 | ||
} | ||
|
||
var count: Int { | ||
return array.count - head | ||
} | ||
|
||
mutating func enqueue(_ element: T) { | ||
array.append(element) | ||
} | ||
|
||
mutating func dequeue() -> T? { | ||
// Get element at head of queue | ||
guard let element = array[guarded: head] else { | ||
return nil | ||
} | ||
|
||
// Replace the element with nil and increment the head | ||
array[head] = nil | ||
head += 1 | ||
|
||
// Calculate the percentage of the array that is nil | ||
let percentage = Double(head) / Double(array.count) | ||
|
||
// If more than 25% of the queue is nil, chop off the head and reset | ||
// head to 0 | ||
if array.count > 50 && percentage > 0.25 { | ||
array.removeFirst(head) | ||
head = 0 | ||
} | ||
|
||
return element | ||
} | ||
|
||
var front: T? { | ||
if isEmpty { | ||
return nil | ||
} else { | ||
return array[head] | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Sources/SuperwallKit/Misc/Data Structures/SerialTaskManager.swift
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 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Yusuf Tör on 28/09/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Serially executes tasks added to it. | ||
final class SerialTaskManager { | ||
private var taskQueue: Queue<() async -> Void> = Queue() | ||
private var dispatchQueue = DispatchQueue(label: "com.superwall.serial-task-queue") | ||
|
||
func addTask(_ task: @escaping () async -> Void) { | ||
dispatchQueue.async { [weak self] in | ||
guard let self = self else { | ||
return | ||
} | ||
|
||
// Add the task to the queue | ||
self.taskQueue.enqueue(task) | ||
|
||
// If there's only one task in the queue, start executing it | ||
if self.taskQueue.count == 1 { | ||
self.executeNextTask() | ||
} | ||
} | ||
} | ||
|
||
private func executeNextTask() { | ||
dispatchQueue.async { [weak self] in | ||
guard let self = self else { | ||
return | ||
} | ||
// Check if there are tasks in the queue | ||
if taskQueue.isEmpty { | ||
return | ||
} | ||
|
||
// Get the next task from the queue | ||
guard let nextTask = taskQueue.dequeue() else { | ||
return | ||
} | ||
|
||
Task { | ||
await nextTask() | ||
// After the task completes, recursively execute the next task | ||
self.executeNextTask() | ||
} | ||
} | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.