forked from itsjustcon/JavaScriptCoreBrowserObjectModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimers.swift
More file actions
139 lines (113 loc) · 4.66 KB
/
Copy pathTimers.swift
File metadata and controls
139 lines (113 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Timers.swift
// JavaScriptCoreBrowserObjectModel
//
// Created by Connor Grady on 10/7/17.
// Copyright © 2017 Connor Grady. All rights reserved.
//
import Foundation
import JavaScriptCore
//@objc protocol TimerJSExport : JSExport {
// func setTimeout(_ callback: JSValue, _ ms: Double) -> String
// func clearTimeout(_ identifier: String)
// func setInterval(_ callback: JSValue, _ ms: Double) -> String
// func clearInterval(_ identifier: String)
//}
@objc open class Timers: NSObject {
//
// General
//
//public class var count: Int { return timers.count }
//public class func destroy() {
// // TODO: iterate all `timers` & invalidate each
//}
// @usage:
// let context = JSContext()!
// Timers.extend(context)
// context.evaluateScript("setTimeout(done, 5 * 1000)") // `done()` is called after 5s
open class func extend(_ jsContext: JSContext) {
jsContext.setObject(setTimeout, forKeyedSubscript: "setTimeout" as (NSCopying & NSObjectProtocol))
jsContext.setObject(clearTimeout, forKeyedSubscript: "clearTimeout" as (NSCopying & NSObjectProtocol))
jsContext.setObject(setInterval, forKeyedSubscript: "setInterval" as (NSCopying & NSObjectProtocol))
jsContext.setObject(clearInterval, forKeyedSubscript: "clearInterval" as (NSCopying & NSObjectProtocol))
}
//
// JS Methods
//
// Timeout
public static let setTimeout: @convention(block) (JSValue, Double) -> UInt = { (callback, delay) in
return createTimer(callback: callback, delay: delay, repeats: false)
}
public static let clearTimeout: @convention(block) (UInt) -> Void = { (identifier) in
invalidateTimer(identifier)
}
// Interval
public static let setInterval: @convention(block) (JSValue, Double) -> UInt = { (callback, delay) in
return createTimer(callback: callback, delay: delay, repeats: true)
}
public static let clearInterval: @convention(block) (UInt) -> Void = { (identifier) in
invalidateTimer(identifier)
}
//
// Internals
//
internal static var timers = [UInt: Timer]()
internal static var prevTimerId: UInt = 0
fileprivate struct TimerData {
var id: UInt
var callbackManagedValue: JSManagedValue
var repeats: Bool
}
internal class func createTimer(callback: JSValue, delay: Double, repeats: Bool) -> UInt {
let callbackManagedValue = JSManagedValue(value: callback)!
let timerId = prevTimerId + 1
prevTimerId = timerId
let timer = Timer.scheduledTimer(
timeInterval: delay/1000.0,
target: self,
selector: #selector(fireTimer(timer:)),
userInfo: TimerData(id: timerId, callbackManagedValue: callbackManagedValue, repeats: repeats),
repeats: repeats
)
/*
// NOTE: the following implementation requires iOS 10, so we'll switch to it eventually
let timer = Timer.scheduledTimer(withTimeInterval: delay/1000.0, repeats: repeats) { timer in
let callback = callbackManagedValue.value
guard callback?.call(withArguments: []) != nil else {
// INFO: callback no-longer exists, or is not a function
timer.invalidate()
timers.removeValue(forKey: timerId)
return
}
if !repeats {
timers.removeValue(forKey: timerId)
}
}
*/
callback.context.virtualMachine.addManagedReference(callbackManagedValue, withOwner: timer)
timers[timerId] = timer
return timerId
}
@objc internal class func fireTimer(timer: Timer) {
let userInfo = timer.userInfo as! TimerData
let callback = userInfo.callbackManagedValue.value
guard callback?.call(withArguments: []) != nil else {
// INFO: callback no-longer exists, or is not a function
timer.invalidate()
timers.removeValue(forKey: userInfo.id)
return
}
if !userInfo.repeats {
timers.removeValue(forKey: userInfo.id)
}
}
internal class func invalidateTimer(_ identifier: UInt) {
if let timer = timers.removeValue(forKey: identifier) {
let userInfo = timer.userInfo as! TimerData
let callbackManagedValue = userInfo.callbackManagedValue
let callback = callbackManagedValue.value
callback?.context.virtualMachine.removeManagedReference(callbackManagedValue, withOwner: timer)
timer.invalidate()
}
}
}