1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00

Refactor slightly

This commit is contained in:
Tae Won Ha 2018-05-21 21:30:19 +02:00
parent 8b103bbd0a
commit a3d9bfe34e
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 63 additions and 54 deletions

View File

@ -10,6 +10,7 @@
1929B30D6C4175835D1F5B21 /* MessagePackCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B44323D6611E2927EC3B /* MessagePackCommons.swift */; }; 1929B30D6C4175835D1F5B21 /* MessagePackCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B44323D6611E2927EC3B /* MessagePackCommons.swift */; };
1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */; }; 1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */; };
1929B86897DAEFDBABAB1C14 /* NvimApiExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */; }; 1929B86897DAEFDBABAB1C14 /* NvimApiExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */; };
1929B8CA73D3702364903BB7 /* SimpleCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BD86668017F804408E3A /* SimpleCache.swift */; };
1929BA70C221E3C199833B8C /* UiBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B52174EC68D2974B5BAE /* UiBridge.swift */; }; 1929BA70C221E3C199833B8C /* UiBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B52174EC68D2974B5BAE /* UiBridge.swift */; };
1929BA93BDEA029011F034FF /* RxSwiftCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */; }; 1929BA93BDEA029011F034FF /* RxSwiftCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */; };
4B177886201220F300E32FF0 /* SharedTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 1929B4F32708E99C40A57020 /* SharedTypes.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B177886201220F300E32FF0 /* SharedTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 1929B4F32708E99C40A57020 /* SharedTypes.h */; settings = {ATTRIBUTES = (Public, ); }; };
@ -99,6 +100,7 @@
1929B52174EC68D2974B5BAE /* UiBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiBridge.swift; sourceTree = "<group>"; }; 1929B52174EC68D2974B5BAE /* UiBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiBridge.swift; sourceTree = "<group>"; };
1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxSwiftCommons.swift; sourceTree = "<group>"; }; 1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxSwiftCommons.swift; sourceTree = "<group>"; };
1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimApiExtension.swift; sourceTree = "<group>"; }; 1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimApiExtension.swift; sourceTree = "<group>"; };
1929BD86668017F804408E3A /* SimpleCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleCache.swift; sourceTree = "<group>"; };
4B17E548209E3E4100265C1D /* RxNeovimApi.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxNeovimApi.framework; path = ../Carthage/Build/Mac/RxNeovimApi.framework; sourceTree = "<group>"; }; 4B17E548209E3E4100265C1D /* RxNeovimApi.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxNeovimApi.framework; path = ../Carthage/Build/Mac/RxNeovimApi.framework; sourceTree = "<group>"; };
4B8662E41FDC3D4F007F490D /* vimr.vim */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = vimr.vim; sourceTree = "<group>"; }; 4B8662E41FDC3D4F007F490D /* vimr.vim */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = vimr.vim; sourceTree = "<group>"; };
4B90F0041FD2AF59008A39E0 /* NvimView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NvimView.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 4B90F0041FD2AF59008A39E0 /* NvimView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NvimView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@ -225,6 +227,7 @@
1929B52174EC68D2974B5BAE /* UiBridge.swift */, 1929B52174EC68D2974B5BAE /* UiBridge.swift */,
1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */, 1929B6F4B70B90F7CFB7B523 /* RxSwiftCommons.swift */,
1929B44323D6611E2927EC3B /* MessagePackCommons.swift */, 1929B44323D6611E2927EC3B /* MessagePackCommons.swift */,
1929BD86668017F804408E3A /* SimpleCache.swift */,
); );
path = NvimView; path = NvimView;
sourceTree = "<group>"; sourceTree = "<group>";
@ -405,6 +408,7 @@
1929BA70C221E3C199833B8C /* UiBridge.swift in Sources */, 1929BA70C221E3C199833B8C /* UiBridge.swift in Sources */,
1929BA93BDEA029011F034FF /* RxSwiftCommons.swift in Sources */, 1929BA93BDEA029011F034FF /* RxSwiftCommons.swift in Sources */,
1929B30D6C4175835D1F5B21 /* MessagePackCommons.swift in Sources */, 1929B30D6C4175835D1F5B21 /* MessagePackCommons.swift in Sources */,
1929B8CA73D3702364903BB7 /* SimpleCache.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -38,57 +38,3 @@ class ColorUtils {
return color return color
} }
} }
class SimpleCache<K: Hashable, V> {
private class ValueBox<T> {
fileprivate let value: T
fileprivate init(_ value: T) {
self.value = value
}
}
private class KeyBox<K: Hashable>: NSObject {
fileprivate let key: K
fileprivate init(_ key: K) {
self.key = key
}
override var hash: Int {
return key.hashValue
}
override func isEqual(_ object: Any?) -> Bool {
return self.key == (object as? KeyBox<K>)?.key
}
}
private let cache = NSCache<KeyBox<K>, ValueBox<V>>()
init(countLimit: Int? = nil) {
if let limit = countLimit {
self.cache.countLimit = limit
}
}
func object(forKey key: K) -> V? {
return self.cache.object(forKey: KeyBox(key))?.value
}
func set(object: V, forKey key: K) {
self.cache.setObject(ValueBox(object), forKey: KeyBox(key))
}
func removeObject(forKey key: K) {
cache.removeObject(forKey: KeyBox(key))
}
func removeAllObjects() {
cache.removeAllObjects()
}
}

View File

@ -0,0 +1,59 @@
//
// Created by Tae Won Ha on 21.05.18.
// Copyright (c) 2018 Tae Won Ha. All rights reserved.
//
import Foundation
class SimpleCache<K: Hashable, V> {
private class ValueBox<T> {
fileprivate let value: T
fileprivate init(_ value: T) {
self.value = value
}
}
private class KeyBox<K: Hashable>: NSObject {
fileprivate let key: K
fileprivate init(_ key: K) {
self.key = key
}
override var hash: Int {
return key.hashValue
}
override func isEqual(_ object: Any?) -> Bool {
return self.key == (object as? KeyBox<K>)?.key
}
}
private let cache = NSCache<KeyBox<K>, ValueBox<V>>()
init(countLimit: Int? = nil) {
if let limit = countLimit {
self.cache.countLimit = limit
}
}
func object(forKey key: K) -> V? {
return self.cache.object(forKey: KeyBox(key))?.value
}
func set(object: V, forKey key: K) {
self.cache.setObject(ValueBox(object), forKey: KeyBox(key))
}
func removeObject(forKey key: K) {
cache.removeObject(forKey: KeyBox(key))
}
func removeAllObjects() {
cache.removeAllObjects()
}
}