Skip to content

Commit

Permalink
V1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
escamoteur committed Sep 11, 2023
1 parent 8f709e2 commit b2b63dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 1.0.4
* added some more asserts to provide better error messages in case you forgot to use the WatchItMixin
## 1.0.3
* bumped get_it version
## 1.0.2
Expand Down
80 changes: 53 additions & 27 deletions lib/src/watch_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ T watch<T extends Listenable>(T target) {
/// default one. 99% of the time you won't need this.
T watchIt<T extends Listenable>({String? instanceName, GetIt? getIt}) {
assert(_activeWatchItState != null,
'watch can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
'watchIt can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
final getItInstance = getIt ?? di;
final observedObject = getItInstance<T>(instanceName: instanceName);
_activeWatchItState!.watchListenable(target: observedObject);
Expand All @@ -118,7 +118,7 @@ T watchIt<T extends Listenable>({String? instanceName, GetIt? getIt}) {
R watchValue<T extends Object, R>(ValueListenable<R> Function(T) selectProperty,
{String? instanceName, GetIt? getIt}) {
assert(_activeWatchItState != null,
'watch can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
'watchValue can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
ValueListenable<R> observedObject;
final getItInstance = getIt ?? di;
observedObject = selectProperty(getItInstance<T>(instanceName: instanceName));
Expand Down Expand Up @@ -147,7 +147,7 @@ R watchValue<T extends Object, R>(ValueListenable<R> Function(T) selectProperty,
R watchPropertyValue<T extends Listenable, R>(R Function(T) selectProperty,
{T? target, String? instanceName, GetIt? getIt}) {
assert(_activeWatchItState != null,
'watchIt can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
'watchPropertyValue can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
late final T observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -190,6 +190,8 @@ AsyncSnapshot<R> watchStream<T extends Object, R>(
String? instanceName,
GetIt? getIt,
}) {
assert(_activeWatchItState != null,
'watchStream can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
Stream<R>? observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -240,6 +242,8 @@ AsyncSnapshot<R?> watchFuture<T extends Object, R>(
bool preserveState = true,
GetIt? getIt,
}) {
assert(_activeWatchItState != null,
'watchFuture can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
Future<R>? observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -289,6 +293,8 @@ void registerHandler<T extends Object, R>({
String? instanceName,
GetIt? getIt,
}) {
assert(_activeWatchItState != null,
'registerHandler can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
Listenable? observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -334,6 +340,8 @@ void registerStreamHandler<T extends Object, R>({
String? instanceName,
GetIt? getIt,
}) {
assert(_activeWatchItState != null,
'registerStreamHandler can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
Stream<R>? observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -383,6 +391,8 @@ void registerFutureHandler<T extends Object, R>({
String? instanceName,
GetIt? getIt,
}) {
assert(_activeWatchItState != null,
'registerFutureHandler can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
Future<R>? observedObject;

final getItInstance = getIt ?? di;
Expand Down Expand Up @@ -411,24 +421,30 @@ void registerFutureHandler<T extends Object, R>({
/// returned `true` within [timeout].
/// It will trigger a rebuild if this state changes
bool allReady(
{void Function(BuildContext context)? onReady,
void Function(BuildContext context, Object? error)? onError,
Duration? timeout}) =>
_activeWatchItState!
.allReady(onReady: onReady, onError: onError, timeout: timeout);
{void Function(BuildContext context)? onReady,
void Function(BuildContext context, Object? error)? onError,
Duration? timeout}) {
assert(_activeWatchItState != null,
'allReady can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
return _activeWatchItState!
.allReady(onReady: onReady, onError: onError, timeout: timeout);
}

/// registers a handler that is called when the all-ready state is reached
/// it does not trigger a rebuild like [allReady] does.
/// You can force a timeout Exception if [allReady] has completed
/// within [timeout] which will call [onError]
void allReadyHandler(void Function(BuildContext context)? onReady,
{void Function(BuildContext context, Object? error)? onError,
Duration? timeout}) =>
_activeWatchItState!.allReady(
onReady: onReady,
onError: onError,
timeout: timeout,
shouldRebuild: false);
{void Function(BuildContext context, Object? error)? onError,
Duration? timeout}) {
assert(_activeWatchItState != null,
'allReadyHandler can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
_activeWatchItState!.allReady(
onReady: onReady,
onError: onError,
timeout: timeout,
shouldRebuild: false);
}

/// returns `true` if the registered async or dependent object defined by [T] and
/// [instanceName] is ready
Expand All @@ -437,15 +453,18 @@ void allReadyHandler(void Function(BuildContext context)? onReady,
/// returned `true` within [timeout].
/// It will trigger a rebuild if this state changes.
bool isReady<T extends Object>(
{void Function(BuildContext context)? onReady,
void Function(BuildContext context, Object? error)? onError,
Duration? timeout,
String? instanceName}) =>
_activeWatchItState!.isReady<T>(
instanceName: instanceName,
onReady: onReady,
onError: onError,
timeout: timeout);
{void Function(BuildContext context)? onReady,
void Function(BuildContext context, Object? error)? onError,
Duration? timeout,
String? instanceName}) {
assert(_activeWatchItState != null,
'isReady can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
return _activeWatchItState!.isReady<T>(
instanceName: instanceName,
onReady: onReady,
onError: onError,
timeout: timeout);
}

/// Pushes a new GetIt-Scope. After pushing, it executes [init] where you can register
/// objects that should only exist as long as this scope exists.
Expand All @@ -456,10 +475,17 @@ bool isReady<T extends Object>(
/// an async disposal function, that function won't be awaited.
/// I would recommend doing pushing and popping from your business layer but sometimes
/// this might come in handy.
void pushScope({void Function(GetIt getIt)? init, void Function()? dispose}) =>
_activeWatchItState!.pushScope(init: init, dispose: dispose);
void pushScope({void Function(GetIt getIt)? init, void Function()? dispose}) {
assert(_activeWatchItState != null,
'pushScope can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
_activeWatchItState!.pushScope(init: init, dispose: dispose);
}

/// Will triger a rebuild of the Widget if any new GetIt-Scope is pushed or popped.
/// This function will return `true` if the change was a push otherwise `false`.
/// If no change has happened then the return value will be null.
bool? rebuildOnScopeChanges() => _activeWatchItState!.rebuildOnScopeChanges();
bool? rebuildOnScopeChanges() {
assert(_activeWatchItState != null,
'rebuildOnScopeChanges can only be called inside a build function within a WatchingWidget or a widget using the WhatchItMixin');
return _activeWatchItState!.rebuildOnScopeChanges();
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: watch_it
description: The simple state management powered by get_it. It allows to observe changes of objects inside the get_it service locator and rebuild the UI accordingly.
version: 1.0.3
version: 1.0.4
homepage: https://github.com/escamoteur/watch_it
funding:
- https://github.com/sponsors/escamoteur/
Expand Down

0 comments on commit b2b63dc

Please sign in to comment.