Skip to content

Commit

Permalink
Renamed MVar to Atomic.
Browse files Browse the repository at this point in the history
  • Loading branch information
buh committed Sep 13, 2019
1 parent bf2b192 commit a3b14c9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MVar.swift
// Atomic.swift
// StreamChatCore
//
// Created by Alexey Bukhtin on 25/06/2019.
Expand All @@ -9,15 +9,15 @@
import Foundation

/// A mutable thread safe variable.
public final class MVar<T> {
public final class Atomic<T> {
/// A didSet callback type.
public typealias DidSetCallback = (T?) -> Void

private let queue = DispatchQueue(label: "io.getstream.Chat.MVar", qos: .utility, attributes: .concurrent)
private let queue = DispatchQueue(label: "io.getstream.Chat.Atomic", qos: .utility, attributes: .concurrent)
private var value: T?
private var didSet: DidSetCallback?

/// Init a MVar.
/// Init a Atomic.
///
/// - Parameters:
/// - value: an initial value.
Expand Down Expand Up @@ -53,15 +53,15 @@ public final class MVar<T> {

// MARK: - Helper Operator

public extension MVar where T == Int {
public extension Atomic where T == Int {

static func += (lhs: MVar<T>, rhs: T) {
static func += (lhs: Atomic<T>, rhs: T) {
if let currentValue = lhs.get() {
lhs.set(currentValue + rhs)
}
}

static func -= (lhs: MVar<T>, rhs: T) {
static func -= (lhs: Atomic<T>, rhs: T) {
if let currentValue = lhs.get() {
lhs.set(currentValue - rhs)
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/Core/Model/Channel+Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ extension Channel {
.flatMapLatest { [weak self] _ in
Client.shared.webSocket.response
.filter { self?.updateUnreadCount($0) ?? false }
.map { _ in self?.unreadCountMVar.get() }
.startWith(self?.unreadCountMVar.get())
.map { _ in self?.unreadCountAtomic.get() }
.startWith(self?.unreadCountAtomic.get())
.unwrap()
}
.startWith(0)
Expand All @@ -87,7 +87,7 @@ extension Channel {

func setupUnreadCount(_ channelResponse: ChannelResponse) {
guard let unreadMessageRead = channelResponse.unreadMessageRead else {
unreadCountMVar.set(0)
unreadCountAtomic.set(0)
return
}

Expand All @@ -101,7 +101,7 @@ extension Channel {
}
}

unreadCountMVar.set(count)
unreadCountAtomic.set(count)
}

func updateUnreadCount(_ response: WebSocket.Response) -> Bool {
Expand All @@ -110,12 +110,12 @@ extension Channel {
}

if case .messageNew = response.event {
unreadCountMVar += 1
unreadCountAtomic += 1
return true
}

if case .messageRead(let messageRead, _) = response.event, messageRead.user.isCurrent {
unreadCountMVar.set(0)
unreadCountAtomic.set(0)
return true
}

Expand Down Expand Up @@ -146,7 +146,7 @@ extension Channel {
return .empty()
}

self.onlineUsersMVar.set(onlineUsers)
self.onlineUsersAtomic.set(onlineUsers)

// Subscribe for user presence changes.
return Client.shared.onEvent(.userPresenceChanged)
Expand All @@ -155,7 +155,7 @@ extension Channel {
return []
}

var onlineUsers = self.onlineUsersMVar.get(defaultValue: [])
var onlineUsers = self.onlineUsersAtomic.get(defaultValue: [])

if user.online {
if !onlineUsers.contains(user) {
Expand All @@ -167,7 +167,7 @@ extension Channel {
}
}

self.onlineUsersMVar.set(onlineUsers)
self.onlineUsersAtomic.set(onlineUsers)

return onlineUsers
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Model/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public final class Channel: Codable {
return Channel.activeChannelIds.contains(cid)
}

var unreadCountMVar = MVar(0)
var onlineUsersMVar = MVar<[User]>([])
var unreadCountAtomic = Atomic(0)
var onlineUsersAtomic = Atomic<[User]>([])

/// Init a channel.
///
Expand Down
40 changes: 20 additions & 20 deletions Sources/Core/Presenter/ChannelPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public final class ChannelPresenter: Presenter<ChatItem> {

private let channelType: ChannelType
private let channelId: String
private let channelMVar = MVar<Channel>()
private let channelAtomic = Atomic<Channel>()

/// A channel (see `Channel`).
public var channel: Channel {
return channelMVar.get(defaultValue: Channel(type: channelType, id: channelId))
return channelAtomic.get(defaultValue: Channel(type: channelType, id: channelId))
}

/// A parent message for replies.
Expand All @@ -40,18 +40,18 @@ public final class ChannelPresenter: Presenter<ChatItem> {
public private(set) var showStatuses = true

private var startedTyping = false
private let lastMessageMVar = MVar<Message>()
private let lastMessageAtomic = Atomic<Message>()

/// The last parsed message from WebSocket events.
public var lastMessage: Message? {
return lastMessageMVar.get()
return lastMessageAtomic.get()
}

private var lastAddedOwnMessage: Message?
private var lastParsedEvent: Event?
private var lastWebSocketEventViewChanges: ViewChanges?

private lazy var unreadMessageReadMVar = MVar<MessageRead>()
private lazy var unreadMessageReadAtomic = Atomic<MessageRead>()

/// A list of typing users (see `TypingUser`).
public private(set) var typingUsers: [TypingUser] = []
Expand All @@ -61,7 +61,7 @@ public final class ChannelPresenter: Presenter<ChatItem> {

/// Check if the channel has unread messages.
public var isUnread: Bool {
return channel.config.readEventsEnabled && unreadMessageReadMVar.get() != nil
return channel.config.readEventsEnabled && unreadMessageReadAtomic.get() != nil
}

/// Check if the channel has ephemeral message, e.g. Giphy preview.
Expand Down Expand Up @@ -128,7 +128,7 @@ public final class ChannelPresenter: Presenter<ChatItem> {
public init(channel: Channel, parentMessage: Message? = nil, queryOptions: QueryOptions = .all, showStatuses: Bool = true) {
channelType = channel.type
channelId = channel.id
channelMVar.set(channel)
channelAtomic.set(channel)
self.parentMessage = parentMessage
self.queryOptions = queryOptions
self.showStatuses = showStatuses
Expand All @@ -143,7 +143,7 @@ public final class ChannelPresenter: Presenter<ChatItem> {
public init(response: ChannelResponse, queryOptions: QueryOptions, showStatuses: Bool = true) {
channelType = response.channel.type
channelId = response.channel.id
channelMVar.set(response.channel)
channelAtomic.set(response.channel)
parentMessage = nil
self.queryOptions = queryOptions
self.showStatuses = showStatuses
Expand Down Expand Up @@ -208,14 +208,14 @@ extension ChannelPresenter {
}

if let messageNewChannel = messageNewChannel {
channelMVar.set(messageNewChannel)
channelAtomic.set(messageNewChannel)
}

if channel.config.readEventsEnabled {
if let lastMessage = lastMessageMVar.get() {
unreadMessageReadMVar.set(MessageRead(user: lastMessage.user, lastReadDate: lastMessage.updated))
if let lastMessage = lastMessageAtomic.get() {
unreadMessageReadAtomic.set(MessageRead(user: lastMessage.user, lastReadDate: lastMessage.updated))
} else {
unreadMessageReadMVar.set(MessageRead(user: message.user, lastReadDate: message.updated))
unreadMessageReadAtomic.set(MessageRead(user: message.user, lastReadDate: message.updated))
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ extension ChannelPresenter {
}

private func appendOrUpdateMessageItem(_ message: Message, at index: Int = -1) {
lastMessageMVar.set(message)
lastMessageAtomic.set(message)

if index == -1 {
if message.isOwn {
Expand Down Expand Up @@ -391,7 +391,7 @@ extension ChannelPresenter {

@discardableResult
private func parseResponse(_ query: ChannelResponse) -> ViewChanges {
channelMVar.set(query.channel)
channelAtomic.set(query.channel)
let isNextPage = next != pageSize
var items = isNextPage ? self.items : []

Expand All @@ -400,7 +400,7 @@ extension ChannelPresenter {
}

if channel.config.readEventsEnabled {
unreadMessageReadMVar.set(query.unreadMessageRead)
unreadMessageReadAtomic.set(query.unreadMessageRead)

if !isNextPage {
messageReadsToMessageId = [:]
Expand Down Expand Up @@ -484,7 +484,7 @@ extension ChannelPresenter {
ownMessagesIndexes.append(index)
}

lastMessageMVar.set(message)
lastMessageAtomic.set(message)
items.insert(.message(message, []), at: index)
index += 1
}
Expand Down Expand Up @@ -657,24 +657,24 @@ extension ChannelPresenter {
///
/// - Returns: an observable completion.
public func markReadIfPossible() -> Observable<Void> {
guard let oldUnreadMessageRead = unreadMessageReadMVar.get() else {
guard let oldUnreadMessageRead = unreadMessageReadAtomic.get() else {
Client.shared.logger?.log("🎫", "Skip read.")
return .empty()
}

unreadMessageReadMVar.set(nil)
unreadMessageReadAtomic.set(nil)

return Observable.just(())
.subscribeOn(MainScheduler.instance)
.filter { UIApplication.shared.appState == .active }
.do(onNext: { Client.shared.logger?.log("🎫", "Send Message Read. Unread from \(oldUnreadMessageRead.lastReadDate)") })
.flatMap { [weak self] in self?.channel.markRead() ?? .empty() }
.do(onNext: { [weak self] _ in
self?.unreadMessageReadMVar.set(nil)
self?.unreadMessageReadAtomic.set(nil)
self?.isReadSubject.onNext(())
Client.shared.logger?.log("🎫", "Message Read done.")
}, onError: { [weak self] error in
self?.unreadMessageReadMVar.set(oldUnreadMessageRead)
self?.unreadMessageReadAtomic.set(oldUnreadMessageRead)
self?.isReadSubject.onError(error)
ClientLogger.log("🎫", error, message: "Send Message Read error.")
})
Expand Down
8 changes: 4 additions & 4 deletions StreamChat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
8AD5ED4422E9BA5A005CFAC9 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED0E22E9BA39005CFAC9 /* String+Extensions.swift */; };
8AD5ED4522E9BA5A005CFAC9 /* Result+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED0F22E9BA39005CFAC9 /* Result+Extensions.swift */; };
8AD5ED4622E9BA5A005CFAC9 /* URL+Media.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1022E9BA39005CFAC9 /* URL+Media.swift */; };
8AD5ED4722E9BA5A005CFAC9 /* MVar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1222E9BA39005CFAC9 /* MVar.swift */; };
8AD5ED4722E9BA5A005CFAC9 /* Atomic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1222E9BA39005CFAC9 /* Atomic.swift */; };
8AD5ED4822E9BA5A005CFAC9 /* RepeatingTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1322E9BA39005CFAC9 /* RepeatingTimer.swift */; };
8AD5ED4922E9BA5A005CFAC9 /* UIApplication+AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1422E9BA39005CFAC9 /* UIApplication+AppState.swift */; };
8AD5ED4A22E9BA5A005CFAC9 /* Codable+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ED1522E9BA39005CFAC9 /* Codable+Extensions.swift */; };
Expand Down Expand Up @@ -289,7 +289,7 @@
8AD5ED0E22E9BA39005CFAC9 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
8AD5ED0F22E9BA39005CFAC9 /* Result+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Result+Extensions.swift"; sourceTree = "<group>"; };
8AD5ED1022E9BA39005CFAC9 /* URL+Media.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Media.swift"; sourceTree = "<group>"; };
8AD5ED1222E9BA39005CFAC9 /* MVar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MVar.swift; sourceTree = "<group>"; };
8AD5ED1222E9BA39005CFAC9 /* Atomic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Atomic.swift; sourceTree = "<group>"; };
8AD5ED1322E9BA39005CFAC9 /* RepeatingTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepeatingTimer.swift; sourceTree = "<group>"; };
8AD5ED1422E9BA39005CFAC9 /* UIApplication+AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+AppState.swift"; sourceTree = "<group>"; };
8AD5ED1522E9BA39005CFAC9 /* Codable+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Codable+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -779,7 +779,7 @@
8AD5ED1122E9BA39005CFAC9 /* Utils */ = {
isa = PBXGroup;
children = (
8AD5ED1222E9BA39005CFAC9 /* MVar.swift */,
8AD5ED1222E9BA39005CFAC9 /* Atomic.swift */,
8AD5ED1322E9BA39005CFAC9 /* RepeatingTimer.swift */,
);
path = Utils;
Expand Down Expand Up @@ -1215,7 +1215,7 @@
8AD5ED5522E9BA5A005CFAC9 /* Message.swift in Sources */,
8AD5ED6422E9BA5A005CFAC9 /* BaseURL.swift in Sources */,
8AD5ED4B22E9BA5A005CFAC9 /* Dictionary+Extensions.swift in Sources */,
8AD5ED4722E9BA5A005CFAC9 /* MVar.swift in Sources */,
8AD5ED4722E9BA5A005CFAC9 /* Atomic.swift in Sources */,
8A04964922E9F59A00E94795 /* Date+Extensions.swift in Sources */,
8A04964522E9F53200E94795 /* ChannelsPresenter.swift in Sources */,
8A04964622E9F53900E94795 /* ChannelPresenter.swift in Sources */,
Expand Down

0 comments on commit a3b14c9

Please sign in to comment.