jodinathan / typings

Apache License 2.0
39 stars 9 forks source link

Problem transitioning from js_bindings to typings #4

Closed agbossi closed 1 year ago

agbossi commented 1 year ago

Hi, I apologize if this is not the place for this kind of issues, but there I found very little information. I have some code in a project that uses js_bindings. Today when working on this project after while I saw that my app doesn't build anymore and that the problem is related to js_bindings dependency, that's when I found it is discontinued, so I'm trying to adapt the code using typings, but I'm having some issues.

here is the most relevant part of the code along with the errors

import 'dart:async';
import 'dart:math';
import 'package:proyecto_final_amboage_piacentino/business/audio_processor.dart';
import '../../interfaces/microphone.dart';
import 'dart:typed_data';
import 'package:typings/core.dart';

class GptRecorder implements IMicrophone {
  //late final audioContext;
  final AudioContext _audioContext = AudioContext();
  late final MediaStream _mediaStream;
  late final MediaStreamAudioSourceNode _source;
  late final AudioWorkletNode _node;
  late final MediaStreamAudioDestinationNode _destNode;
  late final MediaRecorder _mediaRecorder;

  void _setupAudioGraph(MediaStream mediaStream) async {
    _source = _audioContext.createMediaStreamSource(mediaStream);
    _destNode = _audioContext.createMediaStreamDestination();
    await _audioContext.audioWorklet
        .addModule('lib/models/web_recorder/hello_processor.js')
        .catchError((error) {
      //print('Loading error: ' + error);
    });
    _node = AudioWorkletNode(_audioContext, 'pcm16-processor');
    _source.connect(_node);
    _node.connect(_destNode);
  }
  _onDataAvailable(Event event) async {
    if (event is BlobEvent && event.data.size > 0) {
      final reader = FileReader();
      reader.readAsArrayBuffer(event.data);

      reader.addEventListener('loadend', (event) {
        final arrayBuffer = reader.result;
        final uint8list = Uint8List.view(arrayBuffer);
        //final intensity = _getAudioIntensity(uint8list);
        if (uint8list.length > 1) {
          try {
            Int16List audio = AudioProcessor.decode(uint8list);
            _onData(audio);
          } on Exception catch (e) {
            print('fragment error');
            print(uint8list);
          }
        }
        // following will log the audio intensity for each chunk of data
        //print("intensity: " + intensity.toString());
      });
    }
  }
}

errors:

print(uint8list);
void print() Type: void Function() package:typings/src/d/core/lib_dom_d.dart

Too many positional arguments: 0 expected, but 1 found.

reader.addEventListener('loadend', (event) {...});

({void Function<K$ extends Event>(FileReaderEventMap<K$>, dynamic Function(K$), [Object?]) $1, void Function(String, EventListenerOrEventListenerObjectCommon, [Object?]) $2}) get addEventListener
package:typings/src/d/core/lib_dom_d.dart

Overload accessor: $1, $2

The expression doesn't evaluate to a function, so it can't be invoked

_source.connect(_node);
MediaStreamAudioSourceNode _source Type: MediaStreamAudioSourceNode

The expression doesn't evaluate to a function, so it can't be invoked.dart[invocation_of_non_function_expression

As I mentioned before, I couldn't find many examples, but from what I understand from the pub.dev documentation, I should be able to use these objects. What am I missing?

jodinathan commented 1 year ago

try to alias the typings import: import 'package:typings/core.dart' as ty; so it doesn't conflict with other libraries (including the SDK)

agbossi commented 1 year ago

That only solves the print error, the connect()and addEventListener() still throw the same error

jodinathan commented 1 year ago

@agbossi as Dart doesn't have method overloading, Typings recreates overloads as accessors:

addEventListener.$1()
addEventListener.$2()

Basically, change your code to use addEventListener.$1(...) or addEventListener.$2(...) with the same parameters that it should work.

The reasoning behind this is that it makes the bindings sound.

agbossi commented 1 year ago

oooh.. so that's how it should be used. Great now those errors are gone. I updated the relevant lines the following way:

_source.connect.$2(_node);

_mediaRecorder.addEventListener.$2('dataavailable',
        _onDataAvailable as ty.EventListenerOrEventListenerObject);

when trying to build, the following happens:

Launching lib/main.dart on Chrome in debug mode...
main.dart:1
Unhandled exception:
Unsupported operation: Undetermined nullability. 
Encountered while compiling file:///home/abossi/.pub-cache/hosted/pub.dev/typings-0.0.1+1/lib/src/d/core/lib_es2015_collection_d.dart, which contains the type: InterfaceType(Object%).
lib_es2015_collection_d.dart:1
#0      ProgramCompiler._typeCompilationError (package:dev_compiler/src/kernel/compiler.dart:3355:7)
#1      ProgramCompiler._undeterminedNullabilityError (package:dev_compiler/src/kernel/compiler.dart:3352:7)
#2      ProgramCompiler._emitInterfaceType (package:dev_compiler/src/kernel/compiler.dart:3277:7)
#3      ProgramCompiler.visitInterfaceType (package:dev_compiler/src/kernel/compiler.dart:3191:7)
#4      InterfaceType.accept (package:kernel/ast.dart:12100:42)
#5      MappedListIterable.elementAt (dart:_internal/iterable.dart:415:31)
#6      MappedListIterable.elementAt (dart:_internal/iterable.dart:415:40)
#7      ListIterator.moveNext (dart:_internal/iterable.dart:344:26)
#8      InstantiatorGeneratorVisitor.splayNodes (package:dev_compiler/src/js_ast/template.dart:224:23)
#9      InstantiatorGeneratorVisitor.handleCallOrNew.<anonymous closure> (package:dev_compiler/src/js_ast/template.dart:604:22)

#10     Template.instantiate (package:dev_compiler/src/js_ast/template.dart:118:26)
#11     JsBuilder.call (package:dev_compiler/src/js_ast/builder.dart:212:21)
#12     ProgramCompiler._emitGenericClassType (package:dev_compiler/src/kernel/compiler.dart:3393:15)
#13     ProgramCompiler._emitInterfaceType (package:dev_compiler/src/kernel/compiler.dart:3256:17)
#14     ProgramCompiler.visitInterfaceType (package:dev_compiler/src/kernel/compiler.dart:3191:7)
#15     InterfaceType.accept (package:kernel/ast.dart:12100:42)
#16     ProgramCompiler._emitType (package:dev_compiler/src/kernel/compiler.dart:3076:60)
#17     ProgramCompiler._emitArgumentList (package:dev_compiler/src/kernel/compiler.dart:6223:41)
#18     ProgramCompiler._emitMethodCall (package:dev_compiler/src/kernel/compiler.dart:5326:16)
#19     ProgramCompiler.visitInstanceInvocation (package:dev_compiler/src/kernel/compiler.dart:5240:12)
#20     InstanceInvocation.accept (package:kernel/ast.dart:6128:44)
#21     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#22     ProgramCompiler._emitMethodCall (package:dev_compiler/src/kernel/compiler.dart:5325:22)
#23     ProgramCompiler.visitInstanceInvocation (package:dev_compiler/src/kernel/compiler.dart:5240:12)
#24     InstanceInvocation.accept (package:kernel/ast.dart:6128:44)
#25     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#26     ProgramCompiler.visitConditionalExpression (package:dev_compiler/src/kernel/compiler.dart:6598:21)
#27     ConditionalExpression.accept (package:kernel/ast.dart:7420:44)
#28     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#29     ProgramCompiler.visitLet (package:dev_compiler/src/kernel/compiler.dart:6996:16)
#30     Let.accept (package:kernel/ast.dart:9277:44)
#31     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#32     ProgramCompiler.visitLet (package:dev_compiler/src/kernel/compiler.dart:6995:16)
#33     Let.accept (package:kernel/ast.dart:9277:44)
#34     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#35     MappedListIterable.elementAt (dart:_internal/iterable.dart:415:31)
#36     ListIterator.moveNext (dart:_internal/iterable.dart:344:26)
#37     new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#38     new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#39     new List.of (dart:core-patch/array_patch.dart:47:28)
#40     ListIterable.toList (dart:_internal/iterable.dart:214:7)

#41     ProgramCompiler._visitExpressionList (package:dev_compiler/src/kernel/compiler.dart:4195:40)
#42     ProgramCompiler.visitListLiteral (package:dev_compiler/src/kernel/compiler.dart:6833:20)
#43     ListLiteral.accept (package:kernel/ast.dart:8700:44)
#44     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#45     ProgramCompiler._emitArgumentList (package:dev_compiler/src/kernel/compiler.dart:6232:11)
#46     ProgramCompiler.visitStaticInvocation (package:dev_compiler/src/kernel/compiler.dart:6146:16)
#47     StaticInvocation.accept (package:kernel/ast.dart:7029:44)
#48     ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:4232:20)
#49     ProgramCompiler.visitReturnStatement (package:dev_compiler/src/kernel/compiler.dart:4794:45)
#50     ReturnStatement.accept (package:kernel/ast.dart:10786:43)
#51     ProgramCompiler._visitStatement (package:dev_compiler/src/kernel/compiler.dart:4141:20)
#52     ProgramCompiler._emitFunctionScopedBody (package:dev_compiler/src/kernel/compiler.dart:4174:18)
#53     ProgramCompiler._emitSyncFunctionBody.<anonymous closure> (package:dev_compiler/src/kernel/compiler.dart:3909:17)
#54     ProgramCompiler._withLetScope (package:dev_compiler/src/kernel/compiler.dart:2593:25)
#55     ProgramCompiler._withCurrentFunction (package:dev_compiler/src/kernel/compiler.dart:3943:18)
#56     ProgramCompiler._emitSyncFunctionBody (package:dev_compiler/src/kernel/compiler.dart:3905:17)
#57     ProgramCompiler._emitFactoryConstructor (package:dev_compiler/src/kernel/compiler.dart:2310:18)

#58     ProgramCompiler._emitJSInteropClassNonExternalMembers (package:dev_compiler/src/kernel/compiler.dart:919:23)
#59     ProgramCompiler._emitClass (package:dev_compiler/src/kernel/compiler.dart:734:31)

#60     List.forEach (dart:core-patch/growable_array.dart:416:8)
#61     ProgramCompiler._emitLibrary (package:dev_compiler/src/kernel/compiler.dart:670:23)
#62     List.forEach (dart:core-patch/growable_array.dart:416:8)
#63     ProgramCompiler.emitModule (package:dev_compiler/src/kernel/compiler.dart:479:15)
#64     IncrementalJavaScriptBundler.compile (package:frontend_server/src/javascript_bundle.dart:217:33)
#65     FrontendCompiler.writeJavaScriptBundle (package:frontend_server/frontend_server.dart:778:46)

<asynchronous suspension>
#66     FrontendCompiler.compile (package:frontend_server/frontend_server.dart:641:9)

<asynchronous suspension>
#67     listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1308:11)
<asynchronous suspension>

the Dart compiler exited unexpectedly.
Exited (sigterm)
Failed to compile application.

Any ideas about what could be going on?

jodinathan commented 1 year ago

it seems to be the same error here. It should work in release mode.
What is your Dart version? Are you using Flutter?

agbossi commented 1 year ago

Yes Flutter SDK 3.10.6 Dart SDK 3.0.6 (Flutter) I'll try the release mode and update

agbossi commented 1 year ago

You are right, it works in release mode. Thank you Out of curiosity, do you think this will be solved in a near future?

jodinathan commented 1 year ago

yes because the Dart team has been working hard with JS web WASM interop.
Also it helps thumbing up that issue so it can get more attention

agbossi commented 1 year ago

Will do, thank you for everything