Skip to content

Page iterator Implementation #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 3, 2018
Merged

Page iterator Implementation #118

merged 6 commits into from
Oct 3, 2018

Conversation

muthurathinam
Copy link
Contributor

PageIterator

For a variety of reasons, collections of entities are often split into pages and each page is returned with a URL to the next page. Sometimes, page granularity provided by the API does not match the requirements of the consumer. This task aims to simplify the life of consumers of paged collections.

Iterate over all the messages

async function callingPattern() {
    try {
        // Makes request to fetch mails list. Which is expected to have multiple pages of data.
        let response: PageCollection = await client.api("/me/messages").get();
       // A callback function to be called for every item in the collection. This call back should return boolean indicating whether not to continue the iteration process.
        let callback: PageIteratorCallback = (data) => {
            console.log(data);
            return true;
        };
        // Creating a new page iterator instance with client a graph client instance, page collection response from request and callback
        let pageIterator = new PageIterator(client, response, callback);
        // This iterates the collection until the nextLink is drained out.
        pageIterator.iterate();
    } catch (e) {
        throw e;
    }
}

Stopping and Resuming the iteration

// Populating custom size pages if the api restricts to some maximum size. Lazy loading more data on user prompt or something, stop and resume will do the trick.
async function customSize() {
    try {
        let response: PageCollection = await client.api("/me/messages").get();
        let size = 1000;
        let count = 0;
        let callback: PageIteratorCallback = (data) => {
            console.log(data);
            count++;
            if (count === size) {
                count = 0;
                return false;
            }
            return true;
        };
        let pageIterator = new PageIterator(client, response, callback);
        // This stops iterating over for 1000 entities.
        pageIterator.iterate();

        // Resuming will do start from where it left off and iterate for next 1000 entities.
        // Resume is likely to be called in any user interaction requiring to load more data.
        pageIterator.resume();
    } catch (e) {
        throw e;
    }
}

Copy link
Contributor

@MIchaelMainer MIchaelMainer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support DeltaLink as part of the pageIterator as it uses nextLink to perform the paging until the changes are sync'd

* Iterates over a collection by enqueuing entries one by one and kicking the callback with the enqueued entry
* @return A boolean indicating the continue flag to process next page
*/
iterationHelper(): boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think iterationHelper is supposed to be private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in the spec as private, but I missed in the implementation. will change

iterate(): Promise<any>;
/**
* @async
* This internally calls the iterate method, Its juts for more readability.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it's just

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will change

@muthurathinam
Copy link
Contributor Author

@MIchaelMainer Have done the changes that you are requested.

Copy link

@deepak2016 deepak2016 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@muthurathinam muthurathinam merged commit ca5e846 into dev Oct 3, 2018
@muthurathinam muthurathinam deleted the PageIterator branch October 3, 2018 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants