defaultIsolateHandler function

Future<void> defaultIsolateHandler(
  1. SendPort sendPort
)

This is the default handler function used by PasswordScoringIsolateHandler.

It will initialize the zxcvbn with the default options and then wait for Locale or String messages. When it receives a Locale message it will update the Options of zxcvbn and when it receives a ScoringRequest message it will calculate the score and send the Result message back.

Implementation

@pragma('vm:entry-point')
Future<void> defaultIsolateHandler(SendPort sendPort) async {
  ReceivePort receivePort = ReceivePort();

  // Always send the [SendPort] as first message
  sendPort.send(receivePort.sendPort);

  // Default handler only supports Common language package
  final langCommon = LanguageCommon();

  zxcvbn.setOptions(Options(
    dictionary: Dictionary.merge([
      langCommon.dictionary,
    ]),
    graphs: langCommon.adjacencyGraphs,
    translations: langCommon.translations,
  ));

  // Used to refresh response when locale changes
  ScoringRequest? lastRequest;

  await for (var message in receivePort) {
    if (message is Locale) {
      // Default handler doesn't support switching locales
    } else if (message is ScoringRequest) {
      lastRequest = message;
    }

    if (lastRequest != null) {
      final Result result = zxcvbn(
        lastRequest.password,
        options: lastRequest.options,
        userInputs: lastRequest.userInputs,
      );
      sendPort.send(result);
    }
  }
}