Skip to content

Commit

Permalink
Notify every device only once #23
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed May 14, 2020
1 parent 247f2a5 commit 16ebcaa
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion PufferyServer/Sources/App/Services/PushService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct PushService {
func notifyDevices(message: Message) -> EventLoopFuture<Void> {
notifiableDeviceTokens(message: message)
.flatMap { devices in
let apnsSends = devices.filter { !$0.token.isEmpty }.map { device in
let apnsSends = devices.unique(\.token).filter { !$0.token.isEmpty }.map { device in
self.send(message: message, to: device)
}
return self.req.eventLoop.flatten(apnsSends)
Expand Down Expand Up @@ -71,3 +71,10 @@ struct PushService {
.all()
}
}

private extension Sequence {
func unique<V: Hashable>(_ prop: (Iterator.Element) -> V) -> [Iterator.Element] {
var seen: Set<V> = []
return filter { seen.insert(prop($0)).inserted }
}
}

0 comments on commit 16ebcaa

Please sign in to comment.