JoshLambert.xyz

Small businesses, independent infrastructure, and a stubborn bet that things get better.

Ti.ModernRESTe: RESTe 2 for Titanium Apps

Ti.ModernRESTe: RESTe 2 for Titanium Apps

A phone app is mostly screens asking a server for things. Mobile Wash USA has 80-some screens, and nearly every one loads something: the jobs near a vendor, the offers on a job, a chat, a receipt. The code that does the asking gets used more than anything else in the app.

In Titanium apps that code has very often been RESTe, a library by Jason Kneen of BouncingFish. You describe your whole API once, in one config, and RESTe hands back a function for every endpoint and lists that fill themselves. It has carried Titanium apps for more than ten years, ours included. Its last release on npm was in 2021.

Every project we built on RESTe grew the same layer of code around it, for promises, retries and error handling. When we started the Mobile Wash USA rebuild, with a week to wire all of those screens to a new backend, I didn’t want to write that layer one more time. So my company, Centreville Tech, moved it into the library and released the result as RESTe 2, in a repository called Ti.ModernRESTe.

Jason’s idea

Jason is a developer in England, online as BouncingFish, and he published RESTe in February 2015. The idea in it is the reason so many Titanium apps were quick to build. Without something like it, every screen that needs data ends up carrying its own networking code. With it, the whole API lives in one short file that a new developer can read in a minute, and the screens just ask for what they want.

I’ve leaned on that idea for years, and Mobile Wash USA went from a prototype on sample data to a real backend in days partly because of it. RESTe 2 exists because RESTe was worth building on. It’s still his design, and everything I added sits behind the same config he came up with.

What’s new

  • Every call returns a promise, which is how JavaScript is written now. The old callbacks still work.
  • A failure comes back as one kind of error that says what happened: the server said no, the network dropped, or the request timed out.
  • A failed request is repeated automatically only when repeating can’t do harm. Anything else needs an idempotency key, a tag that lets the server recognize a duplicate. In Mobile Wash USA, posting a job carries one, so the server can tell a retry on a weak signal from a second job.
  • When the server says a list hasn’t changed, the app doesn’t download it again.
  • A long list loads a page at a time and keeps what the server said about how many are left.
  • When a user signs out, answers still in flight from their session are dropped and never land in the next user’s screens.

Lists that don’t flicker

The change a user can see is in the lists. The old code rebuilt a list from nothing every time it was fetched. With the newer versions of Backbone, the library underneath Titanium’s lists, that made a bound list redraw once for every row. RESTe 2 merges the new data into the list that’s already there and redraws once.

Two list screens from the Mobile Wash USA app. A vendor’s Available Jobs screen shows a fleet wash for six work trucks at $900 and a car wash at $120, each with its date, town and distance. A customer’s My Jobs screen shows a driveway and deck pressure wash at $250 and a boat and trailer wash at $200.
Two of the lists RESTe 2 keeps filled in Mobile Wash USA, shown with sample data: a vendor’s Available Jobs and a customer’s My Jobs.

The benchmark in the repository compares RESTe 2 with Jason’s current code. It runs in Node on a shared server and measures only the library’s own work, with a 500-item list.

MeasurementUpstream RESTeRESTe 2
First fetch into a list (Backbone 1.4.0)501 redraws1 redraw
Fetch again, nothing changed5.0 to 6.2 ms0.8 to 1.1 ms, no redraw
Fetch again, 10 items changed10 redraws1 redraw
Reading the 500-item response2.3 to 2.4 ms1.5 ms

It isn’t free. The file is thirteen times the size of the original and takes a few milliseconds longer to load when the app starts, and a single small request costs 3 or 4 millionths of a second more than it used to. The README gives those numbers alongside the good ones.

Keeping Jason’s design

A fork that breaks the apps already using a library isn’t much use to them, so RESTe 2 is still one file with no dependencies, and a config written for RESTe 1 still works. To check that, 62 scenarios taken from the original README run against both versions side by side.

The rest of the suite is 417 tests, many of them repeated on each of the five Backbone versions Titanium’s Alloy framework offers. There’s also a check that breaks 77 safety-related lines one at a time and confirms that a test fails each time. The example app runs 48 more checks on an Android device image and an iPhone simulator. Mobile Wash USA runs on RESTe 2 in both stores.

RESTe is Jason’s. The fork keeps the project’s history, his name on the copyright and his Apache license, and the changes are there for his project to take. If RESTe 2 is useful to you, the person to thank first is Jason.

Where to get it

Version 2.0.0 is on GitHub. Copy reste.js into your app or install it with npm from the release tag. It was tested on Titanium SDK 13.4.1 with Alloy 3.0.1. It’s one of three modules that came out of the Mobile Wash USA rebuild, with Ti.Paysheet for card payments and Ti.Socketline for live chat.

Is your Titanium app built on a library nobody has updated in years? Send me an email and tell me where it’s stuck.