Skip to content

Commit

Permalink
add NSTimerExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Maloney committed Nov 18, 2015
1 parent ffe9793 commit d800ccb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CleanroomBridging.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
3B00E5681BA3782D0011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E55D1BA3782D0011486D /* CleanroomBridging.framework */; };
3B00E5861BA379830011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E57C1BA379830011486D /* CleanroomBridging.framework */; };
3B00E5A41BA37A420011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E59A1BA37A420011486D /* CleanroomBridging.framework */; };
3B26C2441BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; };
3B26C2451BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; };
3B26C2461BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; };
3B26C2471BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; };
3BFFEB7C1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; };
3BFFEB7D1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; };
3BFFEB7E1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -75,6 +79,7 @@
3B00E5851BA379830011486D /* CleanroomBridgingUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CleanroomBridgingUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3B00E59A1BA37A420011486D /* CleanroomBridging.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CleanroomBridging.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3B00E5A31BA37A420011486D /* CleanroomBridgingUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CleanroomBridgingUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimerExtension.swift; sourceTree = "<group>"; };
3B38DBD01BA76428000986DF /* Debug-iOS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-iOS.xcconfig"; sourceTree = "<group>"; };
3B38DBD11BA76428000986DF /* Debug-OSX.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-OSX.xcconfig"; sourceTree = "<group>"; };
3B38DBD21BA76428000986DF /* Debug-tvOS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-tvOS.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -231,6 +236,7 @@
3BFFEB781BAA140A0042FFF5 /* Exception.h */,
3BFFEB791BAA140A0042FFF5 /* Exception.m */,
3BFFEB9D1BAA156B0042FFF5 /* NotificationWatcher.swift */,
3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */,
3BFFEB7B1BAA140A0042FFF5 /* TargetAction.swift */,
);
path = Code;
Expand Down Expand Up @@ -527,6 +533,7 @@
3BFFEB801BAA140A0042FFF5 /* AssociatedObject.m in Sources */,
3BFFEB9E1BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */,
3BFFEB901BAA140A0042FFF5 /* TargetAction.swift in Sources */,
3B26C2441BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */,
3BFFEB881BAA140A0042FFF5 /* Exception.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -547,6 +554,7 @@
3BFFEB811BAA140A0042FFF5 /* AssociatedObject.m in Sources */,
3BFFEB9F1BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */,
3BFFEB911BAA140A0042FFF5 /* TargetAction.swift in Sources */,
3B26C2451BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */,
3BFFEB891BAA140A0042FFF5 /* Exception.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -567,6 +575,7 @@
3BFFEB821BAA140A0042FFF5 /* AssociatedObject.m in Sources */,
3BFFEBA01BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */,
3BFFEB921BAA140A0042FFF5 /* TargetAction.swift in Sources */,
3B26C2461BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */,
3BFFEB8A1BAA140A0042FFF5 /* Exception.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -587,6 +596,7 @@
3BFFEB831BAA140A0042FFF5 /* AssociatedObject.m in Sources */,
3BFFEBA11BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */,
3BFFEB931BAA140A0042FFF5 /* TargetAction.swift in Sources */,
3B26C2471BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */,
3BFFEB8B1BAA140A0042FFF5 /* Exception.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
66 changes: 66 additions & 0 deletions Code/NSTimerExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// NSTimerExtension.swift
// Cleanroom Project
//
// Created by Evan Maloney on 10/1/15.
// Copyright © 2015 Gilt Groupe. All rights reserved.
//

import Foundation

extension NSTimer
{
public typealias TimerFiredCallback = (NSTimer) -> Void

public class func scheduledTimerWithTimeInterval(interval: NSTimeInterval, userInfo: AnyObject? = nil, repeats: Bool = false, callback: TimerFiredCallback)
-> NSTimer
{
let ta = TargetAction() { (obj: AnyObject?) in
callback(obj as! NSTimer)
}

let timer = NSTimer(timeInterval: interval, target: ta, selector: ta.action, userInfo: userInfo, repeats: repeats)

NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

return timer
}

public class func scheduledTimerWithFireDate(fireDate: NSDate, callback: TimerFiredCallback)
-> NSTimer
{
let timer = NSTimer(fireDate: fireDate, callback: callback)

NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

return timer
}

public class func scheduledTimerWithFireDate(fireDate: NSDate, repeatInterval: NSTimeInterval, callback: TimerFiredCallback)
-> NSTimer
{
let timer = NSTimer(fireDate: fireDate, repeatInterval: repeatInterval, callback: callback)

NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

return timer
}

public convenience init(fireDate: NSDate, callback: TimerFiredCallback)
{
let ta = TargetAction() { (obj: AnyObject?) in
callback(obj as! NSTimer)
}

self.init(fireDate: fireDate, interval: 0.0, target: ta, selector: ta.action, userInfo: nil, repeats: false)
}

public convenience init(fireDate: NSDate, repeatInterval: NSTimeInterval, callback: TimerFiredCallback)
{
let ta = TargetAction() { (obj: AnyObject?) in
callback(obj as! NSTimer)
}

self.init(fireDate: fireDate, interval: repeatInterval, target: ta, selector: ta.action, userInfo: nil, repeats: true)
}
}
2 changes: 1 addition & 1 deletion Tests/ExceptionTrapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Cleanroom
//
// Created by Evan Maloney on 1/2/15.
// Copyright (c) 2015 Gilt Groupe. All rights reserved.
// Copyright © 2015 Gilt Groupe. All rights reserved.
//

import XCTest
Expand Down
2 changes: 1 addition & 1 deletion Tests/TargetActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Cleanroom Project
//
// Created by Evan Maloney on 3/31/15.
// Copyright (c) 2015 Gilt Groupe. All rights reserved.
// Copyright © 2015 Gilt Groupe. All rights reserved.
//

import Foundation
Expand Down

0 comments on commit d800ccb

Please sign in to comment.