Dev

Reactive Time with Vue.js

Mar 29, 2017

Now that we’re diving into the day-to-day with time-tracking in Cushion, accurately indicating the current time and date is very important. As a timer runs, its duration should increase, but it should also grow in the graph as time passes. Likewise, as the current day carries over to the next day, the interface should update to reflect that today of a moment ago is now yesterday. Before reactive frameworks like Vue.js, this was a considerable task, but now it couldn’t be easier.

When we first released the time-tracking beta, we used a computed now property to indicate the current time:

We quickly realized that new Date is not reactive, so the computed property would cache itself and never update. Having a now property is still incredibly useful when dealing with time-tracking, but we needed to update the value on an interval, so the property wouldn’t remain cached. To achieve this, we used the setInterval function to update the now property each minute:

This worked well for a single component, but we rely on the now property in several other areas of the app, so we would need to update those values, too. I scoff at the idea of repeating this logic wherever needed, let alone running multiple intervals simultaneously, so we needed to find a better way. We needed a centralized now property that would update itself across all the components that rely on it.

In Cushion, we use Vue.js’s state management library, Vuex, which provides the centralized store we need to map global properties to our components. We moved the now property out of our component’s local state and into a brand new, rent-controlled time module within our Vuex store. This let us access the property from any component that needs to reference it instead of copy/pasting the interval everywhere like a monster.

We then added a start action to the time module, which starts the interval and commits the current time each minute—exactly like before, but now centralized:

In our components, we map the centralized now state to a local computed property, which updates itself when the property changes:

And, for components that only need to be updated when the day changes, we can add a getter to our Vuex module:

Cushion’s interface will now automatically update itself to accurately reflect the current time. In the time-tracking week view, today’s column will highlight the current day and move to the next one when we cross midnight. In the day view, the “now” label will move across the graph as each minute passes and any timer will follow along with it. I doubt anyone will notice or think about the additional work required to make this happen, but that’s the story of my life.

Share this on Twitter or Facebook

Archive

  1. Restructuring an Evolving App: Part 1
    Design
  2. My Typical Week as a Founder
    Story
  3. Building Components in a Sandbox
    Dev
  4. Reactive Time with Vue.js
    Dev
  5. Visualizing Daylight Saving Time
    Dev
  6. Recording Screencast GIFs
    Dev
  7. Writing a Job Listing
    Story
  8. Using Feature Flags to Run Betas
    Dev
  9. Our First Company Lunch
    Story
  10. How to embed Vue.js & Vuex inside an AngularJS app... wait what?
    Dev
  11. Funding Cushion
    Story
  12. Hiring a Team of Freelancers
    Story
  13. Taking a Real Break From Work
    Story
  14. Slack as a Notification Center
    Dev
  15. Document Your Features
    Story
  16. 300
    Story
  17. Vacations
    Design
  18. Offering Discounts
    Design
  19. Waves of Traffic
    Story
  20. Less Blogging, More Journaling
    Story
  21. Retention Through Useful Features
    Design
  22. The Onboarding Checklist
    Design
  23. Spreading the Word
    Story
  24. From Beta to Launch - The Subdomain
    Dev
  25. From Beta to Launch - Sign up
    Design
  26. From Beta to Launch - Messaging
    Design
  27. Launch
    Story
  28. Authenticating with 3rd Party Services
    Dev
  29. Intro to Integrations
    Design
  30. Inspiration vs Imitation
    Story
  31. The Emotional Rollercoaster
    Story
  32. Designing Project Blocks
    Design
  33. Everything in Increments
    Story
  34. Deleting Your Account
    Design
  35. Designing the Subscription Page
    Design
  36. Rewriting the Timeline
    Dev
  37. Restructuring the Individual Project Page
    Design
  38. Project Blocks
    Story
  39. Redesigning the Homepage
    Design
  40. Multiple Timelines
    Design
  41. Archiving and Estimate Differences
    Design
  42. Multiple Financial Goals
    Design
  43. Zooming in on the Timeline
    Design
  44. Currency
    Dev
  45. Preferences, Accounts, and a Typeface Change
    Design
  46. Sending Out the First Email
    Story
  47. Currency Inputs, Notifications, and Invoice Nets
    Design
  48. Dots and Lines
    Design
  49. Calculating in the Database and Revealing Tendencies
    Dev
  50. Improved Form UX
    Design
  51. Cushion is Online
    Story
  52. Schedule Timeline Patterns
    Design
  53. A Slimmer Schedule Timeline
    Design
  54. The Schedule Timeline
    Design
  55. Plugging in Real Data for the First Time
    Design
  56. Transitions and Project Lists
    Design
  57. Death to Modals
    Design
  58. The Individual Project Page
    Design
  59. Estimated Incomes and Talks with Other Freelancers
    Story
  60. Statuses to Lists and the Paid Beta
    Story
  61. The Timeline
    Story
  62. Invoice Terminology
    Dev
  63. Modal Forms
    Dev
  64. Wiring the Backend to the Frontend
    Dev
  65. Balancing Design and Dev
    Story
  66. Timecop, Monocle, and Vagrant
    Dev
  67. Going with Ruby and Sinatra
    Dev
  68. Ditching local-first and trying out Node.js
    Dev
  69. Switching to AngularJS
    Dev
  70. Building the Table with Vue.js
    Dev
  71. Clients, Projects, and Invoices
    Dev
  72. Introduction
    Story

Running Costs

Take a close look at the costs that go into running a web app and why we use specific services.

View the Costs

How It’s Made

Follow along with the journal for insight into the overall experience of building an app.

Read the Journal