You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.4 KiB

2 years ago
  1. //
  2. // SocketIOClientConfiguration.swift
  3. // Socket.IO-Client-Swift
  4. //
  5. // Created by Erik Little on 8/13/16.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. /// An array-like type that holds `SocketIOClientOption`s
  25. public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collection, MutableCollection {
  26. // MARK: Typealiases
  27. /// Type of element stored.
  28. public typealias Element = SocketIOClientOption
  29. /// Index type.
  30. public typealias Index = Array<SocketIOClientOption>.Index
  31. /// Iterator type.
  32. public typealias Iterator = Array<SocketIOClientOption>.Iterator
  33. /// SubSequence type.
  34. public typealias SubSequence = Array<SocketIOClientOption>.SubSequence
  35. // MARK: Properties
  36. private var backingArray = [SocketIOClientOption]()
  37. /// The start index of this collection.
  38. public var startIndex: Index {
  39. return backingArray.startIndex
  40. }
  41. /// The end index of this collection.
  42. public var endIndex: Index {
  43. return backingArray.endIndex
  44. }
  45. /// Whether this collection is empty.
  46. public var isEmpty: Bool {
  47. return backingArray.isEmpty
  48. }
  49. /// The number of elements stored in this collection.
  50. public var count: Index.Stride {
  51. return backingArray.count
  52. }
  53. /// The first element in this collection.
  54. public var first: Element? {
  55. return backingArray.first
  56. }
  57. public subscript(position: Index) -> Element {
  58. get {
  59. return backingArray[position]
  60. }
  61. set {
  62. backingArray[position] = newValue
  63. }
  64. }
  65. public subscript(bounds: Range<Index>) -> SubSequence {
  66. get {
  67. return backingArray[bounds]
  68. }
  69. set {
  70. backingArray[bounds] = newValue
  71. }
  72. }
  73. // MARK: Initializers
  74. /// Creates a new `SocketIOClientConfiguration` from an array literal.
  75. ///
  76. /// - parameter arrayLiteral: The elements.
  77. public init(arrayLiteral elements: Element...) {
  78. backingArray = elements
  79. }
  80. // MARK: Methods
  81. /// Creates an iterator for this collection.
  82. ///
  83. /// - returns: An iterator over this collection.
  84. public func makeIterator() -> Iterator {
  85. return backingArray.makeIterator()
  86. }
  87. /// - returns: The index after index.
  88. public func index(after i: Index) -> Index {
  89. return backingArray.index(after: i)
  90. }
  91. /// Special method that inserts `element` into the collection, replacing any other instances of `element`.
  92. ///
  93. /// - parameter element: The element to insert.
  94. /// - parameter replacing: Whether to replace any occurrences of element to the new item. Default is `true`.
  95. public mutating func insert(_ element: Element, replacing replace: Bool = true) {
  96. for i in 0..<backingArray.count where backingArray[i] == element {
  97. guard replace else { return }
  98. backingArray[i] = element
  99. return
  100. }
  101. backingArray.append(element)
  102. }
  103. }
  104. /// Declares that a type can set configs from a `SocketIOClientConfiguration`.
  105. public protocol ConfigSettable {
  106. // MARK: Methods
  107. /// Called when an `ConfigSettable` should set/update its configs from a given configuration.
  108. ///
  109. /// - parameter config: The `SocketIOClientConfiguration` that should be used to set/update configs.
  110. mutating func setConfigs(_ config: SocketIOClientConfiguration)
  111. }