Created
November 4, 2019 12:01
-
-
Save chanonly123/0565da4438eede9376fa34b94ef69669 to your computer and use it in GitHub Desktop.
Simple and lite Observable protocol, with weak storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SimpleObserver.swift | |
// | |
// Created by Chandan Karmakar on 15/10/19. | |
// Copyright © 2019 Chandan Karmakar. All rights reserved. | |
// | |
import Foundation | |
fileprivate let rTargets = NSMapTable<AnyObject, AnyObject>(keyOptions: .weakMemory, | |
valueOptions: .strongMemory) | |
protocol Observable: class {} | |
extension Observable { | |
func registerChange(_ target: AnyObject, handler: @escaping ((Self)->Void)) { | |
var objectsMap: NSMapTable<AnyObject, AnyObject>! | |
if let objects = rTargets.object(forKey: target) as? NSMapTable<AnyObject, AnyObject> { | |
objectsMap = objects | |
} else { | |
objectsMap = NSMapTable<AnyObject, AnyObject>(keyOptions: .weakMemory, valueOptions: .strongMemory) | |
rTargets.setObject(objectsMap, forKey: target) | |
} | |
objectsMap.setObject(handler as AnyObject, forKey: self) | |
} | |
func removeChange(_ target: AnyObject) { | |
rTargets.removeObject(forKey: target) | |
} | |
func notify() { | |
let targets = rTargets.keyEnumerator().allObjects | |
targets.forEach { | |
if let objectsMap = rTargets.object(forKey: $0 as AnyObject) as? NSMapTable<AnyObject, AnyObject> { | |
if let handler = objectsMap.object(forKey: self as AnyObject) as? ((Self)->Void) { | |
handler(self) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment