Skip to content

Commit

Permalink
fix: Add name parameter to cache and watch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Nov 10, 2022
1 parent 5a9080a commit 61ef962
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/katana_scoped/example/pubspec.lock
Expand Up @@ -234,7 +234,7 @@ packages:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
graphs:
dependency: transitive
description:
Expand Down Expand Up @@ -311,7 +311,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.1"
version: "1.2.2"
lints:
dependency: transitive
description:
Expand Down
6 changes: 6 additions & 0 deletions packages/katana_scoped/lib/value/cache.dart
Expand Up @@ -9,16 +9,22 @@ extension RefCacheExtensions on Ref {
///
/// If [keys] is passed a value different from the previous value, [callback] is executed again and the value is updated.
///
/// If [name] is specified, it is saved as a separate type. If [keys] is changed, the previous state is discarded, but if [name] is changed, it is kept as a separate state.
///
/// [callback]で返される値をキャッシュして保存します。
///
/// [keys]が前の値と違う値が渡された場合、再度[callback]が実行され値が更新されます。
///
/// [name]を指定すると別のタイプとして保存されます。[keys]を変えた場合は以前の状態は破棄されますが、[name]を変えた場合は別々の状態として保持されます。
T cache<T>(
T Function() callback, {
List<Object> keys = const [],
String? name,
}) {
return getScopedValue<T, _CacheValue<T>>(
() => _CacheValue<T>(callback: callback, keys: keys),
listen: false,
name: name,
);
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/katana_scoped/lib/value/watch.dart
Expand Up @@ -11,18 +11,24 @@ extension RefWatchExtensions on Ref {
///
/// If [keys] is different from the previous value, [callback] is executed again and the new [ChangeNotifier] is saved.
///
/// If [name] is specified, it is saved as a separate type. If [keys] is changed, the previous state is discarded, but if [name] is changed, it is kept as a separate state.
///
/// [ChangeNotifier]を返す[callback]を渡すとそれを監視することができます。
///
/// [ChangeNotifier.notifyListeners]が実行されると更新が通知され、関連するウィジェットが再描画されます。
///
/// [keys]が前の値と違う場合再度[callback]が実行され、新しい[ChangeNotifier]が保存されます。
///
/// [name]を指定すると別のタイプとして保存されます。[keys]を変えた場合は以前の状態は破棄されますが、[name]を変えた場合は別々の状態として保持されます。
T watch<T extends Listenable>(
T Function() callback, {
List<Object> keys = const [],
String? name,
}) {
return getScopedValue<T, _WatchValue<T>>(
() => _WatchValue<T>(callback: callback, keys: keys),
listen: true,
name: name,
);
}
}
Expand Down

0 comments on commit 61ef962

Please sign in to comment.