<commit_before>//
//  Presence.swift
//  Pods
//
//  Created by <NAME> on 6/07/2016.
//
//

import Foundation

public final class Presence {
    // MARK: - Convenience typealiases

    public typealias PresenceState = [String: [Meta]]
    public typealias Diff = [String: [String: Any]]
    public typealias Meta = [String: AnyObject]

    // MARK: - Properties

    fileprivate(set) public var state: PresenceState

    // MARK: - Callbacks

    public var onJoin: ((_ id: String, _ meta: Meta) -> ())?
    public var onLeave: ((_ id: String, _ meta: Meta) -> ())?
    public var onStateChange: ((_ state: PresenceState) -> ())?

    // MARK: - Initialisation

    init(state: PresenceState) {
        self.state = state
    }

    // MARK: - Syncing

    func sync(_ diff: Response) {
        // Initial state event
        if diff.event == "presence_state" {
            diff.payload.forEach{ id, entry in
                if let entry = entry as? [String: Any] {
                    if let metas = entry["metas"] as? [Meta] {
                        state[id] = metas
                    }
                }
            }
        }
        else if diff.event == "presence_diff" {
            if let leaves = diff.payload["leaves"] as? Diff {
                syncLeaves(leaves)
            }
            if let joins = diff.payload["joins"] as? Diff {
                syncJoins(joins)
            }
        }

        onStateChange?(state)
    }

    func syncLeaves(_ diff: Diff) {
        defer {
            diff.forEach { id, entry in
                if let metas = entry["metas"] as? [Meta] {
                    metas.forEach { onLeave?(id, $0) }
                }
            }
        }

        for (id, entry) in diff where state[id] != nil {
            guard var existing = state[id] else {
                continue
            }

            // If there's only one entry for the id, just remove it.
            if existing.count == 1 {
                state.removeValue(forKey: id)
                continue
            }

            // Otherwise, we need to find the phx_ref keys to delete.
            let metas = entry["metas"] as? [Meta]
            if let refsToDelete = metas?.flatMap({ $0["phx_ref"] as? String }) {
                existing = existing.filter {
                    if let phxRef = $0["pxh_ref"] as? String {
                        return !refsToDelete.contains(phxRef)
                    }
                    
                    return false
                }
                state[id] = existing
            }
        }
    }

    func syncJoins(_ diff: Diff) {<commit_msg>Filter ref if it cannot be typecasted to String<commit_after>//
//  Presence.swift
//  Pods
//
//  Created by <NAME> on 6/07/2016.
//
//

import Foundation

public final class Presence {
    // MARK: - Convenience typealiases

    public typealias PresenceState = [String: [Meta]]
    public typealias Diff = [String: [String: Any]]
    public typealias Meta = [String: AnyObject]

    // MARK: - Properties

    fileprivate(set) public var state: PresenceState

    // MARK: - Callbacks

    public var onJoin: ((_ id: String, _ meta: Meta) -> ())?
    public var onLeave: ((_ id: String, _ meta: Meta) -> ())?
    public var onStateChange: ((_ state: PresenceState) -> ())?

    // MARK: - Initialisation

    init(state: PresenceState) {
        self.state = state
    }

    // MARK: - Syncing

    func sync(_ diff: Response) {
        // Initial state event
        if diff.event == "presence_state" {
            diff.payload.forEach{ id, entry in
                if let entry = entry as? [String: Any] {
                    if let metas = entry["metas"] as? [Meta] {
                        state[id] = metas
                    }
                }
            }
        }
        else if diff.event == "presence_diff" {
            if let leaves = diff.payload["leaves"] as? Diff {
                syncLeaves(leaves)
            }
            if let joins = diff.payload["joins"] as? Diff {
                syncJoins(joins)
            }
        }

        onStateChange?(state)
    }

    func syncLeaves(_ diff: Diff) {
        defer {
            diff.forEach { id, entry in
                if let metas = entry["metas"] as? [Meta] {
                    metas.forEach { onLeave?(id, $0) }
                }
            }
        }

        for (id, entry) in diff where state[id] != nil {
            guard var existing = state[id] else {
                continue
            }

            // If there's only one entry for the id, just remove it.
            if existing.count == 1 {
                state.removeValue(forKey: id)
                continue
            }

            // Otherwise, we need to find the phx_ref keys to delete.
            let metas = entry["metas"] as? [Meta]
            if let refsToDelete = metas?.flatMap({ $0["phx_ref"] as? String }) {
                existing = existing.filter {
                    if let phxRef = $0["phx_ref"] as? String {
                        return !refsToDelete.contains(phxRef)
                    }
                    
                    return true
                }
                state[id] = existing
            }
        }
    }

    func syncJoins(_ diff: Diff) {
{'fraction_non_alphanumeric': 0.10902184235517569, 'fraction_numerical': 0.004178537511870846, 'mean_word_length': 1.3331856446610546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2718379', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1440, 'n_words': 421}