Skip to content

Commit 71fa108

Browse files
authored
feat!: use plugin options for optional plugin parameters (#4762)
1 parent de67bd4 commit 71fa108

32 files changed

+215
-130
lines changed

canaries/lib/main.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ Future<void> main() async {
2020
AmplifyAuthCognito(),
2121
AmplifyStorageS3(),
2222
AmplifyAnalyticsPinpoint(),
23-
AmplifyAPI(modelProvider: ModelProvider.instance),
24-
AmplifyDataStore(modelProvider: ModelProvider.instance),
23+
AmplifyAPI(
24+
options: APIPluginOptions(modelProvider: ModelProvider.instance),
25+
),
26+
AmplifyDataStore(
27+
modelProvider: ModelProvider.instance,
28+
),
2529
]);
2630
await Amplify.configure(amplifyconfig);
2731
safePrint('Successfully configured Amplify.');

packages/amplify_core/lib/src/config/analytics/pinpoint_config.dart

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import 'package:amplify_core/amplify_core.dart';
5-
import 'package:json_annotation/json_annotation.dart';
65
import 'package:meta/meta.dart';
76

87
part 'pinpoint_config.g.dart';
@@ -36,9 +35,7 @@ class PinpointPluginConfig
3635
const PinpointPluginConfig({
3736
required this.pinpointAnalytics,
3837
required this.pinpointTargeting,
39-
int autoFlushEventsInterval = 30,
40-
}) : _autoFlushEventsInterval = autoFlushEventsInterval;
41-
38+
});
4239
factory PinpointPluginConfig.fromJson(Map<String, Object?> json) =>
4340
_$PinpointPluginConfigFromJson(json);
4441

@@ -48,16 +45,8 @@ class PinpointPluginConfig
4845
final PinpointAnalytics pinpointAnalytics;
4946
final PinpointTargeting pinpointTargeting;
5047

51-
final int _autoFlushEventsInterval;
52-
53-
/// The duration in seconds between flushing analytics events to Pinpoint.
54-
@_DurationConverter()
55-
Duration get autoFlushEventsInterval =>
56-
Duration(seconds: _autoFlushEventsInterval);
57-
5848
@override
59-
List<Object?> get props =>
60-
[pinpointAnalytics, pinpointTargeting, autoFlushEventsInterval];
49+
List<Object?> get props => [pinpointAnalytics, pinpointTargeting];
6150

6251
PinpointPluginConfig copyWith({
6352
PinpointAnalytics? pinpointAnalytics,
@@ -67,8 +56,6 @@ class PinpointPluginConfig
6756
return PinpointPluginConfig(
6857
pinpointAnalytics: pinpointAnalytics ?? this.pinpointAnalytics,
6958
pinpointTargeting: pinpointTargeting ?? this.pinpointTargeting,
70-
autoFlushEventsInterval:
71-
autoFlushEventsInterval ?? _autoFlushEventsInterval,
7259
);
7360
}
7461

@@ -132,13 +119,3 @@ class PinpointTargeting with AWSEquatable<PinpointTargeting>, AWSSerializable {
132119
@override
133120
Map<String, Object?> toJson() => _$PinpointTargetingToJson(this);
134121
}
135-
136-
class _DurationConverter implements JsonConverter<Duration, int> {
137-
const _DurationConverter();
138-
139-
@override
140-
Duration fromJson(int json) => Duration(seconds: json);
141-
142-
@override
143-
int toJson(Duration object) => object.inSeconds;
144-
}

packages/amplify_core/lib/src/config/analytics/pinpoint_config.g.dart

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_core/test/config/cli_config_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const expected = {
3939
region: REGION,
4040
),
4141
pinpointTargeting: PinpointTargeting(region: REGION),
42-
autoFlushEventsInterval: ANALYTICS_FLUSH_INTERVAL,
4342
),
4443
},
4544
),

packages/amplify_core/test/config/testdata/cli_generated.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ const _v4analytics = '''
3939
},
4040
"pinpointTargeting": {
4141
"region": "$REGION"
42-
},
43-
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
42+
}
4443
}
4544
}
4645
}
@@ -226,8 +225,7 @@ const _v5analytics = '''
226225
},
227226
"pinpointTargeting": {
228227
"region": "$REGION"
229-
},
230-
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
228+
}
231229
}
232230
}
233231
}
@@ -413,8 +411,7 @@ const _v6analytics = '''
413411
},
414412
"pinpointTargeting": {
415413
"region": "$REGION"
416-
},
417-
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
414+
}
418415
}
419416
}
420417
}
@@ -600,8 +597,7 @@ const _v7analytics = '''
600597
},
601598
"pinpointTargeting": {
602599
"region": "$REGION"
603-
},
604-
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
600+
}
605601
}
606602
}
607603
}
@@ -787,8 +783,7 @@ const _vlatestanalytics = '''
787783
},
788784
"pinpointTargeting": {
789785
"region": "$REGION"
790-
},
791-
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
786+
}
792787
}
793788
}
794789
}

packages/amplify_core/test/config/testdata/generate/generate.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const child_process = require('child_process');
1414
const REGION = '$REGION';
1515
const API_KEY = '$API_KEY';
1616
const ANALYTICS_APP_ID = '$ANALYTICS_APP_ID';
17-
const ANALYTICS_FLUSH_INTERVAL = '$ANALYTICS_FLUSH_INTERVAL';
1817
const GRAPHQL_ENDPOINT = '$GRAPHQL_ENDPOINT';
1918
const REST_ENDPOINT = '$REST_ENDPOINT';
2019
const BUCKET = '$BUCKET';

packages/amplify_core/test/config/testdata/test_values.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const API_KEY = 'MY_KEY';
2121

2222
/// Analytics values
2323
const ANALYTICS_APP_ID = 'analytics_app_id';
24-
const ANALYTICS_FLUSH_INTERVAL = 10;
2524

2625
/// API values
2726
const GRAPHQL_TYPE = 'GraphQL';

packages/amplify_datastore/example/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ class _MyAppState extends State<MyApp> {
7373
try {
7474
datastorePlugin = AmplifyDataStore(
7575
modelProvider: ModelProvider.instance,
76-
errorHandler: ((error) =>
77-
{print("Custom ErrorHandler received: " + error.toString())}),
76+
options: DataStorePluginOptions(
77+
errorHandler: ((error) =>
78+
{print("Custom ErrorHandler received: " + error.toString())}),
79+
),
7880
);
7981
await Amplify.addPlugin(datastorePlugin);
8082

packages/amplify_datastore/lib/amplify_datastore.dart

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:convert';
66

77
import 'package:amplify_core/amplify_core.dart';
88
import 'package:amplify_datastore/src/amplify_datastore_stream_controller.dart';
9+
import 'package:amplify_datastore/src/datastore_plugin_options.dart';
910
import 'package:amplify_datastore/src/method_channel_datastore.dart';
1011
import 'package:amplify_datastore/src/native_plugin.g.dart';
1112
import 'package:collection/collection.dart';
@@ -15,36 +16,24 @@ import 'package:meta/meta.dart';
1516
export 'package:amplify_core/src/types/datastore/datastore_types.dart'
1617
hide DateTimeParse;
1718

19+
export 'src/datastore_plugin_options.dart';
20+
1821
class AmplifyDataStore extends DataStorePluginInterface
1922
with AWSDebuggable, AmplifyLoggerMixin {
2023
/// Constructs an AmplifyDataStore plugin with mandatory [modelProvider]
21-
/// and optional datastore configuration properties including
22-
///
23-
/// [syncExpressions]: list of sync expressions to filter datastore sync against
24-
///
25-
/// [syncInterval]: datastore syncing interval (in seconds)
26-
///
27-
/// [syncMaxRecords]: max number of records to sync
28-
///
29-
/// [syncPageSize]: page size to sync
24+
/// and an optional datastore plugin options [options]
3025
AmplifyDataStore({
3126
required ModelProviderInterface modelProvider,
32-
Function(AmplifyException)? errorHandler,
33-
DataStoreConflictHandler? conflictHandler,
34-
List<DataStoreSyncExpression> syncExpressions = const [],
35-
int? syncInterval,
36-
int? syncMaxRecords,
37-
int? syncPageSize,
38-
AuthModeStrategy authModeStrategy = AuthModeStrategy.defaultStrategy,
27+
DataStorePluginOptions options = const DataStorePluginOptions(),
3928
}) : super(
4029
modelProvider: modelProvider,
41-
errorHandler: errorHandler,
42-
conflictHandler: conflictHandler,
43-
syncExpressions: syncExpressions,
44-
syncInterval: syncInterval,
45-
syncMaxRecords: syncMaxRecords,
46-
syncPageSize: syncPageSize,
47-
authModeStrategy: authModeStrategy,
30+
errorHandler: options.errorHandler,
31+
conflictHandler: options.conflictHandler,
32+
syncExpressions: options.syncExpressions,
33+
syncInterval: options.syncInterval,
34+
syncMaxRecords: options.syncMaxRecords,
35+
syncPageSize: options.syncPageSize,
36+
authModeStrategy: options.authModeStrategy,
4837
);
4938

5039
/// Internal use constructor
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:amplify_core/amplify_core.dart';
5+
6+
/// {@template amplify_datastore.datastore_plugin_options}
7+
/// The plugin options for the Amplify DataStore plugin.
8+
/// {@endtemplate}
9+
class DataStorePluginOptions {
10+
/// {@macro amplify_datastore.datastore_plugin_options}
11+
const DataStorePluginOptions({
12+
this.errorHandler,
13+
this.conflictHandler,
14+
this.syncExpressions = const [],
15+
this.syncInterval,
16+
this.syncMaxRecords,
17+
this.syncPageSize,
18+
this.authModeStrategy = AuthModeStrategy.defaultStrategy,
19+
});
20+
21+
/// The custom error handler function that receives an [AmplifyException]
22+
/// object when DataStore encounters an unhandled error.
23+
final Function(AmplifyException)? errorHandler;
24+
25+
/// The custom conflict handler function that receives an [ConflictData]
26+
/// object when DataStore encounters a data conflict.
27+
final DataStoreConflictHandler? conflictHandler;
28+
29+
/// The list of sync expressions to filter datastore sync.
30+
final List<DataStoreSyncExpression> syncExpressions;
31+
32+
/// The syncing interval in seconds.
33+
final int? syncInterval;
34+
35+
/// The max number of records to sync.
36+
final int? syncMaxRecords;
37+
38+
/// The page size to sync.
39+
final int? syncPageSize;
40+
41+
/// The strategy for authorizing an API call.
42+
final AuthModeStrategy authModeStrategy;
43+
}

0 commit comments

Comments
 (0)