evaluate method

Future<Map> evaluate(
  1. String data
)

Implementation

Future<Map> evaluate(String data) async {
  this.data = data.trim();
  initialize();

  const timeoutDuration = Duration(seconds: 10); // Adjust timeout as needed
  final id = _generateUniqueId(); // Generate a unique ID

  try {
    var result = {};
    bool found = false;

    await Future.any([
      // This future handles waiting for the controller and executing JavaScript
      () async {
        // Wait until the controller is initialized
        while (!webViewCreated.isCompleted) {
          await Future.delayed(const Duration(milliseconds: 100));
        }

        // Execute the JavaScript with the generated ID
        await controller!.evaluateJavascript(source: "query($id, '${jql.replaceAll("'", '"')}')");

        // Wait for the result
        while (!found) {
          for (var element in results) {
            if (element.containsKey('id') && element['id'] == id) {
              result = {'value': element['value'], 'error': element['error']};
              found = true;
              results.remove(element);
              break;
            }
          }
          if (!found) {
            await Future.delayed(const Duration(milliseconds: 100));
          }
        }
      }(),

      Future.delayed(timeoutDuration).then((_) {
        if (!found) {
          throw TimeoutException('Query timed out');
        }
      }),
    ]);

    return result;
  } catch (e) {
    throw Exception('Error: $e');
  }
}