felixblaschke / shelf_plus

MIT License
73 stars 14 forks source link

`onShelfRunContextClosing` or `onServerClosing` Implementation for `ShelfRunContext` is required. #29

Closed omegaui closed 1 year ago

omegaui commented 1 year ago

An implementation is necessary for listening to server close event. Suppose, if this is the case,

import 'dart:convert';
import 'dart:io';

import 'package:chat_desk/core/io/logger.dart';
import 'package:chat_desk/core/server/server_events.dart';
import 'package:shelf_plus/shelf_plus.dart';
import 'package:http/http.dart' as http;

late ShelfRunContext serverContext;

var host = "127.0.0.1";
var port = 8080;

void main() async {
  serverContext = await shelfRun(
    _initServer,
    defaultBindAddress: host,
    defaultBindPort: port,
    onStartFailed: (e) =>
        streamLog(initError, "Cannot Launch Server at this address!"),
    onStarted: (address, portNumber) {
      streamLog(initSuccess, "Server Started Successfully! $address");
    },
  );
  serverContext.onServerClosing = () => print("Server Closed");
}

There could be many situations at client side to close the context when time arises. Now, if we already have a listener listening to closing event, it would be easy to notify all clients in session from a single code, actually, this is like a extra add-on but will increase code maintainability when usiing this package.

felixblaschke commented 1 year ago

Hi. Sure why not, we can add a "onWillClose" and a "'onClosed". If you like you can create MR for that.

omegaui commented 1 year ago

@felixblaschke Hi! I was about to implement this new feature right now, but I'm unable to understand what do you mean by onWillClose callback, I mean where I should place it.

omegaui commented 1 year ago

Does it mean a callback before closing the context?