{"id":2368,"date":"2014-11-01T14:42:40","date_gmt":"2014-11-01T14:42:40","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2368"},"modified":"2014-11-01T14:42:40","modified_gmt":"2014-11-01T14:42:40","slug":"trace-logging-react","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/trace-logging-react\/","title":{"rendered":"Trace Logging with React"},"content":{"rendered":"<p>Long ago, when I was a Delphi (Pascal) programmer, I encountered a neat language feature called <a href=\"http:\/\/www.delphibasics.co.uk\/RTL.asp?Name=Property\">properties<\/a>, which are awesome for debugging (and more well known now, as they are in C#). <\/p>\n<pre lang=\"pascal\">\n  Property Area   : Longint read fArea;\n  Property Left   : Longint Index 0 read GetCoord write SetCoord;\n<\/pre>\n<p>Properties are accessed with the same syntax as a class-level variable. The consequence of this is that you can turn any variable into a property by adding three or four lines of code, which gives you a chokepoint where you can put a breakpoint to find out whenever that variable is set, which can be pretty awesome for understanding how an application works. I found this taught me to visualize an application as a plumbing problem: data goes from point A to point B, and all you need to do to fix a defect is figure out where it stops or gets mangled.<\/p>\n<p>In the new world of React programming, we&#8217;re taught to treat a UI code as simply a mapping from state to DOM objects, which is similarly freeing (although without the rigidity that XSLT imposed). <\/p>\n<p>If you use JSX, you can write composable components that are pretty easy to read (this is rendering a proposal editor):<\/p>\n<pre lang=\"Javascript\">\nreturn (\n  <Accordion\n    selectedIndex={self.props.ui.accordionIndex}\n    setSelectedIndex={self.props.events.setAccordionIndex}>\n    <div title=\"Choose Template\" key=\"templateChooser\">\n      <PickList values={this.props.templates} \n                onselect={self.props.events.selectProposal}><\/PickList>\n    <\/div>\n    <FeeEditor \n      title=\"Fees\" \n      currency={currencyFn} \n      fees={self.props.ui.selectedFees} \n      events={self.props.events} \n      products={self.props.products} \n      feeTypes={self.props.feeTypes} \n      worksheetFocus={self.props.ui.worksheetFocus}\n      setWorksheetFocus={self.props.events.setWorksheetFocus} \/>\n      <Accordion\n        title=\"Proposal Sections\"\n        joinParent=\"true\"\n        selectedIndex={self.props.ui.sectionIndex}\n        setSelectedIndex={self.props.events.setSectionIndex} >\n        {sections}\n      <\/Accordion>\n      <div \n        title=\"Client Contact\">\n        <PickList values={self.props.clients} \n          onselect={self.props.events.selectUser} >\n        <\/PickList>\n        <UserEditor user={self.props.ui.selectedUser} \/>\n      <\/div>\n    <\/Accordion>\n  );\n<\/pre>\n<p>All the state that controls the UI is passed in. <\/p>\n<p>This design causes one or more large top-level objects. I split these into three classes of state: the &#8216;input&#8217; data model (things from the database), the &#8216;output&#8217; data model (what the end user thinks they picked), and transient UI state (which text box is focused, which tabs are open, etc).<\/p>\n<p>The top level state object exposes event handlers which correspond to things a user does:<\/p>\n<pre lang=\"javascript\">\nvar events = {\n  selectProposal: function (proposal, event, id) {\n    data.ui.sectionIndex = 0;\n    data.ui.selectedSections = proposal.sections;\n\n    page.setState(data);\n  }\n...\n}\n<\/pre>\n<p>Most of these event handlers look almost the same (using <a href=\"https:\/\/github.com\/dustingetz\/react-cursor\">cursors<\/a> may be eventually a helpful simplification).<\/p>\n<p>In essence, we&#8217;ve replicated the &#8220;property&#8221; functionality that C# and Delphi have, which some fun results.<\/p>\n<p>We can trivially write a function which takes the above events list and wraps each event in logging code:<\/p>\n<pre lang=\"Javascript\">\nfunction trace(events, log) {\n  return _(events).reduce(\n    function (accum, fn, key) {\n      accum[key] = _.partial(log, fn, key);\n      return accum;\n    },\n  {});\n}\n<\/pre>\n<p>The &#8216;log&#8217; function can just write to the console output, although that can get verbose fast:<\/p>\n<pre lang=\"Javascript\">\nlog.before = function (fn, key, args) {\n  console.log(key + ': ' + args.join(', '));\n}\n<\/pre>\n<p>Rather than logging everything, I&#8217;ve chosen to provide pre\/post event hooks &#8211; this lets you focus the log on the particular piece of state you&#8217;re interested in.<\/p>\n<pre lang=\"javascript\">\nfunction log() {\n  fn = arguments[0];\n  key = arguments[1];\n\n  var newArgs = _(arguments).slice(2);\n\n  if (!!log.before) {\n    log.before(fn, key, newArgs);\n  }\n\n  fn.apply(this, newArgs.value());\n      \n  if (!!log.after) {\n    log.after(fn, key, newArgs);\n  }\n}\n<\/pre>\n<p>An interesting side effect of this approach is that you can add conditional breakpoints that fire when a value is set:<\/p>\n<pre lang=\"Javascript\">\nlog.after = function (fn, key, args) {\n  console.log('data.ui.accordionIndex: ' + data.ui.accordionIndex);\n\n  if (data.ui.accordionIndex === undefined) {\n    debugger;\n  }\n}\n<\/pre>\n<p>You can also record checkpoints of the top level state &#8211; this lets you return at any time to the point right before your error occurred:<\/p>\n<pre lang=\"Javascript\">\nlog.before = function (fn, key, args) {\n  console.log('data.ui.accordionIndex: ' + data.ui.accordionIndex);\n\n  if (args[0] === undefined) {\n    checkpoints.push(_.cloneDeep(data));\n  }\n}\n<\/pre>\n<p>Alternately, if data became wrong earlier in the process, you could record constant snapshots, allowing you to replay everything in a test case.<\/p>\n<p>Overall, this style of programming is a huge improvement in working with the otherwise very chaotic nature of Javascript development.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long ago, when I was a Delphi (Pascal) programmer, I encountered a neat language feature called properties, which are awesome for debugging (and more well known now, as they are in C#). Property Area : Longint read fArea; Property Left : Longint Index 0 read GetCoord write SetCoord; Properties are accessed with the same syntax &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/trace-logging-react\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Trace Logging with React&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,29],"tags":[302,345,440,449,542],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2368"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2368"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2368\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}