zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 622 forks source link

feat(graphql): Configurable option to deduplicate pollers #1451

Closed kvenn closed 3 months ago

kvenn commented 3 months ago

Problem

If you call startPolling 10 times for the same request, it'll start 10 pollers that are all doing the same thing.

Solution

Testing

    final obs5 = _client.friendRequests(_onMyFriendRequests);
    final obs10 = _client.friendRequests(_onMyFriendRequests);
    final obs3 = _client.friendRequests(_onMyFriendRequests);
    final obs15 = _client.friendRequests(_onMyFriendRequests);

    obs5.startPolling(const Duration(seconds: 5));
    obs10.startPolling(const Duration(seconds: 10));
    obs3.startPolling(const Duration(seconds: 3));
    obs15.startPolling(const Duration(seconds: 15));
    // Should settle at 3 seconds (fastest)

    // Wait 10 seconds to confirm pollers only every 3 seconds
    Future.delayed(const Duration(seconds: 7)).then((_) {
      print('------------------TEST------------------');
      obs3.stopPolling();
      // Duration should now be 5 seconds
    });
    // Confirm it's 5 (should see 3 or 4 of them)
    Future.delayed(const Duration(seconds: 20)).then((_) {
      print('------------------TEST------------------');
      obs5.close();
      // Duration should now be 10 seconds
    });