Skip to content

Commit

Permalink
Reworking the interval stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Nov 22, 2013
1 parent 5c29449 commit 7b4d67d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 23 deletions.
8 changes: 8 additions & 0 deletions Cocoa Script.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@
CC66D8D5181A2E2F0039A0A5 /* JSTTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = CC66D8CD181A2E2F0039A0A5 /* JSTTextView.m */; };
CC66D8D6181A2E2F0039A0A5 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = CC66D8CE181A2E2F0039A0A5 /* main.m */; };
CC66D8D8181A2FC10039A0A5 /* cocoascript_tool.m in Sources */ = {isa = PBXBuildFile; fileRef = CC66D8D7181A2FC10039A0A5 /* cocoascript_tool.m */; };
CC847AFC183EE02300436C38 /* COScript+Interval.h in Headers */ = {isa = PBXBuildFile; fileRef = CC847AFA183EE02300436C38 /* COScript+Interval.h */; };
CC847AFD183EE02300436C38 /* COScript+Interval.m in Sources */ = {isa = PBXBuildFile; fileRef = CC847AFB183EE02300436C38 /* COScript+Interval.m */; };
CC86430D163B29E600908427 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CC86430C163B29E600908427 /* QuartzCore.framework */; };
CC86430F163B29F500908427 /* OpenCL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CC86430E163B29F500908427 /* OpenCL.framework */; };
CC94E0520F4A616300208BE0 /* JSTalk.icns in Resources */ = {isa = PBXBuildFile; fileRef = CC94E0510F4A616300208BE0 /* JSTalk.icns */; };
Expand Down Expand Up @@ -559,6 +561,8 @@
CC66D8CD181A2E2F0039A0A5 /* JSTTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = JSTTextView.m; path = src/editor/JSTTextView.m; sourceTree = "<group>"; };
CC66D8CE181A2E2F0039A0A5 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = src/editor/main.m; sourceTree = "<group>"; };
CC66D8D7181A2FC10039A0A5 /* cocoascript_tool.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = cocoascript_tool.m; path = src/cocoascript_tool.m; sourceTree = "<group>"; };
CC847AFA183EE02300436C38 /* COScript+Interval.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "COScript+Interval.h"; path = "src/framework/COScript+Interval.h"; sourceTree = "<group>"; };
CC847AFB183EE02300436C38 /* COScript+Interval.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "COScript+Interval.m"; path = "src/framework/COScript+Interval.m"; sourceTree = "<group>"; };
CC86430C163B29E600908427 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = ../../../../../../System/Library/Frameworks/QuartzCore.framework; sourceTree = "<group>"; };
CC86430E163B29F500908427 /* OpenCL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenCL.framework; path = ../../../../../../System/Library/Frameworks/OpenCL.framework; sourceTree = "<group>"; };
CC94E0510F4A616300208BE0 /* JSTalk.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = JSTalk.icns; path = res/images/JSTalk.icns; sourceTree = "<group>"; };
Expand Down Expand Up @@ -919,6 +923,8 @@
children = (
CCA1A533183EA86E000159B3 /* COSInterval.h */,
CCA1A534183EA86E000159B3 /* COSInterval.m */,
CC847AFA183EE02300436C38 /* COScript+Interval.h */,
CC847AFB183EE02300436C38 /* COScript+Interval.m */,
);
name = helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1131,6 +1137,7 @@
CC66D85E181A2ABF0039A0A5 /* NSOrderedSet+MochaAdditions.h in Headers */,
CC66D7C5181A2A470039A0A5 /* TDCollectionParser.h in Headers */,
CC66D7B9181A2A460039A0A5 /* TDAlternation.h in Headers */,
CC847AFC183EE02300436C38 /* COScript+Interval.h in Headers */,
CC66D8AE181A2B0A0039A0A5 /* MOMethodDescription.h in Headers */,
CC66D7C7181A2A470039A0A5 /* TDComment.h in Headers */,
);
Expand Down Expand Up @@ -1314,6 +1321,7 @@
CC66D823181A2A710039A0A5 /* COSListener.m in Sources */,
CC66D7C2181A2A470039A0A5 /* TDChar.m in Sources */,
CC66D757181A2A210039A0A5 /* NoodleLineNumberMarker.m in Sources */,
CC847AFD183EE02300436C38 /* COScript+Interval.m in Sources */,
CC66D7C6181A2A470039A0A5 /* TDCollectionParser.m in Sources */,
CC66D7ED181A2A470039A0A5 /* TDSequence.m in Sources */,
CC66D8B6181A2B0A0039A0A5 /* MOPointerValue.m in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions examples/COSInterval.coscript
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var cosi = [COSInterval scheduleWithInterval:1 jsFunction:function(cinterval) {
var cosi = [coscript scheduleWithInterval:1 jsFunction:function(cinterval) {
print("hi!");
}];


var idx = 0;

[COSInterval scheduleWithRepeatingInterval:2 jsFunction:function(cinterval) {
[coscript scheduleWithRepeatingInterval:2 jsFunction:function(cinterval) {
print("hello again! (" + idx + ")");

idx++;
Expand Down
9 changes: 7 additions & 2 deletions src/framework/COSInterval.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
#import <CocoaScript/COScript.h>

@interface COSInterval : NSObject {
COScript *_callingContext;
MOJavaScriptObject *_jsfunc;

NSTimer *_timer;
BOOL _onshot;
}

@property (weak) COScript *coscript;
@property (strong) MOJavaScriptObject *jsfunc;

+ (id)scheduleWithInterval:(NSTimeInterval)i cocoaScript:(COScript*)cos jsFunction:(MOJavaScriptObject *)jsFunction repeat:(BOOL)repeat;
- (void)cancel;

@end
26 changes: 8 additions & 18 deletions src/framework/COSInterval.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,22 @@
//

#import "COSInterval.h"

#pragma message "FIXME: This guy should be tied to the COScript class somehow - like adding a timer there. Or at least add some sort of notification that gets sent out when the context is rerun, so that repeating intervals don't continue to happen again and again with each run... and then these guys can get dealloc'd somehow."


#import "COScript+Interval.h"
@implementation COSInterval

+ (id)scheduleWithRepeatingInterval:(NSTimeInterval)i jsFunction:(MOJavaScriptObject *)jsFunction {
return [self scheduleWithInterval:i jsFunction:jsFunction repeat:YES];
}

+ (id)scheduleWithInterval:(NSTimeInterval)i jsFunction:(MOJavaScriptObject *)jsFunction {
return [self scheduleWithInterval:i jsFunction:jsFunction repeat:NO];
}

+ (id)scheduleWithInterval:(NSTimeInterval)i jsFunction:(MOJavaScriptObject *)jsFunction repeat:(BOOL)repeat {
+ (id)scheduleWithInterval:(NSTimeInterval)i cocoaScript:(COScript*)cos jsFunction:(MOJavaScriptObject *)jsFunction repeat:(BOOL)repeat {

COSInterval *interval = [[[self class] alloc] init];

interval->_jsfunc = jsFunction;
[interval setJsfunc:jsFunction];
[interval setCoscript:cos];

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:i target:interval selector:@selector(timerHit:) userInfo:nil repeats:repeat];

interval->_onshot = !repeat;

interval->_timer = t;

interval->_callingContext = [COScript currentCOScript];

return interval;

}
Expand All @@ -45,8 +33,10 @@ - (void)dealloc {

- (void)cleanup {

[_coscript removeInterval:self];

_jsfunc = nil;
_callingContext = nil;
_coscript = nil;
}

- (void)cancel {
Expand All @@ -58,7 +48,7 @@ - (void)cancel {

- (void)timerHit:(NSTimer*)timer {

[_callingContext callJSFunction:[_jsfunc JSObject] withArgumentsInArray:@[self]];
[_coscript callJSFunction:[_jsfunc JSObject] withArgumentsInArray:@[self]];

if (_onshot) {
[self cancel];
Expand Down
17 changes: 17 additions & 0 deletions src/framework/COScript+Interval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// COSScript+Interval.h
// Cocoa Script
//
// Created by August Mueller on 11/21/13.
//
//

#import <Foundation/Foundation.h>
#import <CocoaScript/COScript.h>

@class COSInterval;

@interface COScript (IntervalAdditions)
- (void)cleanupIntervals;
- (void)removeInterval:(COSInterval*)interval;
@end
64 changes: 64 additions & 0 deletions src/framework/COScript+Interval.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// COScript+Interval.m
// Cocoa Script
//
// Created by August Mueller on 11/21/13.
//
//

#import "COScript+Interval.h"
#import "COSInterval.h"

@implementation COScript (IntervalAdditions)

- (void)addInterval:(COSInterval*)i {

if (!_intervals) {
_intervals = [NSMutableArray array];
}

[_intervals addObject:i];
}

- (void)cleanupIntervals {

for (COSInterval *i in _intervals) {
[i cancel];
}

[_intervals removeAllObjects];

_intervals = nil;

}

- (void)removeInterval:(COSInterval*)interval {

if ([_intervals indexOfObject:interval] == NSNotFound) {
NSLog(@"Could not remove interval %@ because it is not in %@'s interval stack.", interval, self);
return;
}

debug(@"removing %@", interval);

[_intervals removeObject:interval];
}

- (id)scheduleWithRepeatingInterval:(NSTimeInterval)i jsFunction:(MOJavaScriptObject *)jsFunction {

COSInterval *cosi = [COSInterval scheduleWithInterval:i cocoaScript:self jsFunction:jsFunction repeat:YES];
[self addInterval:cosi];

return cosi;
}

- (id)scheduleWithInterval:(NSTimeInterval)i jsFunction:(MOJavaScriptObject *)jsFunction {

COSInterval *cosi = [COSInterval scheduleWithInterval:i cocoaScript:self jsFunction:jsFunction repeat:NO];
[self addInterval:cosi];

return cosi;
}


@end
4 changes: 3 additions & 1 deletion src/framework/COScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
@class COScript;

@interface COScript : NSObject {

Mocha *_mochaRuntime;

NSMutableArray *_intervals;
}

@property (weak) id printController;
Expand Down
5 changes: 5 additions & 0 deletions src/framework/COScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "COScript.h"
#import "COSListener.h"
#import "COSPreprocessor.h"
#import "COScript+Interval.h"

#import <ScriptingBridge/ScriptingBridge.h>
#import "MochaRuntime.h"
#import "MOMethod.h"
Expand Down Expand Up @@ -64,6 +66,9 @@ - (id)init {

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];

[self cleanupIntervals];

}


Expand Down

0 comments on commit 7b4d67d

Please sign in to comment.