Skip to content

Commit

Permalink
Make experiment checks use dispatch_once when not debugging, clean up…
Browse files Browse the repository at this point in the history
… singleton
  • Loading branch information
Adlai-Holler committed Mar 11, 2019
1 parent 32a2ebf commit 2cc6f2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
16 changes: 15 additions & 1 deletion Source/ASConfigurationInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ NS_ASSUME_NONNULL_BEGIN
*
* The delegate will be notified asynchronously.
*/
AS_EXTERN BOOL ASActivateExperimentalFeature(ASExperimentalFeatures option);
#if DEBUG
#define ASActivateExperimentalFeature(opt) _ASActivateExperimentalFeature(opt)
#else
#define ASActivateExperimentalFeature(opt) ({\
static BOOL result;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{ result = _ASActivateExperimentalFeature(opt); });\
result;\
})
#endif

/**
* Internal function. Use the macro without the underbar.
*/
AS_EXTERN BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures option);

/**
* Notify the configuration delegate that the framework initialized, if needed.
Expand Down
26 changes: 11 additions & 15 deletions Source/ASConfigurationInternal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
#import <AsyncDisplayKit/ASConfigurationDelegate.h>
#import <stdatomic.h>

#define ASGetSharedConfigMgr() (__bridge ASConfigurationManager *)ASConfigurationManager.sharedInstance
NS_INLINE ASConfigurationManager *ASConfigurationManagerGet() {
static ASConfigurationManager *inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = [[ASConfigurationManager alloc] init];
});
return inst;
}

@implementation ASConfigurationManager {
ASConfiguration *_config;
Expand All @@ -21,17 +28,6 @@ @implementation ASConfigurationManager {
_Atomic(ASExperimentalFeatures) _activatedExperiments;
}

/// Return CFTypeRef to avoid retain/release on this singleton.
+ (CFTypeRef)sharedInstance
{
static CFTypeRef inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = (__bridge_retained CFTypeRef)[[ASConfigurationManager alloc] init];
});
return inst;
}

+ (ASConfiguration *)defaultConfiguration NS_RETURNS_RETAINED
{
ASConfiguration *config = [[ASConfiguration alloc] init];
Expand Down Expand Up @@ -103,12 +99,12 @@ + (void)test_resetWithConfiguration:(ASConfiguration *)configuration

@end

BOOL ASActivateExperimentalFeature(ASExperimentalFeatures feature)
BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures feature)
{
return [ASGetSharedConfigMgr() activateExperimentalFeature:feature];
return [ASConfigurationManagerGet() activateExperimentalFeature:feature];
}

void ASNotifyInitialized()
{
[ASGetSharedConfigMgr() frameworkDidInitialize];
[ASConfigurationManagerGet() frameworkDidInitialize];
}

0 comments on commit 2cc6f2b

Please sign in to comment.