subscribeMulti method

Future<bool> subscribeMulti(
  1. List<String> subs
)

will subscribe to chunked updates using the /status endpoint.

To make things easier, the available and working endpoints are stored as static constants in the ProApiSubbable class.

Note: in 7.9, these endpoints do not work in this call despite the claims in the documentation:

  • playlist/current
  • announcement/active/timeline

Note also: Other endpoints are supported but they require id values. See the static class methods on ProApiSubbable for endpoint generators.

Implementation

Future<bool> subscribeMulti(List<String> subs) async {
  emit('subscribing', subs);

  var s = await api.statusUpdatesGet(subs);
  if (s == null) return false;

  var streamListener = s.listen((obj) {
    // emit the raw data identified by the endpoint/url
    var url = obj['url'] as String;
    emit(url, obj);

    // parse into the ProState
    state.handleData(obj);
  });

  streamListener.onDone(() {
    for (var endpoint in subs) {
      emit('unsubscribed', endpoint);
    }
    updateListeners.removeWhere((key, value) => value == streamListener);
  });

  for (var endpoint in subs) {
    // cancel all previous streamListeners on this endpoint
    await updateListeners[endpoint]?.cancel();
    updateListeners[endpoint] = streamListener;
    emit('subscribed', endpoint);
  }
  return true;
}