even more java script best practices

40
Even more JavaScript best practices @GregWeng NCU 2015/10/23

Upload: chenghui-weng

Post on 20-Jan-2017

103 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Even more java script best practices

Even more JavaScriptbest practices

@GregWeng

NCU 2015/10/23

Page 2: Even more java script best practices

Today's lesson

Page 3: Even more java script best practices

Comment in JSDoc Style

Racing

Event queuing & Abortable Promise

ES6/7/8 & Babel

Homework review

Page 5: Even more java script best practices

Generate (pretty) API documents for human

Page 6: Even more java script best practices

If you follow its comment format

Page 7: Even more java script best practices

Demo site of Gaia System app

http://alivedise.github.io/gaia-system-jsdoc/

Page 8: Even more java script best practices

Good first bugs for adding JSDoc comment

https://bugzilla.mozilla.org/show_bug.cgi?id=1170465

It is a good way to learn contribution

Page 9: Even more java script best practices

Racing & Abortable Promise

Page 10: Even more java script best practices

Racing

SystemBooting

passcode.enabled

Screen LockShows up

reading Lock withPasscode

return value

Booting Logo

Screen LockInitializing

~ 3 seconds

Page 11: Even more java script best practices

passcode.enabled

Screen LockShows up

reading Lock withPasscode

return value

Screen LockInitializing

~ 3 seconds

Racing

SystemBooting Booting Logo

Racing

Page 12: Even more java script best practices

Whenever there is a "guessing" about some async operations...

Page 13: Even more java script best practices

A racing is held

Page 14: Even more java script best practices

And the solution is not trivial...

Page 15: Even more java script best practices

Bug 1173284100+ comments & 1 month

Page 16: Even more java script best practices

passcode.enabled

Screen LockShows up

reading Lock withPasscode

return value

Screen LockInitializing

~ 3 seconds

Solution

SystemBooting Booting Logo

Freeze the slider

Page 17: Even more java script best practices

Event queueing & Abortable Promise

Page 18: Even more java script best practices

Public interfaces of JavaScript program components

JavaScript components usually use handlers as public interfaces among the user, server and other components

like to draw or to fire new events

private helper methods...

event handlers

server gateway

local data keeper

component usernative events

serversync

componentscustom events

Page 19: Even more java script best practices

Or racing may occur when two events and their async operations are performing

handler bar

handler foo

"handleEvent"

component

Async event handlers need a queue

async helper

sync helper

handling foo

waiting to handle bar

waiting to handle foo

waiting to handle foo

waiting to handle bar

event comes when thereis one being handled

Page 20: Even more java script best practices

Promise as the queue

Page 21: Even more java script best practices

Every handler will be queued by concating them with the 'then' method

handler bar

handler foo

"handleEvent"

component

Promise as the queue

async helper

sync helper

event comes when thereis one being handled

this.mainPromise = this.mainPromise.then( this.handlerBar.bind(this));

handling foo

waiting to handle bar

waiting to handle foo

Page 22: Even more java script best practices

Promise is not enoughwhen events are prioritized

Page 23: Even more java script best practices

Must not be queued: handle it immediately and clear all queued steps

handler bar

handler foo

"handleEvent"

component

Prioritized events

async helper

sync helper

handling bar

waiting to handle bar

waiting to handle foo

waiting to handle bar

waiting to handle bar

Page 24: Even more java script best practices

How to clear the queued steps?

handler bar

handler foo

"handleEvent"

component

Prioritized events

async helper

sync helper

waiting to handle bar

handling foo

waiting to handle bar

waiting to handle bar

waiting to handle bar

Page 25: Even more java script best practices

Promise/A+ spec doesn't provide any method to abort it...

Page 26: Even more java script best practices

Need to create a "Process" to wrap the original Promise

Page 27: Even more java script best practices

Throw an "Interrupt" customized error when prioritized event comes, and capture it later to silent the error console

handler bar

handler foo

"handleEvent"

component

Prioritized events with "Process"

async helper

sync helper

waiting to handle bar

handling foo

waiting to handle bar

waiting to handle bar

waiting to handle bar

Error: Interrupt

Page 28: Even more java script best practices

ES6/7/8 & Babel

Page 29: Even more java script best practices

import Utils from 'js/utils'

export default class {

constructor(id) {

this.id = id

this.queue = Promise.resolve()

}

handleEvent(event) {

this.queue = this.queue.then(() => {

return this.onEvent[event.type].bind(this)

}).catch((err) => {

this.onErrorOrInterrupt(err)

})

}

}

Page 30: Even more java script best practices

EcmaScript transpilerBabel

Page 31: Even more java script best practices

https://babeljs.io/

Page 32: Even more java script best practices

In the current front-end worldES6+ is becoming the mainstream

Page 33: Even more java script best practices

Homework review

Page 34: Even more java script best practices

Please do not test expression only:

About the test

it('will test some pure functions', function() { assert(2 === (1 + 1)); });

it('will test some pure functions', function() { var manager = new Manager() assert(2 === manager.pureAdd(1, 1)); });

//....

Manager.prototype.pureAdd = function(a, b) { return a + b; };

Page 35: Even more java script best practices

It is not necessary to bind then call it immediately

About the 'bind'

handleEvent(event) { .... case 'foo': this.onEventFoo.bind(this)() }

window.addEventListener('foo', this); // Bind `this` to the handleEvent //.... handleEvent(event) { .... case 'foo': this.onEventFoo() // `this` of 'handleEvent' is already bound }

Page 36: Even more java script best practices

The `then` method will pass the argument, and bound function can still be called as normal function, so:

About the Promise + 'bind'

.then(function(data) { this.foo(data); }.bind(this));

.then(this.foo.bind(this));

Page 37: Even more java script best practices

Please give it a clear naming convention to distinguish constructor and instance

Naming convention is important

var foo = function() {};var f = new foo();//....

var Foo = function() {};var foo = new Foo();//....

Page 38: Even more java script best practices

You are in trouble if your PR show some red lines like this and you can't explain that

Page 39: Even more java script best practices

And please comment on PR or using commits to indicate what issue you're addressing

Page 40: Even more java script best practices

Update: people who already commented on PRs

Quiz#1rockwyc992 : 吳易璋CaeserNieh : 聶順成rockwyc992 : 吳易璋Quiz#2jason1122g : 邱義傑bdsword : 蔣彥亭c910335 : 陳達仁Davisanity : 林唐正amm040341 : 蘇聖雅rueian : 黃瑞安w181496 : 黃詩凱Peter654q :莊侑穎bdsword : 蔣彥亭Quiz#3rueian : 黃瑞安c910335 : 陳達仁CrashedBboy : 吳承霖Sharknevercries : 李政遠jason1122g : 邱義傑