subscribeMulti method
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<ProEventObserver?> subscribeMulti(List<String> subs) async {
emit('subscribing', subs);
var s = await statusUpdatesGet(subs);
if (s == null) return null;
// we listen to the events and `emit` them as they come in
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);
});
// this is called when the http client is finished sending data
streamListener.onDone(() {
for (var endpoint in subs) {
emit('unsubscribed', endpoint);
}
streamListener.cancel();
updateListeners.removeWhere((key, value) => value == streamListener);
});
for (var endpoint in subs) {
// is this needed?
// cancel all previous streamListeners on these endpoints
await updateListeners[endpoint]?.cancel();
updateListeners[endpoint] = streamListener;
emit('subscribed', endpoint);
}
return ProEventObserver(() => streamListener.cancel());
}