felangel / felangel/fresh

[fresh_graphql] Link for Subscription

Abierto
#50 4 comentarios 0 reacciones 1 asignado Reclamado por @felangel Ver en GitHub
enhancement
Lenguaje dominante
Dart
Estrellas
428
Forks
60
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Great idea this package and exactly what I was looking for. 🙇
Based on this idea it would be possible to create a similar link implementation for subscription.

What do you think?

This is my current code that could be replaced
```
class WSLink extends Link {
/// Creates a new [WebSocketLink] instance with the specified config.
WSLink(
this.url, {
this.headers = const {},
this.getToken,
});

final String url;
final Map headers;
final Future? getToken;

// cannot be final because we're changing the instance upon a header change.
SocketClient? _socketClient;

@override
Stream request(Request request, [forward]) async* {
// Get Token or Refresh
if (getToken != null) {
var token = await getToken;
headers['Authorization'] = 'Bearer $token';
}

if (_socketClient == null) {
connectOrReconnect();
}

yield* _socketClient!.subscribe(request, true);
}

/// Connects or reconnects to the server with the specified headers.
void connectOrReconnect() {
_socketClient?.dispose();
_socketClient = SocketClient(
url,
config: SocketClientConfig(
inactivityTimeout: Duration(seconds: 30),
delayBetweenReconnectionAttempts: const Duration(seconds: 2),
autoReconnect: true,
connect: (url, protocols) => IOWebSocketChannel.connect(
url,
protocols: protocols,
headers: headers,
),
),
);
}

/// Disposes the underlying socket client explicitly. Only use this, if you want to disconnect from
/// the current server in favour of another one. If that's the case, create a new [WebSocketLink] instance.
Future dispose() async {
await _socketClient?.dispose();
_socketClient = null;
}
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.