Skip to content

Commit

Permalink
feat(crystalline): Refactor term error to failure in all packages
Browse files Browse the repository at this point in the history
  • Loading branch information
easazade committed Sep 22, 2023
1 parent 2fc63a6 commit cb4954e
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class _HomePageState extends State<HomePage> {
'on of required data in Home Store'
' has null value aka not available',
),
onError: (context, _) => Text(homeStore.error.message),
onFailure: (context, _) => Text(homeStore.failure.message),
),
],
),
Expand Down
4 changes: 2 additions & 2 deletions example/lib/flutter_crystalline_example/home/home_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HomeStore extends Store {
final newValue = ++number.value;
number.value = null;
number.operation = Operation.update;
error = null;
failure = null;
notifyListeners();

await Future.delayed(const Duration(seconds: 1));
Expand All @@ -31,7 +31,7 @@ class HomeStore extends Store {
notifyListeners();

await Future.delayed(const Duration(seconds: 1));
error = Failure('Some fake made error');
failure = Failure('Some fake made failure');
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class ProfileStore extends ChangeNotifierData {
print('updating profile');

operation = Operation.operating;
error = Failure('message');
failure = Failure('message');

await Future.delayed(const Duration(seconds: 1));

operation = Operation.none;
error = null;
failure = null;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ class Authenticated extends AuthState {
Authenticated(this.user, this.isAnonymous);
}

class Error extends AuthState {}
class Failure extends AuthState {}

// ####################################################################################
// ####################################################################################
// ####################################################################################
// ####################################################################################

class AuthStore extends Store {

// we can run operations on user and it will be distinguished from other operations
// we can access this data while operations are running, ( assuming it is ok to since this is auth store)
// we can have custom operations if we needed to
// we can have custom operations if we needed to
final Data<User> user = Data();

// we can easily distinguish between operations. operations even can run at the same time
// we can have custom operations in OperationData if we needed to
// in fact we can have a single login OperationData with custom Operation('loggingInAnonymously')
// assuming we would show error the same way for both operations
// assuming we would show failure the same way for both operations
final OperationData login = OperationData();
final OperationData anonymousLogin = OperationData();

Expand Down
40 changes: 20 additions & 20 deletions packages/crystalline/lib/src/data_types/collection_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:crystalline/src/data_types/failure.dart';
import 'package:collection/collection.dart';

typedef _DataPredicate<T> = bool Function(
List<Data<T>> value, Operation operation, Failure? error)?;
List<Data<T>> value, Operation operation, Failure? failure)?;

abstract class CollectionData<T> extends Data<List<Data<T>>>
with Iterable<Data<T>> {
Expand Down Expand Up @@ -124,7 +124,7 @@ abstract class CollectionData<T> extends Data<List<Data<T>>>
items.addAll(data.value.toList());
items.forEach((e) => _addObserversToItem(e));
operation = data.operation;
error = data.errorOrNull;
failure = data.failureOrNull;
sideEffects.clear();
sideEffects.addAll(data.sideEffects);
allowNotifyObservers();
Expand Down Expand Up @@ -154,7 +154,7 @@ abstract class CollectionData<T> extends Data<List<Data<T>>>
CollectionData<T> copy() => ListData(
items.toList().map((data) => data.copy()).toList(),
operation: this.operation,
error: this.errorOrNull,
failure: this.failureOrNull,
);

@override
Expand All @@ -164,18 +164,18 @@ abstract class CollectionData<T> extends Data<List<Data<T>>>
return runtimeType == other.runtimeType &&
ListEquality<Data<T>>().equals(items, other.items) &&
operation == other.operation &&
errorOrNull == other.errorOrNull;
failureOrNull == other.failureOrNull;
}
}

class ListData<T> extends CollectionData<T> {
ListData(
this.items, {
Operation operation = Operation.none,
Failure? error,
Failure? failure,
List<dynamic>? sideEffects,
this.isOperatingStrategy,
this.hasErrorStrategy,
this.hasFailureStrategy,
this.hasValueStrategy,
this.hasNoValueStrategy,
this.isCreatingStrategy,
Expand All @@ -185,7 +185,7 @@ class ListData<T> extends CollectionData<T> {
this.hasCustomOperationStrategy,
}) {
this.operation = operation;
this.error = error;
this.failure = failure;
if (sideEffects != null) {
addAllSideEffects(sideEffects);
}
Expand All @@ -195,7 +195,7 @@ class ListData<T> extends CollectionData<T> {
final List<Data<T>> items;

final _DataPredicate<T> isOperatingStrategy;
final _DataPredicate<T> hasErrorStrategy;
final _DataPredicate<T> hasFailureStrategy;
final _DataPredicate<T> hasValueStrategy;
final _DataPredicate<T> hasNoValueStrategy;
final _DataPredicate<T> isCreatingStrategy;
Expand All @@ -206,63 +206,63 @@ class ListData<T> extends CollectionData<T> {

@override
bool get isOperating {
return isOperatingStrategy?.call(value, operation, errorOrNull) ??
return isOperatingStrategy?.call(value, operation, failureOrNull) ??
super.isOperating;
}

@override
bool get hasError {
return hasErrorStrategy?.call(value, operation, errorOrNull) ??
super.hasError;
bool get hasFailure {
return hasFailureStrategy?.call(value, operation, failureOrNull) ??
super.hasFailure;
}

@override
bool get hasValue {
return hasValueStrategy?.call(value, operation, errorOrNull) ??
return hasValueStrategy?.call(value, operation, failureOrNull) ??
super.hasValue;
}

@override
bool get hasNoValue {
return hasNoValueStrategy?.call(value, operation, errorOrNull) ??
return hasNoValueStrategy?.call(value, operation, failureOrNull) ??
super.hasNoValue;
}

@override
bool get isCreating {
return isCreatingStrategy?.call(value, operation, errorOrNull) ??
return isCreatingStrategy?.call(value, operation, failureOrNull) ??
super.isCreating;
}

@override
bool get isDeleting {
return isDeletingStrategy?.call(value, operation, errorOrNull) ??
return isDeletingStrategy?.call(value, operation, failureOrNull) ??
super.isDeleting;
}

@override
bool get isFetching {
return isFetchingStrategy?.call(value, operation, errorOrNull) ??
return isFetchingStrategy?.call(value, operation, failureOrNull) ??
super.isFetching;
}

@override
bool get isUpdating {
return isUpdatingStrategy?.call(value, operation, errorOrNull) ??
return isUpdatingStrategy?.call(value, operation, failureOrNull) ??
super.isUpdating;
}

@override
bool get hasCustomOperation {
return hasCustomOperationStrategy?.call(value, operation, errorOrNull) ??
return hasCustomOperationStrategy?.call(value, operation, failureOrNull) ??
super.hasCustomOperation;
}

@override
ListData<T> copy() => ListData(
items.toList().map((data) => data.copy()).toList(),
operation: this.operation,
error: this.errorOrNull,
failure: this.failureOrNull,
);

@override
Expand Down
8 changes: 4 additions & 4 deletions packages/crystalline/lib/src/data_types/context_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import 'package:crystalline/src/exceptions.dart';
class ContextData<T, C> extends Data<T> {
ContextData({
T? value,
Failure? error,
Failure? failure,
Operation operation = Operation.none,
List<dynamic>? sideEffects,
C? context,
}) : _context = context,
super(
value: value,
error: error,
failure: failure,
operation: operation,
sideEffects: sideEffects,
);
Expand Down Expand Up @@ -55,7 +55,7 @@ class ContextData<T, C> extends Data<T> {
disallowNotifyObservers();
value = data.valueOrNull;
operation = data.operation;
error = data.errorOrNull;
failure = data.failureOrNull;
context = data.context;
sideEffects.clear();
sideEffects.addAll(data.sideEffects);
Expand All @@ -66,7 +66,7 @@ class ContextData<T, C> extends Data<T> {
@override
ContextData<T, C> copy() => ContextData(
value: valueOrNull,
error: errorOrNull,
failure: failureOrNull,
operation: operation,
context: contextOrNull,
);
Expand Down
Loading

0 comments on commit cb4954e

Please sign in to comment.