-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a centralized configuration API (#747)
* Update the dangerfile * Make a trivial change to test new dangerfile * Try out the new value with another trivial change * Add a configuration API to make a unified place for pulling config from clients safely * Specify properties for delegate * Finish removing text experiment global enable * Generate the config file * Clean up configuration to fix tests * Work on making it serializable * Finish it up * Fix example code * Update sample project * Clean up a few things * Align with new project order * Make it faster and update license header * Add an option to specify your config at compile time * Update another license header * Add a version field, and bring interface state coalescing into configuration * Update CA queue code * Update CATransactionQueue tests * Turn transaction queue on by default (for now, see comment) * Update the tests * Update the tests AGAIN * Remove unused ordered set
- Loading branch information
1 parent
3d9fe8c
commit 27fac9f
Showing
46 changed files
with
838 additions
and
320 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"id": "configuration.json", | ||
"title": "configuration", | ||
"description" : "Schema definition of a Texture Configuration", | ||
"$schema": "http://json-schema.org/schema#", | ||
"type": "object", | ||
"properties": { | ||
"version" : { | ||
"type" : "number" | ||
}, | ||
"experimental_features": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"enum": [ | ||
"exp_graphics_contexts", | ||
"exp_text_node", | ||
"exp_interface_state_coalesce" | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// ASConfiguration.h | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <AsyncDisplayKit/ASBaseDefines.h> | ||
#import <AsyncDisplayKit/ASExperimentalFeatures.h> | ||
|
||
@protocol ASConfigurationDelegate; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
static NSInteger const ASConfigurationSchemaCurrentVersion = 1; | ||
|
||
AS_SUBCLASSING_RESTRICTED | ||
@interface ASConfiguration : NSObject <NSCopying> | ||
|
||
/** | ||
* Initialize this configuration with the provided dictionary, | ||
* or nil to create an empty configuration. | ||
* | ||
* The schema is located in `schemas/configuration.json`. | ||
*/ | ||
- (instancetype)initWithDictionary:(nullable NSDictionary *)dictionary; | ||
|
||
/** | ||
* The delegate for configuration-related events. | ||
* Delegate methods are called from a serial queue. | ||
*/ | ||
@property (nonatomic, strong, nullable) id<ASConfigurationDelegate> delegate; | ||
|
||
/** | ||
* The experimental features to enable in Texture. | ||
* See ASExperimentalFeatures for functions to convert to/from a string array. | ||
*/ | ||
@property (nonatomic) ASExperimentalFeatures experimentalFeatures; | ||
|
||
@end | ||
|
||
/** | ||
* Implement this method in a category to make your | ||
* configuration available to Texture. It will be read | ||
* only once and copied. | ||
* | ||
* NOTE: To specify your configuration at compile-time, you can | ||
* define AS_FIXED_CONFIG_JSON as a C-string of JSON. This method | ||
* will then be implemented to parse that string and generate | ||
* a configuration. | ||
*/ | ||
@interface ASConfiguration (UserProvided) | ||
+ (ASConfiguration *)textureConfiguration NS_RETURNS_RETAINED; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// ASConfiguration.m | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <AsyncDisplayKit/ASConfiguration.h> | ||
#import <AsyncDisplayKit/ASConfigurationInternal.h> | ||
|
||
/// Not too performance-sensitive here. | ||
|
||
/// Get this from C++, without the extra exception handling. | ||
#define autotype __auto_type | ||
|
||
@implementation ASConfiguration | ||
|
||
- (instancetype)initWithDictionary:(NSDictionary *)dictionary | ||
{ | ||
if (self = [super init]) { | ||
autotype featureStrings = ASDynamicCast(dictionary[@"experimental_features"], NSArray); | ||
autotype version = ASDynamicCast(dictionary[@"version"], NSNumber).integerValue; | ||
if (version != ASConfigurationSchemaCurrentVersion) { | ||
NSLog(@"Texture warning: configuration schema is old version (%zd vs %zd)", version, ASConfigurationSchemaCurrentVersion); | ||
} | ||
self.experimentalFeatures = ASExperimentalFeaturesFromArray(featureStrings); | ||
} | ||
return self; | ||
} | ||
|
||
- (id)copyWithZone:(NSZone *)zone | ||
{ | ||
ASConfiguration *config = [[ASConfiguration alloc] initWithDictionary:nil]; | ||
config.experimentalFeatures = self.experimentalFeatures; | ||
config.delegate = self.delegate; | ||
return config; | ||
} | ||
|
||
@end | ||
|
||
//#define AS_FIXED_CONFIG_JSON "{ \"version\" : 1, \"experimental_features\": [ \"exp_text_node\" ] }" | ||
|
||
#ifdef AS_FIXED_CONFIG_JSON | ||
|
||
@implementation ASConfiguration (UserProvided) | ||
|
||
+ (ASConfiguration *)textureConfiguration NS_RETURNS_RETAINED | ||
{ | ||
NSData *data = [@AS_FIXED_CONFIG_JSON dataUsingEncoding:NSUTF8StringEncoding]; | ||
NSError *error; | ||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; | ||
if (!d) { | ||
NSAssert(NO, @"Error parsing fixed config string '%s': %@", AS_FIXED_CONFIG_JSON, error); | ||
return nil; | ||
} else { | ||
return [[ASConfiguration alloc] initWithDictionary:d]; | ||
} | ||
} | ||
|
||
@end | ||
|
||
#endif // AS_FIXED_CONFIG_JSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// ASConfigurationDelegate.h | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <AsyncDisplayKit/ASConfiguration.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/** | ||
* Used to communicate configuration-related events to the client. | ||
*/ | ||
@protocol ASConfigurationDelegate <NSObject> | ||
|
||
/** | ||
* Texture performed its first behavior related to the feature(s). | ||
* This can be useful for tracking the impact of the behavior (A/B testing). | ||
*/ | ||
- (void)textureDidActivateExperimentalFeatures:(ASExperimentalFeatures)features; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// ASExperimentalFeatures.h | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <AsyncDisplayKit/ASBaseDefines.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
ASDISPLAYNODE_EXTERN_C_BEGIN | ||
|
||
/** | ||
* A bit mask of features. | ||
*/ | ||
typedef NS_OPTIONS(NSUInteger, ASExperimentalFeatures) { | ||
ASExperimentalGraphicsContexts = 1 << 0, // exp_graphics_contexts | ||
ASExperimentalTextNode = 1 << 1, // exp_text_node | ||
ASExperimentalInterfaceStateCoalescing = 1 << 2, // exp_interface_state_coalesce | ||
ASExperimentalFeatureAll = 0xFFFFFFFF | ||
}; | ||
|
||
/// Convert flags -> name array. | ||
NSArray<NSString *> *ASExperimentalFeaturesGetNames(ASExperimentalFeatures flags); | ||
|
||
/// Convert name array -> flags. | ||
ASExperimentalFeatures ASExperimentalFeaturesFromArray(NSArray<NSString *> *array); | ||
|
||
ASDISPLAYNODE_EXTERN_C_END | ||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// ASExperimentalFeatures.m | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <AsyncDisplayKit/ASExperimentalFeatures.h> | ||
|
||
NSArray<NSString *> *ASExperimentalFeaturesGetNames(ASExperimentalFeatures flags) | ||
{ | ||
NSArray *allNames = ASCreateOnce((@[@"exp_graphics_contexts", | ||
@"exp_text_node", | ||
@"exp_interface_state_coalesce"])); | ||
|
||
if (flags == ASExperimentalFeatureAll) { | ||
return allNames; | ||
} | ||
|
||
// Go through all names, testing each bit. | ||
NSUInteger i = 0; | ||
return ASArrayByFlatMapping(allNames, NSString *name, ({ | ||
(flags & (1 << i++)) ? name : nil; | ||
})); | ||
} | ||
|
||
// O(N^2) but with counts this small, it's probably faster | ||
// than hashing the strings. | ||
ASExperimentalFeatures ASExperimentalFeaturesFromArray(NSArray<NSString *> *array) | ||
{ | ||
NSArray *allNames = ASExperimentalFeaturesGetNames(ASExperimentalFeatureAll); | ||
ASExperimentalFeatures result = 0; | ||
for (NSString *str in array) { | ||
NSUInteger i = [allNames indexOfObject:str]; | ||
if (i != NSNotFound) { | ||
result |= (1 << i); | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.