Skip to content
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

Way to remove a callback using Watcher.attach + ChangeNotifier #9

Open
PackRuble opened this issue Dec 16, 2023 · 0 comments
Open

Way to remove a callback using Watcher.attach + ChangeNotifier #9

PackRuble opened this issue Dec 16, 2023 · 0 comments
Labels
discussion There's a lot to discuss enhancement New feature or request
Milestone

Comments

@PackRuble
Copy link
Owner

I propose to discuss what the functionality should look like to allow convenient use of Cardoteka with the Watcher mixin to attach a callback to a ChangeNotifier object.

I'm considering this kind of hagfish for now:

mixin NotifierDetacher on ChangeNotifier {
  List<VoidCallback>? _onDisposeCallbacks;

  void onDispose(void Function() cb) {
    _onDisposeCallbacks ??= [];
    _onDisposeCallbacks!.add(cb);
  }

  @override
  void dispose() {
    _onDisposeCallbacks?.forEach((cb) => cb.call());
    _onDisposeCallbacks = null;

    super.dispose();
  }
}

This will allow you to do this in the future:

class OrderNotifier with ChangeNotifier, NotifierDetacher {
  final _orders = <String>[];

  void addOrder(String value) {
    _orders.add(value);
    notifyListeners();
    print('New order: $value');
  }
}

And then attach the wiretap like this:

void main() {
  // ignore_for_file: definitely_unassigned_late_local_variable
  // to do: initialize
  late CardotekaImpl cardoteka;
  late Card<String> lastOrderCard;

  final notifier = OrderNotifier();
  cardoteka.attach(
     lastOrderCard,
     notifier.addOrder, // <- nice bonus :)
     detacher: notifier.onDispose, // <- THIS
  );

  cardoteka.set(lastOrderCard, '#341');
  // 1. a value was saved to storage
  // 2. console-> New order: #341
}

Please, semantics, logic, and naming are both important. Share your thoughts :) 🙏

@PackRuble PackRuble added enhancement New feature or request discussion There's a lot to discuss labels Dec 16, 2023
@PackRuble PackRuble added this to the v2.0.0 milestone Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion There's a lot to discuss enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant