28
As true as steel to your desire You come with just an idea - we make great software for you! Trust Teamwork Transparency Isomorphic Javascript: new silver bullet Vitaliy Medvedev, Arcadia

Isomorphic JS - new silver bullet

  • Upload
    imevs

  • View
    293

  • Download
    2

Embed Size (px)

DESCRIPTION

Isomorphism principle involves the use of a single code on the server and the client side of the application. The main advantages of the approach are reducing duplication, improving the quality of code, as well as accelerating the development process.

Citation preview

Page 1: Isomorphic JS - new silver bullet

As true as steel to your desire

You come with just an idea -we make great software for you!

TrustTeamwork

Transparency

Isomorphic Javascript: new silver bullet

Vitaliy Medvedev, Arcadia

Page 2: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

About me

Vitaliy Medvedev

Leading Software Engineer at JSC “Arcadia Inc.”

5+ years of experience in software development

[email protected]

Page 3: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Where JS works

● client● server● desktop● mobile● quadcopters

Page 4: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Single page application (SPA)

JSON(AJAX/WebSockets)

HTML/CSS/JS PHP/C#/Java/NodeJs

Frontend (MVC)

AnimationValidation

TemplatingRouting

Backend (MVC)

PersistenceAuthentication

Heavy calculations

Page 5: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

SPA problems

● loading time● search engine indexing● logic duplication

o different languages - different ecosystemso cross-functional restictions

Page 6: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Page 7: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

1. Solution!

Page 8: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

What to do?

1. Write in the same language on both sides

Page 9: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

What to do?

1. Write in the same language on both sides2. Use same code on both sides

Page 10: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Isomorphic? WAT?!

● cross-platform

● multi-platform

● heteromorphic

● ....

Page 11: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Who invented isomorphism?

History:1. 18 Oct 2011

Scaling Isomorphic Javascript Code

2. 01 Apr 2012Yahoo! Mojito Framework

3. 08 Nov 2013The future of web apps is — ready? — isomorphic JavaScript

Page 12: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

2. Let’s do isomorphism!

Page 13: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Using scripts

var module = require(‘./jquery’);

<script src="script.js" [async] [defer]></script>

require([‘jquery’], function ($) {// do smth

})

Page 14: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Differences between client & server JS

● Global objectsglobal / window

● Different available apilocalStorage, historyApi, WebGL, Canvas, FileSystem

● Differences in API behaviorcookies, websockets, webworkers

Page 15: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Differences between client & server JS

● Environment

● Script loading

● Available api

● Engine version

Page 16: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Diving into isomorphism

● Understand that you can write as isomorphic● Code kinds

o Code that should not work at frontend side

o Code that should not work at backend side

o Independent of enviroment▪ Write isomorphic unit tests

▪ Perform static analysis

o Dependent of enviroment▪ Create abstract layer

Page 17: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Diving into isomorphism

● Abstraction from enviromentbrowserify, typescript

● Enviroment specific unit testskarma, mocha, phantomjs

● Static code analisysjslint, jshint, eslint, tslint

Page 18: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Code before isomorphism

Page 19: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Simple isomorphic module

(function (exports) {exports.isomorphicSum = function (a, b) {

return a + b;};

})(typeof exports ? exports : window)

Page 20: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

More usefull example

http://jsfiddle.net/7o2mrby9/

Page 21: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

// NodeJSvar setCookie = require(‘./isomorphicModule’), http = require(‘http’);http.createServer(function (req, response) {

setCookie(‘name’, ‘value’, { dest: response });}).listen(1337, '127.0.0.1');

// Browser:window.setCookie(‘name’, ‘value’);

More usefull example

Page 22: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

What is isomorphic code good for?

1. Templating (handlebarsjs)2. Business logic:

o validationo filteringo calculations

3. Using common libraries (jquery, undescore)

Page 23: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Isomorphic libraries

● jquery● undescore● backbonejs● lodash● cryptojs● handlebarsjs● async

Page 24: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

3. What’s next?

Page 25: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Full stack javascript frameworks

Page 26: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Profit

✓ decrease loading time✓ search engine indexing✓ decrease logic duplication✓ better code design✓ code consistency

Page 28: Isomorphic JS - new silver bullet

© Copyright JSC “Arcadia, Inc.”

Q & A