Skip to content

Commit

Permalink
this guy can actually be called from multiple threads. Let's make it …
Browse files Browse the repository at this point in the history
…thread-safe then
  • Loading branch information
Bertrand Guiheneuf committed Oct 18, 2013
1 parent 881887a commit dafec6f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/handlebars-objc/astVisitors/HBAstVisitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ @implementation HBAstVisitor
+ (SEL)visitorMethodForAstClassName:(NSString*)classname;
{
static NSMutableDictionary* methodNames = nil;
if (methodNames == nil) {

static dispatch_once_t pred;
dispatch_once(&pred, ^{
methodNames = [[NSMutableDictionary alloc] init];
}
});

if (methodNames[classname] == nil) {
NSAssert([classname hasPrefix:@"HBAst"], @"Ast class names must all start with 'HBAst' (%@)", classname);
NSString* shortenedNodeClass = [classname substringFromIndex:5];
NSString* methodName = [NSString stringWithFormat:@"visit%@:", shortenedNodeClass];
methodNames[classname] = methodName;
@synchronized(methodNames) {
if (methodNames[classname] == nil) {
NSAssert([classname hasPrefix:@"HBAst"], @"Ast class names must all start with 'HBAst' (%@)", classname);
NSString* shortenedNodeClass = [classname substringFromIndex:5];
NSString* methodName = [NSString stringWithFormat:@"visit%@:", shortenedNodeClass];
methodNames[classname] = methodName;
}
}

return NSSelectorFromString(methodNames[classname]);
Expand Down

0 comments on commit dafec6f

Please sign in to comment.