js_mimicry 2.0.0+2

  • Readme
  • Changelog
  • Installing
  • 34

JsMimicry #

Allows the use of Dart classes and objects in javascript. Generates a special proxy classes.

Create javascript API for Dart become easier.

Support:

  • Class inheritance ( with @JsProxy )
  • Named constructors
  • Method
  • Optional positional parameters
  • Optional named parameters
  • Input parameter transform
  • Result mutation
  • Future (via result mutator)
  • Field
  • Getter/Setter

Not support:

  • Factory
  • Operator

Try It Now #

Add the js_mimicry package to your pubspec.yaml file:

dependencies:
  js_mimicry: ">=0.2.0 <0.3.0"

Building and Deploying #

To build a deployable version of your app, add the js_mimicry transformers to your pubspec.yaml file:

transformers:
- js_mimicry

Sample #

##Dart code

    class Test1{
      method1(p1,[p2]){/* code */}
      method2(p1,p2){/* code */}
    }
    
    class Test2 extends Test1{
      int method2(p1,p2){/* new logic */}
      String method3(Test1 obj){/* code */}
      Future<int> method4(){/* code */}
      Test1 method5({namedP1, namedP2}){/* code */}
      String method6(int value){/* code */}
    }

##Add annotation for class

    @JsProxy()
    class Test1{  // ... cut ...
    }

    @JsProxy()
    class Test2 extends Test1{ // ... cut ...
    }

##Add annotation for transform input parameter

    String method6(@JsTransform(ANY_TO_INT) int value){/* code */}

    static int ANY_TO_INT(Object v){
        if (v is String){
         return int.parse(v);
        }
        return v as int;
    }

##Add annotation for mutation result Future

    @JsMutator(insertParams:const ["resultCb","errorCb"],result:Test2.futureToCallbacks)
    Future<int> method4(){/* code */}
    
    static futureToCallbacks(Future result,js.JsFunction resultCb,[js.JsFunction errorCb]){
        if (errorCb!=null){
            result = result.catchError((err)=>errorCb.apply([err]));
        }
        result.then((o)=>resultCb.apply([o]));
        return result;
    }

##Add annotation for mutation result

    @JsMutator(result:ANY_TO_STRING)
    Test1 method5(){/* code */}

    static ANY_TO_STRING(v)=>v.toString();

Import to javascript #

    import 'dart:js' as js;
    import 'package:js_mimicry/annotation.dart';
    main(){
      // Create instance Test1
      js.context["dartInstanceTest1"] = JsProxyFactory.toJs(new Test1());
    }

Uses in javascript #

    // Create Test2 instance, call method5 with named parameters
    new Test2().method5({namedP1:"123"});
    // call Test1.method1 with optional parameters
    dartInstanceTest1.method1("1");

JsProxyFactory methods #

###JsObject toJs(DartClass obj) Create proxy object for Test2 object. ###DartClass toDart(Type dartType, JsObject obj) Convert javascript proxy to real Dart object.

Changelog #

2.0.0 #

  • Dart 2.0

1.0.1 #

  • Dart 1.24, strong mode workaround

1.0.0 #

  • upgrade code_transformers: ^0.5.0

0.2.3 #

  • disable js_mimicry transformer

0.2.2 #

  • useSharedSources mode enabled in the resolver
  • fix analyzer: '>=0.23.0 <0.26.1+15'

0.2.1+3 #

  • reduced memory consumption

0.2.1+2 #

  • upgrade analyzer: '>=0.23.0 <0.27.0'

0.2.1+1 #

  • Fix: JsTransform for optional and named param

0.2.1 #

  • Support JsIgnore

0.2.0+4 #

  • Support abstract class

0.2.0+3 #

  • Added tests
  • Fix: source shift
  • Fix: skip not jsProxy parent

0.2.0 #

  • Initial transformer release.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  js_mimicry: ^2.0.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:js_mimicry/annotation.dart';
import 'package:js_mimicry/generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
56
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
13
Overall:
Weighted score of the above. [more]
34
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Fix lib/src/mimicry_resolver.dart. (-25 points)

Analysis of lib/src/mimicry_resolver.dart failed with 1 error:

line 7 col 7: Missing concrete implementation of 'Resolver.assetIdForElement'.

Fix lib/src/dart_class_info.dart. (-9.08 points)

Analysis of lib/src/dart_class_info.dart reported 19 hints, including:

line 19 col 19: Unnecessary new keyword.

line 48 col 26: Unnecessary new keyword.

line 59 col 7: Don't type annotate initializing formals.

line 62 col 27: Use isNotEmpty instead of length

line 74 col 48: 'element' is deprecated and shouldn't be used.

Fix lib/src/dart_method_info.dart. (-6.78 points)

Analysis of lib/src/dart_method_info.dart reported 14 hints, including:

line 45 col 29: Use isNotEmpty instead of length

line 49 col 27: Unnecessary new keyword.

line 54 col 12: Unnecessary new keyword.

line 55 col 78: 'kind' is deprecated and shouldn't be used.

line 67 col 24: Don't type annotate initializing formals.

Fix additional 8 files with analysis or formatting issues. (-18.70 points)

Additional issues in the following files:

  • lib/src/generator_js_mimicry.dart (13 hints)
  • lib/annotation.dart (7 hints)
  • lib/generator.dart (5 hints)
  • lib/src/dart_class_visitor.dart (4 hints)
  • lib/src/dart_method_mutator.dart (3 hints)
  • lib/src/dart_property_info.dart (3 hints)
  • lib/src/type_provider_helper.dart (3 hints)
  • lib/src/metadata.dart (Run dartfmt to format lib/src/metadata.dart.)

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (build_resolvers).

Package is getting outdated. (-47.40 points)

The package was last published 76 weeks ago.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and js_mimicry.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
build ^1.0.0 1.1.0 1.2.2
build_resolvers ^0.2.1 0.2.3 1.3.4
build_runner ^1.0.0 1.2.3 1.8.1
build_test >=0.10.0 <2.0.0 0.10.6 1.0.0
path >=1.3.5 <2.0.0 1.6.4
Transitive dependencies
analyzer 0.34.3 0.39.6
args 1.6.0
async 2.4.1
boolean_selector 1.0.5 2.0.0
build_config 0.3.2 0.4.2
build_daemon 0.2.3 2.1.4
build_runner_core 2.0.1 5.0.0
built_collection 4.3.2
built_value 6.7.1 7.0.9
charcode 1.1.3
checked_yaml 1.0.2
code_builder 3.2.0 3.2.1
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.14.6+1 0.16.1
dart_style 1.2.3 1.3.4
fixnum 0.10.11
front_end 0.1.9+1 0.1.29
glob 1.2.0
graphs 0.2.0
html 0.14.0 0.14.0+3
http 0.12.0+4
http_multi_server 2.2.0
http_parser 3.1.4
io 0.3.4
js 0.6.1+1
json_annotation 2.4.0 3.0.1
json_rpc_2 2.1.0
kernel 0.3.9+1 0.3.29
logging 0.11.4
matcher 0.12.3+1 0.12.6
meta 1.1.8
mime 0.9.6+3
multi_server_socket 1.0.2
node_interop 1.0.3
node_io 1.0.1+2
node_preamble 1.4.8
package_config 1.9.3
package_resolver 1.0.10
pedantic 1.9.0
plugin 0.2.0+3
pool 1.4.0
pub_semver 1.4.4
pubspec_parse 0.1.5
quiver 2.0.5 2.1.3
shelf 0.7.5
shelf_packages_handler 1.0.4 2.0.0
shelf_static 0.2.8
shelf_web_socket 0.2.3
source_map_stack_trace 1.1.5 2.0.0
source_maps 0.10.9
source_span 1.7.0
stack_trace 1.9.3
stream_channel 1.7.0 2.0.0
stream_transform 0.0.20 1.2.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.2 0.2.15
test_core 0.2.1+1 0.3.3
timing 0.1.1+2
typed_data 1.1.6
vm_service_client 0.2.6+3
watcher 0.9.7+14
web_socket_channel 1.1.0
yaml 2.2.0
Dev dependencies
test 1.3.0 1.5.3 1.14.2