Skip to content

feat: DependencyState.onViewModel for VM-backed dependencies (v2.18.0) - #14

Merged
JhonaCodes merged 2 commits into
mainfrom
feat/dependency-on-view-model
Jul 20, 2026
Merged

feat: DependencyState.onViewModel for VM-backed dependencies (v2.18.0)#14
JhonaCodes merged 2 commits into
mainfrom
feat/dependency-on-view-model

Conversation

@JhonaCodes

Copy link
Copy Markdown
Owner

What

Adds change.onViewModel<VM, T>() to DependencyState, so a ViewModel can declare a dependency on a ViewModel-backed service (ReactiveNotifierViewModel<VM, T>) directly — instead of passing the raw ReactiveNotifier container as change.on<T>() requires today.

@override
void onDependenciesStateChanged(DependencyState change) {
  change.onViewModel<ProfileViewModel, ProfileModel>(
    ProfileService.state,
    (previous, current) {
      if (previous.id != current.id) _refreshFor(current.id);
    },
  );
}

Why

change.on<T>() only accepts a raw ReactiveNotifier. Passing a VM-backed service's container did not react to the inner ViewModel's updateState — a ReactiveNotifier<VM> container does not notify when the inner VM mutates. This makes cross-VM dependencies work ergonomically and correctly.

Changes

  • DependencyState.onViewModel<VM, T>() — delegates to on<T>(service.reactiveNotifier, cb) with typed (previous, current).
  • ReactiveNotifierViewModel.reactiveNotifier getter — exposes the underlying container.
  • Dependency subscriptions (ViewModel and AsyncViewModelImpl) now listen to the inner ViewModel (ChangeNotifier) target, so reactions fire on updateState. Simple-type notifiers behave exactly as before.
  • README section + CHANGELOG; version bumped to 2.18.0 (additive, minor).

Tests

New group in test/dependency_state_test.dart:

  • setup snapshots the inner ViewModel data via a ReactiveNotifierViewModel
  • reacts when the inner ViewModel mutates via updateState
  • onViewModel equivalent to on(service.reactiveNotifier)

All 21 dependency tests pass. Full suite: only pre-existing golden env-mismatch failures remain (verified identical on clean main); no non-golden regressions.

Scope

Deliberately isolated to onViewModel. Does not include any of the ReactiveInspector WIP.

Add `change.onViewModel<VM, T>()` so a ViewModel can declare a dependency
on a `ReactiveNotifierViewModel<VM, T>` service directly, instead of the raw
`ReactiveNotifier` container. Reactions now fire when the inner ViewModel
mutates via `updateState`, not only when the container instance is replaced.

- DependencyState.onViewModel<VM, T>() delegating to on<T>()
- ReactiveNotifierViewModel.reactiveNotifier getter exposes the container
- dependency subscriptions listen to the inner VM (ChangeNotifier) target
- tests for setup snapshot + reaction on inner-VM updateState
- docs (README) + CHANGELOG; version 2.18.0
@JhonaCodes
JhonaCodes merged commit bfe38e8 into main Jul 20, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant