forked from LemonCake/JsCoreEngineiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsCoreEngineWrapper.m
93 lines (68 loc) · 2.77 KB
/
JsCoreEngineWrapper.m
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
//
// JsCoreEngineWrapper.m
// Miso
//
// Created by Joshua Wu on 9/26/11.
// Copyright 2011 Miso. All rights reserved.
//
#import "JsCoreEngineWrapper.h"
@interface JsCoreEngineWrapper ()
- (void)evalJsStringBackground:(NSMutableDictionary *)evalParams;
- (void)returnResultToDelegate:(NSMutableDictionary *)evalParams;
- (void)reloadJsCore;
@end
@implementation JsCoreEngineWrapper
+(JsCoreEngineWrapper *)instance {
static JsCoreEngineWrapper *coreEngine = nil;
if (coreEngine == nil) {
coreEngine = [[JsCoreEngineWrapper alloc] init];
}
return coreEngine;
}
- (id)init {
if((self = [super init])) {
// ScnEngine setup
[self reloadJsCore];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadJsCore) name:@"REFRESH_TEMPLATES" object:nil];
}
return self;
}
- (void)reloadJsCore {
[_scnEngine release];
_scnEngine = [[SCNEngine alloc] init];
//NOTE: initialize any javascript libraries you wish to load here.
/*
NSString *libString = @"compiled/templates.js";
NSString *dirPath = [[TemplateController instance].localBaseUrl
stringByAppendingPathComponent:[libString stringByDeletingLastPathComponent]];
NSString *fqFilePath = [dirPath stringByAppendingPathComponent:[libString lastPathComponent]];
NSString *data = [NSString stringWithContentsOfFile:fqFilePath encoding:NSUTF8StringEncoding error:nil];
if (data) {
[_scnEngine runJS:data];
}
*/
}
- (void)evalJsString:(NSString *)jsString delegate:(id<JsCoreEngineWrapperDelegate>)delegate {
NSMutableDictionary *evalParams = [NSMutableDictionary dictionary];
[evalParams setObject:[NSNumber numberWithInt:jobNumber] forKey:@"jobNumber"];
[evalParams setObject:jsString forKey:@"jsString"];
[evalParams setObject:delegate forKey:@"delegate"];
[self performSelectorInBackground:@selector(runJSBackground:) withObject:evalParams];
}
- (void)evalJsStringBackground:(NSMutableDictionary *)evalParams {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *jsString = [evalParams objectForKey:@"jsString"];
NSString *result = [_scnEngine runJS:jsString];
if (result != nil) {
[evalParams setObject:result forKey:@"result"];
}
[self performSelectorOnMainThread:@selector(returnResultToDelegate:) withObject:evalParams waitUntilDone:NO];
[pool release];
}
- (void)returnResultToDelegate:(NSMutableDictionary *)evalParams {
id<JsCoreEngineWrapperDelegate> delegate = [evalParams objectForKey:@"delegate"];
if ([delegate respondsToSelector:@selector(JsCoreEvalResultsDidLoad:)]) {
[delegate JsCoreEvalResultsDidLoad:[evalParams objectOrNilForKey:@"result"]];
}
}
@end