Created
March 12, 2019 16:22
-
-
Save SuperOleg39/2071ba42e61f03db54d9b545b0a19634 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const state = [{ | |
| id: 1, | |
| active: false, | |
| loaded: false, | |
| }, { | |
| id: 2, | |
| active: false, | |
| loaded: false, | |
| }, { | |
| id: 3, | |
| active: false, | |
| loaded: false, | |
| }, { | |
| id: 4, | |
| active: false, | |
| loaded: false, | |
| }, { | |
| id: 5, | |
| active: false, | |
| loaded: false, | |
| }, { | |
| id: 6, | |
| active: false, | |
| loaded: false, | |
| }]; | |
| const request = (id) => new Promise((resolve) => { | |
| setTimeout(() => { | |
| resolve(); | |
| }, 300); | |
| }); | |
| const createPagination = (state, step) => { | |
| let page = 1; | |
| let inProcess = false; | |
| let done = false; | |
| return { | |
| next: async () => { | |
| if (inProcess || done) { | |
| return; | |
| } | |
| inProcess = true; | |
| for (let i = page * step - step; i < page * step; i++) { | |
| state[i].active = true; | |
| } | |
| for (let i = page * step - step; i < page * step; i++) { | |
| await request(state[i].id); | |
| state[i].loaded = true; | |
| } | |
| inProcess = false; | |
| done = state.length / step <= page; | |
| if (!done) { | |
| page += 1; | |
| } | |
| return { | |
| done, | |
| page, | |
| inProcess, | |
| }; | |
| } | |
| }; | |
| } | |
| const pagination = createPagination(state, 2); | |
| const onBottomEdge = () => { | |
| setTimeout(() => { | |
| pagination.next().then(({done, inProcess}) => { | |
| if (!done && !inProcess) { | |
| onBottomEdge(); | |
| } | |
| }); | |
| }, 500); | |
| }; | |
| onBottomEdge(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment