Kotlin Multiplatform / We have a library for working with JS - moko-javascript

We have a library for working with JS - moko-javascript

While working on one of the projects, we faced a need to dynamically build screens. Previously, all screen display configuration came in JSON because the screen was a constructor form. Further JSON got into our engine, which determined the display of fields in accordance with the type and parameters that were in JSON. The set of fields was fixed, and this functionality was sufficient until we faced a need for dynamics tied to the data content of some fields.

For example, it was necessary to enable the user to add an attachment only if he chose to send documents remotely. Or provide additional fields for entering card data, if the client has chosen to pay in the application in this way. Other types of payment should not require this field when selected.

It was necessary for the visibility condition to be sent by the back, and we could interpret it on the client. Moreover, this had to be wrapped in a common code, since the application is based on KMM. Previously, it was impossible to rebuild an application written in KMM right off the bat. Due to the fixed logic, there was only one way to change the interface: to edit the code, compose a new build, send it to Google Play or the App Store for review and wait for the release. Only two of the five links in this chain of actions are related to development. Therefore, even if you code very quickly, it will take a lot of time from the start of work on changes to the release. It will take at least a week, if there is not much work and the marketplaces have no remarks.

We decided to transfer a string, which will contain the JS code that calculates and returns the result, from the server. Bool in the above mentioned case. That is, we pass this expression, the state of the form and user data to the JS handler, and it gives the result of the visibility of this field or option in the list.

How else can you use moko-javascript: in the case when you need to calculate a value, but the calculation formula is unknown in advance. If you make a request to the server every time, you have to wait for some time. The solution is a request that can return a JS expression to calculate the value. We will be able to use it locally in the calculation by substituting the desired parameters.

For example, user selects two dates, and a special field displays the difference between them in days to show how many days he has selected. This field cannot be entered manually, but must be displayed. Since the entire form is built simply on JSON configuration, no field knows about the existence of others, let alone that the value of the current field A is the difference in days between fields B and C.

This is where JS was helpful for us. We added the knowledge that the value of the A field is the result of executing the JS code line. The engine will execute this code itself, get the result, and we will retain the flexibility of forms and their fields configuration remotely, without resorting to additional code changes on the client. In short, the moko-javascript library makes the application logic more flexible, helps developers and clients avoid additional headache of re-releasing the application due to minor interface changes, and thus saves a huge amount of time. The library was released on June 16 - it is available on Github. Have the use of it!

moko-js.png