Skip to content

Fix prefetching in range mode with args #20

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/range-with-query-and-prefetch/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<div>
<input v-model="query" placeholder="Search for a license...">
<ul>
<li v-for="item in licenses.items" :key="item.id">{{ item.id }}</li>
</ul>
<a @click="licenses.pageTo++">Load more...</a>
</div>
</template>
<script>
import { createInstance } from '../../'

export default {
data() {
return {
query: ''
}
},
computed: {
licenses: createInstance('licenses', {
pageFrom: 1,
args () {
return {
query: this.query,
}
},
})
}
}
</script>
22 changes: 22 additions & 0 deletions examples/range-with-query-and-prefetch/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
import { PaginationPlugin, createResource } from '../../'
import { fetchPage } from '../_api/api-client'

Vue.config.productionTip = false

Vue.use(Vuex)
Vue.use(PaginationPlugin)

const store = new Vuex.Store({
strict: true
})

// Initialize resource
createResource('licenses', fetchPage, { prefetch: true })

new Vue({
render: h => h(App),
store
}).$mount('#app')
5 changes: 5 additions & 0 deletions examples/range-with-query-and-prefetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"serve": "../../node_modules/.bin/vue-cli-service serve main.js"
}
}
14 changes: 10 additions & 4 deletions src/createResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,24 @@ module.exports = function (name, fetchPage, opts) {
prefetchNextPage: async function ({ commit, state }, instanceConfig) {
if (state.currentRequest) await state.currentRequest
let registryName = instanceConfig.registryName
if (instanceConfig.page && (instanceConfig.page * instanceConfig.pageSize) >= state.registry[registryName].items.length) return
if (instanceConfig.pageTo && (instanceConfig.pageTo * instanceConfig.pageSize) >= state.registry[registryName].items.length) return
let rangeMode = !!instanceConfig.pageFrom
let currentPage = rangeMode ? instanceConfig.pageTo : instanceConfig.page
if ((currentPage * instanceConfig.pageSize) >= state.registry[registryName].items.length) return

let nextPage = (instanceConfig.page ? instanceConfig.page : instanceConfig.pageTo) + 1
let nextPage = currentPage + 1

let nextPageIndexStart = nextPage * instanceConfig.pageSize - instanceConfig.pageSize
let nextPageFragment = state.registry[registryName].items.slice(nextPageIndexStart, nextPageIndexStart + instanceConfig.pageSize)

if (!nextPageFragment.includes(undefined)) return

let opts = Object.assign({}, instanceConfig)
opts.page += 1
if (rangeMode) {
opts.pageTo++
opts.page = opts.pageTo
} else {
opts.page++
}
let nextPageReq = fetchPage.call(this, opts).then((result) => {
let slice = []
result.data.map((item) => {
Expand Down
87 changes: 87 additions & 0 deletions test/createInstance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,93 @@ test('Range mode with args fn', async function () {
expect(adapter.lastArgs.args).toEqual({ counter: 2 })
})

test('Range mode with args fn and prefetch', async function () {
let adapter = new TestAdapter()
adapter.nextResult = {
total: 33,
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

const opts = {
pageFrom: 1,
pageTo: 1,
pageSize: 10,
args () {
return { counter: this.counter }
}
}

let component = {
data () {
return {
counter: 1
}
},
computed: {
test: createInstance('test13', opts)
},
render (h) {
return h('div')
}
}

let wrapper = createWrapper('test13', opts, component)
createResource('test13', adapter.fetchPage.bind(adapter), { prefetch: true })

expect(wrapper.vm.test.loading).toBe(true)
await nextTick()

expect(wrapper.vm.test.loading).toBe(false)
expect(wrapper.vm.test.pageFrom).toBe(1)
expect(wrapper.vm.test.pageTo).toBe(1)
expect(wrapper.vm.test.total).toBe(33)
expect(wrapper.vm.test.items.length).toBe(10)
expect(wrapper.vm.test.items).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(adapter.lastArgs.args).toEqual({ counter: 1 })

wrapper.vm.test.pageTo = 2
await nextTick()

expect(wrapper.vm.test.loading).toBe(false)
expect(wrapper.vm.test.pageFrom).toBe(1)
expect(wrapper.vm.test.pageTo).toBe(2)
expect(wrapper.vm.test.total).toBe(33)
expect(wrapper.vm.test.items.length).toBe(20)
expect(wrapper.vm.test.items).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(adapter.lastArgs.args).toEqual({ counter: 1 })

adapter.nextResult = {
total: 22,
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

wrapper.vm.counter = 2

expect(wrapper.vm.test.loading).toBe(true)
expect(wrapper.vm.test.pageFrom).toBe(1)
expect(wrapper.vm.test.pageTo).toBe(1)
await nextTick()

expect(wrapper.vm.test.loading).toBe(false)
expect(wrapper.vm.test.pageFrom).toBe(1)
expect(wrapper.vm.test.pageTo).toBe(1)
expect(wrapper.vm.test.total).toBe(22)
expect(wrapper.vm.test.items.length).toBe(10)
expect(wrapper.vm.test.items).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(adapter.lastArgs.args).toEqual({ counter: 2 })

wrapper.vm.test.pageTo = 2
await nextTick()

expect(wrapper.vm.test.loading).toBe(false)
expect(wrapper.vm.test.pageFrom).toBe(1)
expect(wrapper.vm.test.pageTo).toBe(2)
expect(wrapper.vm.test.total).toBe(22)
expect(wrapper.vm.test.items.length).toBe(20)
expect(wrapper.vm.test.items).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(adapter.lastArgs.args).toEqual({ counter: 2 })
})

test('Don\'t touch computed getters', async function () {
let adapter = new TestAdapter()
adapter.nextResult = {
Expand Down