Using JavaScript Objects/Arrays during iteration


When a JavaScript Array is populated with attributes within query on a container, you will notice that the Array object holds data for future iterations of the query while in the current iteration. This is because of how RPE processes the data and write to the output document in separate threads.

The solution here is to use additional variable called _arrayLength, which should be incremented for every query iteration and any operation on the Array should be through _arrayLength RPE variable.

js-array-iteration

The output (incorrect) is

Array Length (_array.length): 0

Array Length (_array.length): 5

Array Length (_array.length): 5

Array Length (_array.length): 5

Array Length (_array.length): 5

Expected output

Array Length (_array.length): 0

Array Length (_array.length): 1

Array Length (_array.length): 2

Array Length (_array.length): 3

Array Length (_array.length): 4

Solution

For any operations like read, add (push), remove (pop) on such Arrays or objects, you should drive it through a primitive variable _arrayLength and update _arrayLength variable whenever the actual array is being modified. Find sample template here.

Advertisements

Author: Kumaraswamy M

Kumar is an architect for Rational Publishing Engine. He has been with the product for more than 4 years and has been involved in many releases of RPE. His expertise are in Web application development and API design. He loves going on a long ride on bike and conquering peaks during weekend treks.

1 thought on “Using JavaScript Objects/Arrays during iteration”

  1. Good point Kumar. RPE is a multithreaded application designed for performance. The data input works in parallel with the data output and the template processing is split between the two.

    – the input thread – reads the data and executes filters, sorts, conditions, assignments and JavaScript blocks
    – the output thread – evaluates the template elements content and formatting properties. The output thread will receive a snapshot of the execution context at the time the element was reached by the input thread but objects like arrays are not snap-shotted

    The example you provided shows just that:
    – the java script block is evaluated in the input thread
    – the text element content is evaluate in the output thread
    and since they are asynchronous the length of the array in the output thread can be anything between 1 and 5, it all depends of how the threads are prioritized.

    Sample log:

    CRRPE1064I Data source DOORS using URL D:\rpe.support\JSEvaluation\data.xml
    >>> Input thread: evaluating JavaScript block
    >>> Input thread: evaluating JavaScript block
    >>> Input thread: evaluating JavaScript block
    >>> Input thread: evaluating JavaScript block
    CRRPE3571I Finished reading input XML. 18 elements processed.
    >>> Input thread: evaluating JavaScript block
    >>> Output Thread: evaluating element property
    >>> Output Thread: evaluating element property
    >>> Output Thread: evaluating element property
    >>> Output Thread: evaluating element property
    >>> Output Thread: evaluating element property
    CRRPE1064I 60 publishing commands are processed. 0 publishing commands remain to be processed.

    In conclusion: when working with JavaScript in RPE keep in mind that complex objects will not be snapshotted when passed to the output thread and you need to do the snapshot yourself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s