registerExtensions method

void registerExtensions()

Registers the extension. Call once, before runApp.

Implementation

void registerExtensions() {
  GuestExtensions.register('ext.flutterware.watch', (_, args) async {
    // Absent means "leave it alone", which is what lets a caller change the
    // node without restating the interval and the other way round.
    if (args['on'] case var on?) {
      if (on == 'false') {
        unwatch();
      } else {
        watch(
          nodeId: switch (args['node']) {
            // The empty string is how a caller says "structure only" over a
            // wire whose values are all strings.
            null || '' => _nodeId,
            '-' => null,
            var id => id,
          },
          minInterval: Duration(
            milliseconds:
                int.tryParse(args['minIntervalMillis'] ?? '') ??
                _minInterval.inMilliseconds,
          ),
        );
      }
    }
    return developer.ServiceExtensionResponse.result(
      jsonEncode(describe().toJson()),
    );
  });
}