OSDN Git Service

JSONCommand#execute()に例外の発生可能性を示した
[kcd/KCD.git] / KCD / JSONViewCommand.swift
1 //
2 //  JSONViewCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/19.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 #if ENABLE_JSON_LOG
12
13 class JSONViewCommand: JSONCommand {
14     let jsonTree: [JSONNode]?
15     let parameterList: [Any]
16     let recieveDate: Date?
17     let command: JSONCommand
18     
19     init(apiResponse: APIResponse, command: JSONCommand) {
20         self.recieveDate = Date()
21         self.parameterList = apiResponse.argumentArray
22         self.jsonTree = JSONNode
23             .nodeWithJSON(apiResponse.json as AnyObject?)
24             .map { [$0] }
25         self.command = command
26         super.init(apiResponse: apiResponse)
27     }
28     
29     required init(apiResponse: APIResponse) {
30         fatalError("use init(apiResponse:command:)")
31     }
32     
33     override func execute() {
34         do {
35             try command.execute()
36         }
37         catch {
38             print("JSONTracker Cought Exception -> \(error)")
39         }
40         
41         guard let _ = jsonTree else { return print("jsonTree is nil.") }
42         guard let _ = recieveDate else { return print("recieveDate is nil.") }
43         
44         DispatchQueue.main.async {
45             guard let appDelegate = NSApplication.shared().delegate as? AppDelegate
46                 else { return print("Can not get AppDelegate") }
47             let commands: [String:Any] = [
48                 "api": self.api,
49                 "argument": self.parameterList,
50                 "json": self.jsonTree ?? [],
51                 "recieveDate": self.recieveDate ?? Date(),
52                 "date": Date()
53              ]
54             appDelegate.jsonViewWindowController?.setCommand(commands as NSDictionary)
55         }
56     }
57 }
58
59 #endif