Surely, there has to be a better way that we can do this. So, the idea was, what if we used our time travel API to pull all the React information out of a recording of the app and save that for use in our debugger client. So, there is no extension installed in Chrome. We're going to have to figure out a way to post process the recording and extract this data.
We don't know when React actually rendered, and how do we get those operations values anyway? So, our idea was in our backend server every time someone opens up a recording to debug it, we're going to kick off an extra little background process that uses our APIs to extract the data. And then, in order to make that possible, we're going to have to put code into our fork of Chrome to capture timestamps so that we even know what points in time React committed during the recording.
So, most of our modifications to Chrome are in one 6,000 line file that's a mixture of C++ and JavaScript inside C++ strings, which is horrible. But, we went in, mostly me, and I created, like, a fake little version of the React DevTools hook object. And that gets loaded into every page, so that React, during the recording, thinks it's talking to the extension. But, during the recording process, it's just saving timestamps, React rendered, React rendered, React rendered. That way, we know, later on, what points in time actually matter. There's a lot of extra tricky pieces of bookkeeping. I have to do some saving of React fiber variables and render variables to save for later, but it's maybe 100 lines of code. It's not too bad.
So, on the back end, we set up some scaffolding so that every time a user opens up a recording for the very first time and we don't have any data saved, we kick off a background process that has access to our protocol and can now start to call these analysis APIs to extract data. Now, you could write any standalone Node script that uses our protocol. We actually have some examples in a repo that we've put together. But conceptually, a routine is just like a background process that can call protocol methods. So, the basic idea is we first get all these annotations with the timestamps. Then we're going to have to actually send a copy of the React DevTools JavaScript code into this paused browser in the recording. And then for every commit, we're going to ask that bundle, give me all the operations for that commit. We have to do a little more reformatting on the data. And then finally, we can save this information so it can be used by our client UI.
So, how do you run code via time travel? And it's everybody's favorite tool, eval. Now of course, we've been told for years using eval is bad, and evil, and dangerous, and a security risk. And it's probably right. But in this case, it's the hammer that solves everything. So, you can send, like an eval is just, here's a string of code, hey JavaScript interpreter, please run this as if it was real code. We can send a string of code over the network and run it inside a paused browser in the recording in the cloud and it actually works. So, in this example, I'm just evaluating like a tiny little string and it's a few lines.
Comments