Skip to content

Commit 2cc6f2b

Browse files
committed
Make experiment checks use dispatch_once when not debugging, clean up singleton
1 parent 32a2ebf commit 2cc6f2b

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

Source/ASConfigurationInternal.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ NS_ASSUME_NONNULL_BEGIN
2020
*
2121
* The delegate will be notified asynchronously.
2222
*/
23-
AS_EXTERN BOOL ASActivateExperimentalFeature(ASExperimentalFeatures option);
23+
#if DEBUG
24+
#define ASActivateExperimentalFeature(opt) _ASActivateExperimentalFeature(opt)
25+
#else
26+
#define ASActivateExperimentalFeature(opt) ({\
27+
static BOOL result;\
28+
static dispatch_once_t onceToken;\
29+
dispatch_once(&onceToken, ^{ result = _ASActivateExperimentalFeature(opt); });\
30+
result;\
31+
})
32+
#endif
33+
34+
/**
35+
* Internal function. Use the macro without the underbar.
36+
*/
37+
AS_EXTERN BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures option);
2438

2539
/**
2640
* Notify the configuration delegate that the framework initialized, if needed.

Source/ASConfigurationInternal.mm

+11-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
#import <AsyncDisplayKit/ASConfigurationDelegate.h>
1313
#import <stdatomic.h>
1414

15-
#define ASGetSharedConfigMgr() (__bridge ASConfigurationManager *)ASConfigurationManager.sharedInstance
15+
NS_INLINE ASConfigurationManager *ASConfigurationManagerGet() {
16+
static ASConfigurationManager *inst;
17+
static dispatch_once_t onceToken;
18+
dispatch_once(&onceToken, ^{
19+
inst = [[ASConfigurationManager alloc] init];
20+
});
21+
return inst;
22+
}
1623

1724
@implementation ASConfigurationManager {
1825
ASConfiguration *_config;
@@ -21,17 +28,6 @@ @implementation ASConfigurationManager {
2128
_Atomic(ASExperimentalFeatures) _activatedExperiments;
2229
}
2330

24-
/// Return CFTypeRef to avoid retain/release on this singleton.
25-
+ (CFTypeRef)sharedInstance
26-
{
27-
static CFTypeRef inst;
28-
static dispatch_once_t onceToken;
29-
dispatch_once(&onceToken, ^{
30-
inst = (__bridge_retained CFTypeRef)[[ASConfigurationManager alloc] init];
31-
});
32-
return inst;
33-
}
34-
3531
+ (ASConfiguration *)defaultConfiguration NS_RETURNS_RETAINED
3632
{
3733
ASConfiguration *config = [[ASConfiguration alloc] init];
@@ -103,12 +99,12 @@ + (void)test_resetWithConfiguration:(ASConfiguration *)configuration
10399

104100
@end
105101

106-
BOOL ASActivateExperimentalFeature(ASExperimentalFeatures feature)
102+
BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures feature)
107103
{
108-
return [ASGetSharedConfigMgr() activateExperimentalFeature:feature];
104+
return [ASConfigurationManagerGet() activateExperimentalFeature:feature];
109105
}
110106

111107
void ASNotifyInitialized()
112108
{
113-
[ASGetSharedConfigMgr() frameworkDidInitialize];
109+
[ASConfigurationManagerGet() frameworkDidInitialize];
114110
}

0 commit comments

Comments
 (0)