Advertisement

Programming in the Reactive Style with Meteor JS

FITC
FITC
Nov. 14, 2013
Advertisement

More Related Content

Advertisement

Programming in the Reactive Style with Meteor JS

  1. What’s Meteor about? “A better way to build apps.” “Meteor is an open source platform for building top-quality web apps in a fraction of the time, whether you’re an expert developer or just getting started.” ally? Re
  2. Programming in the Reactive Style using Meteor JS Dave Anderson dave@neo.com @eymiha
  3. Why Meteor?
  4. Will Meteor make your web apps look better?
  5. Will Meteor make it easier to build web apps users will enjoy?
  6. Will Meteor stop the software apocalypse from coming?
  7. Three Virtues of a Great Web Developer Programmer • • • Laziness Impatience Hubris
  8. • Laziness
  9. • Impatience
  10. • Hubris
  11. What Matters? • • • Fastest to Goal Most for Least Effort It Just Works
  12. What Obstructs? • • • • Wide Technology Stack Finding and Changing Code ‘Extra’ Coding Conventional Flow
  13. Traditional Wide Web Technology Stack Client: HTML, CSS, JavaScript ! Server: Programming Language, Database Access, Marshaling Language
  14. Meteor Web Technology Stack Client: HTML, CSS, JavaScript, MiniMongo ! Server: JavaScript, Mongo
  15. Additional Stack Java JavaScript libs, Jar files Ruby on Rails JavaScript libs, Gem files ASP Controls, Extensions Clojure Clojure files Meteor packages
  16. How We Must Code Nine months ago you wrote some code...
  17. How We Must Code Today you have to find and change it...
  18. Follow the Breadcrumbs... from the app in the browser ...to a search of the code base ...to the app inspector... ...repeatedly
  19. Searching for Code, Traditionally • look for element by CSS selector • look for action bound to element • look for code that emits element • look for code that changes CSS selector • keep looking inward...
  20. Changing Code, Traditionally • Change all the places! • Test like the Dickens! • ummm... did you really change all the places?
  21. Searching for Code in Meteor • look for the template that emits the element • examine its html, css and javascript
  22. Changing Code in Meteor • Change the code in the template • Test the changes (tinytest, laika, …)
  23. ‘Extra’ Coding This one is hard to quantify. HTML with handlebar templates JavaScript / CoffeeScript CSS / SCSS packages the code needed to build a Meteor-based web UX is the most straightforward I’ve found. YMMV
  24. Conventional Flow Code uses the current values when called. If a is 5 and b is 3, then c = a+b would set c to 8. Later, if a became 4 and b became 2, then c would still be 8 - until the code was called again.
  25. Reactive Flow Code uses values as they change. If a is 5 and b is 3, then c = a+b would set c to 8. Later, if a became 4 and b became 2, then c would become 6 automatically.
  26. Reactive Style Programming is about writing code that renders values when they are delivered not code that retrieves values so they can be rendered. Data is pushed, not pulled.
  27. How about an example... Let’s Take Attendance!
  28. webu13attend.meteor.com
  29. webu13attend Deconstruction client-only code shared code webby bits server-only code
  30. webu13attend client-only code handlebars! client/webu13attend.html
  31. webu13attend client-only code client/webu13attend.html
  32. webu13attend client-only code client/webu13attend.html
  33. webu13attend client-only code client/webu13attend.html
  34. webu13attend client-only code client/webu13attend.scss
  35. webu13attend client-only code client/webu13attend.coffee
  36. webu13attend server-only code server/attendees.coffee
  37. webu13attend shared code lib/collections.coffee
  38. webu13attend webby bits public/images/webu13.png
  39. A Closer Look at Reactivity
  40. Reactivity from the new-to-Meteor perspective...
  41. In the frameworks you’re probably used to, a web application is coded Imperatively. In Meteor using its Reactive framework, a web application is coded Declaratively.
  42. Imperative Coding if you’re lucky enough to have database notification through an observe-like function, you implement callbacks Items.find().observe! added: (item) ->! $('ul').append '<li id="'+item._id+'">'+! item.name+'</li>'! changed: (item) ->! $('ul li#'+item._id).text item.name! removed: (item) -> ! $('ul li#'+item._id).detach()
  43. Declarative Coding Meteor does the observe callback behind the scenes <template name="itemList">! <ul>! {{#each items}}! <li>{{name}}</li>! {{/each}}! </ul>! </template> Template.itemList.helpers! items: -> Items.find()
  44. How does Reactivity work? JS f Reactive Source first use
  45. How does Reactivity work? JS f Reactive Source Computation Processing
  46. How does Reactivity work? JS f Reactive Source Dependency Established
  47. How does Reactivity work? JS f Reactive Source ready to go
  48. Why Computations? Because Meteor has the potential to change the entire interface when a reactive source changes! Changes based on a computation becoming invalid focus on only the parts of the interface that depend on the computation.
  49. How does Reactivity work? JS f Reactive Source ready to go
  50. How does Reactivity work? JS f Reactive Source Source Updated
  51. How does Reactivity work? JS f Reactive Source Computation Invalidated
  52. How does Reactivity work? JS f Reactive Source Computation Processing
  53. How does Reactivity work? JS f Reactive Source Computation Processing
  54. How does Reactivity work? JS f Reactive Source ready to go
  55. What are Reactive Sources? remote local (client) database queries (subscriptions) session variables status user / user id logging in subscription ready
  56. Reactivity from the Meteor-savvy perspective... The magic is all in the Deps object!
  57. How does the Deps object work? Deps Computation Dependency
  58. Deps.Computation firstRun onInvalidate(function) invalidate( ) invalidated stop( ) stopped
  59. Deps.Dependency depend( ) changed( ) hasDependents( )
  60. Deps autorun(function) currentComputation active onInvalidate(function) flush( ) afterFlush(function) nonreactive(function)
  61. An Example: Injectives Sessions variables are reactive sources global to the client. An Injective is a local reactive source that can be more closely scoped to the functions and objects that uses it.
  62. Deps.injective = (init) ->! _value: init ? 0! _dep: new Deps.Dependency! ! set: (value) ->! if (@_value != value)! @_value = value! @changed()! get: ->! @depend()! @_value! depend: ->! @_dep.depend()! changed: ->! @_dep.changed()
  63. Consider the use of the an injective: Foo.innerWidth =! Deps.injective window.innerWidth Any computation that calls Foo.innerWidth.get( ) will react when its value changes. So then, $(window).resize ->! Foo.innerWidth.set window.innerWidth
  64. Example of Injectives Use the height and width of a browser window to control the size of some objects. innerWidth = Deps.injective window.innerWidth! innerHeight = Deps.injective window.innerHeight! ! $(window).resize ->! innerWidth.set window.innerWidth! innerHeight.set window.innerHeight! ! Template.box.helpers! size: ->! Math.floor (innerWidth.get()+innerHeight.get())/4.0
  65. So why should Meteor and Reactivity Narrower technology stack • matter to change • Easier to find and me? code • • Less extra coding Changes are pushed, not pulled
  66. When not to use Meteor
  67. Try It! the ke ge Ta len al Ch or te e M Give it two weeks and expand what you know about building web applications!
  68. Questions? Dave Anderson dave@neo.com @eymiha
Advertisement