-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathJSObjectionProviderEntry.m
More file actions
61 lines (49 loc) · 1.45 KB
/
JSObjectionProviderEntry.m
File metadata and controls
61 lines (49 loc) · 1.45 KB
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
#import "JSObjectionProviderEntry.h"
@interface JSObjectionProviderEntry () {
id<JSObjectionProvider> _provider;
id(^_block)(JSObjectionInjector *context);
JSObjectionScope _lifeCycle;
id _storageCache;
}
@end
@implementation JSObjectionProviderEntry
@synthesize lifeCycle = _lifeCycle;
- (id)initWithProvider:(id<JSObjectionProvider>)theProvider lifeCycle:(JSObjectionScope)theLifeCycle {
if ((self = [super init])) {
_provider = theProvider;
_lifeCycle = theLifeCycle;
_storageCache = nil;
}
return self;
}
- (id)initWithBlock:(id(^)(JSObjectionInjector *context))theBlock lifeCycle:(JSObjectionScope)theLifeCycle {
if ((self = [super init])) {
_block = [theBlock copy];
_lifeCycle = theLifeCycle;
_storageCache = nil;
}
return self;
}
- (id)extractObject:(NSArray *)arguments {
if (self.lifeCycle == JSObjectionScopeNormal || !_storageCache) {
return [self buildObject:arguments];
}
return _storageCache;
}
- (void)dealloc {
_storageCache = nil;
}
- (id)buildObject:(NSArray *)arguments {
id objectUnderConstruction = nil;
if (_block) {
objectUnderConstruction = _block(self.injector);
}
else {
objectUnderConstruction = [_provider provide:self.injector arguments:arguments];
}
if (self.lifeCycle == JSObjectionScopeSingleton) {
_storageCache = objectUnderConstruction;
}
return objectUnderConstruction;
}
@end