COUNT Library

Often times in interfaces we have this use case where we have a datasource that will give us information with a sequence of incrementing IDs. For instance the IMAP email protocol is an example of this.

If you only had 20 emails and each email takes a second to query, then a for loop would work fine. But what if you have 100,000 emails? You really need to be able to wake up a component, get a small batch of say 10, iterate and store the next ID and then get the next 10 and so on.

It’s nice to control where you start from and keep incrementing the value as we go forward. The COUNT library solves this problem.

To test out the COUNT library, make a custom component. See Create a Component.

See Import a Library if you need a refresher on how to do it.

NextId
First, in config.json add your Custom Fields. Then go to your component card and set the value to 156.

Note: You’ll need to change your component to use the DEVELOPMENT commit to see the newly added custom field. See Choosing the code to run for your component.

We can use COUNTget to get the data from the custom field called NextId.

We increment NextId by 1 each time the component runs.

local NextId = COUNTget("NextId") NextId = NextId+1

COUNTset saves the new value of the custom field in the custom field value file.

COUNTset("NextId", NextId)

This means we can see the NextId in the card view of the component - and we can set it too!

This is a very useful pattern for lots of Feeder interfaces. It’s used as part of the IMAP Email Feed component.