Skip to content

chore: add amplify_lints to datastore #4994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/amplify_datastore/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include: package:amplify_lints/library.yaml

analyzer:
exclude:
- lib/**/*.g.dart
- lib/src/sdk/src/**
- lib/src/native_auth_plugin.dart
71 changes: 47 additions & 24 deletions packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class AmplifyDataStore extends DataStorePluginInterface
@protected
AmplifyDataStore.emptyConstructor() : super.emptyConstructor();

static AmplifyDataStore _instance = AmplifyDataStoreMethodChannel();
static final AmplifyDataStore _instance = AmplifyDataStoreMethodChannel();
static DataStoreStreamController streamWrapper = DataStoreStreamController();

@override
StreamController<DataStoreHubEvent> get streamController {
return streamWrapper.datastoreStreamController;
}
Expand Down Expand Up @@ -161,19 +162,21 @@ class AmplifyDataStore extends DataStorePluginInterface
@override
Future<void> configureDataStore({
ModelProviderInterface? modelProvider,
Function(AmplifyException)? errorHandler,
void Function(AmplifyException)? errorHandler,
DataStoreConflictHandler? conflictHandler,
List<DataStoreSyncExpression>? syncExpressions,
int? syncInterval,
int? syncMaxRecords,
int? syncPageSize,
AuthModeStrategy authModeStrategy = AuthModeStrategy.defaultStrategy,
}) async {
ModelProviderInterface provider = modelProvider ?? this.modelProvider!;
final provider = modelProvider ?? this.modelProvider!;
if (provider.modelSchemas.isEmpty) {
throw DataStoreException('No modelProvider or modelSchemas found',
recoverySuggestion:
'Pass in a modelProvider instance while instantiating DataStorePlugin');
throw const DataStoreException(
'No modelProvider or modelSchemas found',
recoverySuggestion:
'Pass in a modelProvider instance while instantiating DataStorePlugin',
);
}
streamWrapper.registerModelsForHub(provider);
return _instance.configureDataStore(
Expand All @@ -189,12 +192,18 @@ class AmplifyDataStore extends DataStorePluginInterface
}

@override
Future<List<T>> query<T extends Model>(ModelType<T> modelType,
{QueryPredicate? where,
QueryPagination? pagination,
List<QuerySortBy>? sortBy}) async {
return _instance.query(modelType,
where: where, pagination: pagination, sortBy: sortBy);
Future<List<T>> query<T extends Model>(
ModelType<T> modelType, {
QueryPredicate? where,
QueryPagination? pagination,
List<QuerySortBy>? sortBy,
}) async {
return _instance.query(
modelType,
where: where,
pagination: pagination,
sortBy: sortBy,
);
}

@override
Expand All @@ -208,8 +217,10 @@ class AmplifyDataStore extends DataStorePluginInterface
}

@override
Stream<SubscriptionEvent<T>> observe<T extends Model>(ModelType<T> modelType,
{QueryPredicate? where}) {
Stream<SubscriptionEvent<T>> observe<T extends Model>(
ModelType<T> modelType, {
QueryPredicate? where,
}) {
return _instance.observe(modelType, where: where);
}

Expand Down Expand Up @@ -259,26 +270,38 @@ class _NativeAmplifyAuthCognito
// authSession cannot be typed properly without depending on amplify_auth_cognito
final authSession = await Amplify.Auth.fetchAuthSession() as dynamic;
final nativeAuthSession = NativeAuthSession(
isSignedIn: authSession.isSignedIn,
userSub: authSession.userSubResult.valueOrNull,
identityId: authSession.identityIdResult.valueOrNull,
// ignore: avoid_dynamic_calls
isSignedIn: authSession.isSignedIn as bool,
// ignore: avoid_dynamic_calls
userSub: authSession.userSubResult.valueOrNull as String?,
// ignore: avoid_dynamic_calls
identityId: authSession.identityIdResult.valueOrNull as String?,
);
// ignore: avoid_dynamic_calls
final userPoolTokens = authSession.userPoolTokensResult.valueOrNull;
if (userPoolTokens != null) {
nativeAuthSession.userPoolTokens = NativeUserPoolTokens(
accessToken: userPoolTokens.accessToken.raw,
refreshToken: userPoolTokens.refreshToken,
idToken: userPoolTokens.idToken.raw,
// ignore: avoid_dynamic_calls
accessToken: userPoolTokens.accessToken.raw as String,
// ignore: avoid_dynamic_calls
refreshToken: userPoolTokens.refreshToken as String,
// ignore: avoid_dynamic_calls
idToken: userPoolTokens.idToken.raw as String,
);
}
// ignore: avoid_dynamic_calls
final awsCredentials = authSession.credentialsResult.valueOrNull;
if (awsCredentials != null) {
nativeAuthSession.awsCredentials = NativeAWSCredentials(
accessKeyId: awsCredentials.accessKeyId,
secretAccessKey: awsCredentials.secretAccessKey,
sessionToken: awsCredentials.sessionToken,
// ignore: avoid_dynamic_calls
accessKeyId: awsCredentials.accessKeyId as String,
// ignore: avoid_dynamic_calls
secretAccessKey: awsCredentials.secretAccessKey as String,
// ignore: avoid_dynamic_calls
sessionToken: awsCredentials.sessionToken as String,
expirationIso8601Utc:
awsCredentials.expiration?.toUtc().toIso8601String(),
// ignore: avoid_dynamic_calls
awsCredentials.expiration?.toUtc().toIso8601String() as String?,
);
}
return nativeAuthSession;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter/services.dart';

const channel = EventChannel('com.amazonaws.amplify/datastore_hub_events');
late ModelProviderInterface modelProvider;
StreamSubscription? eventStream;
StreamSubscription<Map<String, Object?>>? eventStream;

class DataStoreStreamController {
StreamController<DataStoreHubEvent> get datastoreStreamController {
Expand All @@ -26,102 +26,107 @@ StreamController<DataStoreHubEvent> _controller =
onCancel: _onCancel,
);

_onListen() {
if (eventStream == null) {
eventStream = channel
.receiveBroadcastStream(1)
.cast<Map<Object?, Object?>>()
.listen((event) {
final map = event.cast<String, Object?>();
final eventName = map['eventName'] as String;
switch (eventName) {
case 'ready':
{
_rebroadcast(DataStoreHubEventType.ready);
}
void _onListen() {
eventStream ??= channel
.receiveBroadcastStream(1)
.cast<Map<String, Object?>>()
.listen((event) {
final map = event.cast<String, Object?>();
final eventName = map['eventName'] as String;
switch (eventName) {
case 'ready':
{
_rebroadcast(DataStoreHubEventType.ready);
}
case 'networkStatus':
{
_rebroadcast(
DataStoreHubEventType.networkStatus,
payload: NetworkStatusEvent(map),
);
}
case 'subscriptionsEstablished':
{
_rebroadcast(DataStoreHubEventType.subscriptionsEstablished);
}
case 'syncQueriesStarted':
{
_rebroadcast(
DataStoreHubEventType.syncQueriesStarted,
payload: SyncQueriesStartedEvent(map),
);
}
case 'modelSynced':
{
_rebroadcast(
DataStoreHubEventType.modelSynced,
payload: ModelSyncedEvent(map),
);
}
case 'syncQueriesReady':
{
_rebroadcast(DataStoreHubEventType.syncQueriesReady);
}
case 'outboxMutationEnqueued':
{
_rebroadcast(
DataStoreHubEventType.outboxMutationEnqueued,
payload: OutboxMutationEvent(map, modelProvider, eventName),
);
}
case 'outboxMutationProcessed':
{
_rebroadcast(
DataStoreHubEventType.outboxMutationProcessed,
payload: OutboxMutationEvent(map, modelProvider, eventName),
);
}
case 'outboxMutationFailed':
{
_rebroadcast(
DataStoreHubEventType.outboxMutationFailed,
payload: OutboxMutationEvent(map, modelProvider, eventName),
);
}
case 'outboxStatus':
{
_rebroadcast(
DataStoreHubEventType.outboxStatus,
payload: OutboxStatusEvent(map),
);
}
// event name in amplify-android
case 'subscriptionDataProcessed':
// event name in amplify-ios
case 'syncReceived':
{
_rebroadcast(
DataStoreHubEventType.subscriptionDataProcessed,
payload: SubscriptionDataProcessedEvent(
map,
modelProvider,
),
);
break;
case 'networkStatus':
{
_rebroadcast(DataStoreHubEventType.networkStatus,
payload: NetworkStatusEvent(map));
}
break;
case 'subscriptionsEstablished':
{
_rebroadcast(DataStoreHubEventType.subscriptionsEstablished);
}
break;
case 'syncQueriesStarted':
{
_rebroadcast(DataStoreHubEventType.syncQueriesStarted,
payload: SyncQueriesStartedEvent(map));
}
break;
case 'modelSynced':
{
_rebroadcast(DataStoreHubEventType.modelSynced,
payload: ModelSyncedEvent(map));
}
break;
case 'syncQueriesReady':
{
_rebroadcast(DataStoreHubEventType.syncQueriesReady);
}
break;
case 'outboxMutationEnqueued':
{
_rebroadcast(DataStoreHubEventType.outboxMutationEnqueued,
payload: OutboxMutationEvent(map, modelProvider, eventName));
}
break;
case 'outboxMutationProcessed':
{
_rebroadcast(DataStoreHubEventType.outboxMutationProcessed,
payload: OutboxMutationEvent(map, modelProvider, eventName));
}
break;
case 'outboxMutationFailed':
{
_rebroadcast(DataStoreHubEventType.outboxMutationFailed,
payload: OutboxMutationEvent(map, modelProvider, eventName));
}
break;
case 'outboxStatus':
{
_rebroadcast(DataStoreHubEventType.outboxStatus,
payload: OutboxStatusEvent(map));
}
break;
// event name in amplify-android
case 'subscriptionDataProcessed':
// event name in amplify-ios
case 'syncReceived':
{
_rebroadcast(
DataStoreHubEventType.subscriptionDataProcessed,
payload: SubscriptionDataProcessedEvent(
map,
modelProvider,
),
);
break;
}
default:
break;
}
});
}
}
default:
break;
}
});
}

_rebroadcast(DataStoreHubEventType type, {DataStoreHubEventPayload? payload}) {
void _rebroadcast(
DataStoreHubEventType type, {
DataStoreHubEventPayload? payload,
}) {
try {
_controller.add(DataStoreHubEvent(type.name, type: type, payload: payload));
} catch (e) {
print(e);
} on Exception catch (e) {
safePrint(e);
}
}

_onCancel() {
void _onCancel() {
if (!_controller.hasListener) {
eventStream?.cancel();
eventStream = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DataStorePluginOptions {

/// The custom error handler function that receives an [AmplifyException]
/// object when DataStore encounters an unhandled error.
final Function(AmplifyException)? errorHandler;
final void Function(AmplifyException)? errorHandler;

/// The custom conflict handler function that receives an [ConflictData]
/// object when DataStore encounters a data conflict.
Expand Down
Loading
Loading