Dart

Unknown Hero

Евгений Гусев

Powered by

Зачем ... JS?

//get data from server
let comments = api.getComments();
      
/**
* @typedef Comment
* @type Object
* @property {Author} Name of the commenter
*/

/**
* Used to collect comments from server
* @public
* @returns {[Comment]} Function returns array
* of comments object
*/

function getComments(){
                

JavaScript

  • Недокументированный




var comments = api.getComments();

some_internal_storage.push(comments);
//...
comments.push({id:'separator'});

showComments(comments);
                

JavaScript

  • Недокументированный
  • Нестрогий



Dart
C#
Java
C++
SmallTalk

JavaScript

this

JavaScript

  • Недокументированный
  • Нестрогий
  • Непродуманный

JavaScript

  • Недокументированный
  • Нестрогий
  • Непродуманный
  • Небогатый

left-pad

Dart Features

Dart Features

  • Types
  • OOP
  • Rich framework
  • Analyzer
  • Tree Shaking
  • One entry point

Types

import 'dart:html';
import 'dart:convert';

class CommentsService {

  dynamic getComments() {
    return HttpRequest.request("api/comments",
                    method: "GET").then((result) {

      return JSON.decode(result.responseText);
    });
  }
}

class Comment {
  String description;
  String author;
  String avatarUrl;
  DateTime postedAt;
}

Future<List<Comment>> getComments() {
  return HttpRequest.request("api/comments",
    method: "GET").then((HttpRequest result) {

      var raw = JSON.decode(result.responseText);

      return raw.map((Map rawComment) =>
                 new Comment.fromMap(rawComment));
  });
}

//...
  .then((HttpRequest result) {
    /* some code */
  })
//...

//...
  .then((HttpRequest result) {
    /* some code */
  })
  .catchError(handle401Error,
     test: (e) => e is NotAuthenticatedException)
//...

//...
  .then((HttpRequest result) {
    /* some code */
  }).
  .catchError(handle401Error,
     test: (e) => e is NotAuthenticatedException)
  .whenComplete(closeSocketOrServer);
//...

Future<List<Comment>> getComments() {
  return HttpRequest.request("api/comments",
    method: "GET").then((HttpRequest result) {

      var raw = JSON.decode(result.responseText);
      return raw.map((Map rawComment) =>
         new Comment.fromMap(rawComment));
  }).catchError((Error e){
    print(e);
    throw e;
  });
}

Collections


List<Comment> sortAndGetTopComments
    (List<Comment> comments, {int count: 5}) {

    return comments
      ..sort((a, b) =>
        return a.postedAt.compareTo(b.postedAt)
      )
      ..sublist(0, count);
}

OOP


abstract class Entity {
  String author;
  String avatarUrl;
  DateTime postedAt;
}

                

class Comment extends Entity {
  String description;
}

                

abstract class Serializable {
    String serialize();
}

class Comment extends Entity implements Serializable {
  String description;

  String serialize() { //...
}

abstract class Entity {
    //...
    String toString();
}

class Comment extends Entity {
    //...
    @override
    String toString() { //...
}

class DateFormatter {
    String formatDate(DateTime d){
        var formatter = new DateFormat('yyyy-MM-dd hh:mm');
        return formatter.format(d);
    }
}

class Comment extends Entity with DateFormatter {
    //...
    @override
    String toString() {
        //...
        var stringedDate = formatDate(postedAt);
    }
}

Streams


abstract class CommentsService {

    final StreamController<Comment> _controller =
                new StreamController();
    Stream<Comment> get stream => _controller.stream;

    // some polling mechanism
    poll() {
        //...
        var comment = getComment();
        _controller.add(comment);
        //...
    }
}

service.stream.listen((Comment comment){
    //...
});
//and more!
.where(bool test(T event))
.map(convert(T event))
.expand(Iterable expand(T element)
.take(int count)
.skip(int count)
.takeWhile(bool test(T element))
.skipWhile(bool test(T element))
.transform(StreamTransformer streamTransformer)


vs

Функциональность

More Dart Features!

  • Transformers (two kinds)
  • Operator overloading
  • A native VM
  • Deferred libraries
Функциональность
Удобство
Строгость

Summary

- Follow me: @bunopus
- : @wriketeam
- Slides & info: https://goo.gl/THnu3U

Join the
side

Questions