Skip to content

Commit

Permalink
Getting rid of Predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Feb 18, 2024
1 parent f6298bf commit bdbcaf5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
30 changes: 13 additions & 17 deletions Sources/PubNub/Events/New/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,31 @@ extension Subscription: SubscribeMessagesReceiver {

func event(from payload: SubscribeMessagePayload) -> PubNubEvent? {
let isNewerOrEqualToTimetoken = payload.publishTimetoken.timetoken >= timetoken ?? 0
let receivedFromCurrentEntity: Bool
let isMatchingEntity: Bool

if subscriptionType == .channel {
receivedFromCurrentEntity = entity.name.matches(string: payload.channel)
isMatchingEntity = isMatchingEntityName(entity.name, string: payload.channel)
} else if subscriptionType == .channelGroup {
receivedFromCurrentEntity = entity.name.matches(string: payload.subscription ?? payload.channel)
isMatchingEntity = isMatchingEntityName(entity.name, string: payload.subscription ?? payload.channel)
} else {
receivedFromCurrentEntity = true
isMatchingEntity = true
}

if receivedFromCurrentEntity && isNewerOrEqualToTimetoken {
if isMatchingEntity && isNewerOrEqualToTimetoken {
let event = payload.asPubNubEvent()
return options.filterCriteriaSatisfied(event: event) ? event : nil
} else {
return nil
}
}
}

// MARK: - Helper String extension

fileprivate extension String {
func matches(string: String) -> Bool {
guard hasSuffix(".*") else {
return self == string

fileprivate func isMatchingEntityName(_ entityName: String, string: String) -> Bool {
guard entityName.hasSuffix(".*") else {
return entityName == string
}
let pattern = "^" + self + "$"
let predicate = NSPredicate(format: "SELF MATCHES %@", pattern)

return predicate.evaluate(with: string)
if let firstIndex = entityName.lastIndex(of: "."), let secondIndex = string.lastIndex(of: ".") {
return entityName.prefix(upTo: firstIndex) == string.prefix(upTo: secondIndex)
}
return false
}
}
4 changes: 2 additions & 2 deletions Sources/PubNub/Subscription/SubscriptionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ extension SubscriptionSession: EntityCreator {
}

public func userMetadata(_ name: String) -> UserMetadataRepresentation {
UserMetadataRepresentation(name: name, receiver: self)
UserMetadataRepresentation(id: name, receiver: self)
}

public func channelMetadata(_ name: String) -> ChannelMetadataRepresentation {
ChannelMetadataRepresentation(name: name, receiver: self)
ChannelMetadataRepresentation(id: name, receiver: self)
}
}

Expand Down

0 comments on commit bdbcaf5

Please sign in to comment.