File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ final public class OpenAI: OpenAIProtocol {
35
35
}
36
36
37
37
private let session : URLSessionProtocol
38
- private var streamingSessions : [ NSObject ] = [ ]
38
+ private var streamingSessions = ArrayWithThreadSafety < NSObject > ( )
39
39
40
40
public let configuration : Configuration
41
41
Original file line number Diff line number Diff line change
1
+ //
2
+ // ArrayWithThreadSafety.swift
3
+ //
4
+ //
5
+ // Created by James J Kalafus on 2024-02-01.
6
+ //
7
+
8
+ import Foundation
9
+
10
+ internal class ArrayWithThreadSafety < Element> {
11
+ private var array = [ Element] ( )
12
+ private let queue = DispatchQueue ( label: " us.kalaf.OpenAI.threadSafeArray " , attributes: . concurrent)
13
+
14
+ @inlinable public func append( _ element: Element ) {
15
+ queue. async ( flags: . barrier) {
16
+ self . array. append ( element)
17
+ }
18
+ }
19
+
20
+ @inlinable public func removeAll( where shouldBeRemoved: @escaping ( Element ) throws -> Bool ) rethrows {
21
+ try queue. sync ( flags: . barrier) {
22
+ try self . array. removeAll ( where: shouldBeRemoved)
23
+ }
24
+ }
25
+ }
26
+
You can’t perform that action at this time.
0 commit comments