Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer added #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Example/Nifty/MyCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ Renderer.render = function() {
textNode.maxSize = {width: ScreenBounds.width/3, height: 100}
textNode.preferredSize = {width: ScreenBounds.width*0.3, height: 80}
textNode.tag = 1234

var count = 0
let dispatcher = {}
dispatcher.timerHandler = function timerHandler() {
debugger
count = count + 1
textNode.text = count
}.bind(textNode, count)

let timer = Timer.create()
timer.nt_setDispatcher(dispatcher)
timer.setupTimerWithInterval(1.0, true)

return textNode
}

Expand Down
4 changes: 4 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Nifty/Classes/Core/NTModuleRegistery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ internal class NTModuleRegistery {
NTButtonNode.self,

UIColor.self,
UIImage.self]
UIImage.self,
NTTimer.self]

//NTLOOK: Figure out all the default environtment variables related to mobie that should be exported here
//Bundle and document/library directory read write, filemanager etc
Expand Down
98 changes: 98 additions & 0 deletions Nifty/Classes/NTDisplay/Utilities/NTTimer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// NTTimer.swift
// CwlUtils
//
// Created by Naveen Chaudhary on 04/12/17.
//

import JavaScriptCore

@objc public protocol NTTimerNodeExports: JSExport {
static func create() -> NTTimer
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have 2 apis. 1 for creating timer with an interval and other for creating and scheduling with an interval. Kind of like what NSTimer does.
Lets discuss this offline.

func setupTimerWithInterval(_ interval: TimeInterval, _ repeats: Bool)
func invalidateTimer()
}

@objc public class NTTimer: NSObject, NTTimerNodeExports, NTDispatcherModuleExports {

required public override init() {
super.init()
}

public static func create() -> NTTimer {
let timer = self.init()
return timer
}


internal var timer: Timer?

public func setupTimerWithInterval(_ timeInterval: TimeInterval, _ repeats: Bool) {
invalidateTimer()

self.timer = Timer(timeInterval: timeInterval, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: repeats)
if let timer = self.timer {
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
}
}

public func invalidateTimer() {
self.timer?.invalidate()
}

@objc internal func handleTimer() -> Void {
if let dispatcher = self.ntDispatcher?.value {
print(dispatcher)

let _ = dispatcher.objectForKeyedSubscript("timerHandler").call(withArguments: nil)
}
}

// Must be set nil in deinit in order to remove managed reference for dispatcher
public var ntDispatcher: JSManagedValue? {

willSet {
if newValue == nil {
if let context = JSContext.current() {
let delegate = UIApplication.shared.delegate
context.setObject(delegate, forKeyedSubscript: "SharedOwner" as NSString)
context.virtualMachine.removeManagedReference(ntDispatcher, withOwner: delegate)
} else {
print("Context not found!")
}
}
}

didSet {
if ntDispatcher != nil {
if let context = JSContext.current() {
let delegate = UIApplication.shared.delegate
context.setObject(delegate, forKeyedSubscript: "SharedOwner" as NSString)
context.virtualMachine.addManagedReference(ntDispatcher, withOwner: delegate)
} else {
print("Context not found!")
}
}
}
}

open func nt_setDispatcher(_ jsObject: JSValue) {
self.ntDispatcher = JSManagedValue(value: jsObject, andOwner: self)
}

deinit {
ntDispatcher = nil
}
}

extension NTTimer: NTModule {
//MARK:-
//MARK:NTModule
public static func moduleName() -> String {
return "Timer"
}

public static func constantsToExport() -> [String : Any]? {
return nil
}
}