Refech using getMany, reducing the number of API calls

This commit is contained in:
Deluan 2021-06-16 10:00:31 -04:00
parent 521d1ff2bf
commit fb7229a53e
3 changed files with 19 additions and 22 deletions

View File

@ -217,11 +217,11 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
return err
}
if exist {
err = tx.Album(ctx).SetStar(star, ids...)
err = tx.Album(ctx).SetStar(star, id)
if err != nil {
return err
}
event = event.With("album", ids...)
event = event.With("album", id)
continue
}
exist, err = tx.Artist(ctx).Exists(id)
@ -229,18 +229,18 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
return err
}
if exist {
err = tx.Artist(ctx).SetStar(star, ids...)
err = tx.Artist(ctx).SetStar(star, id)
if err != nil {
return err
}
event = event.With("artist", ids...)
event = event.With("artist", id)
continue
}
err = tx.MediaFile(ctx).SetStar(star, ids...)
err = tx.MediaFile(ctx).SetStar(star, id)
if err != nil {
return err
}
event = event.With("song", ids...)
event = event.With("song", id)
}
c.broker.SendMessage(ctx, event)
return nil

View File

@ -27,9 +27,9 @@ export const useResourceRefresh = (...visibleResources) => {
if (resources) {
Object.keys(resources).forEach((r) => {
if (visibleResources.length === 0 || visibleResources?.includes(r)) {
resources[r]?.forEach((id) => {
dataProvider.getOne(r, { id })
})
if (resources[r]?.length > 0) {
dataProvider.getMany(r, { ids: resources[r] })
}
}
})
}

View File

@ -24,8 +24,8 @@ describe('useResourceRefresh', () => {
const useStateMock = (initState) => [initState, setState]
const refresh = jest.fn()
const useRefreshMock = () => refresh
const getOne = jest.fn()
const useDataProviderMock = () => ({ getOne })
const getMany = jest.fn()
const useDataProviderMock = () => ({ getMany })
let lastTime
beforeEach(() => {
@ -69,7 +69,7 @@ describe('useResourceRefresh', () => {
useResourceRefresh()
expect(refresh).toHaveBeenCalledTimes(1)
expect(getOne).not.toHaveBeenCalled()
expect(getMany).not.toHaveBeenCalled()
})
it('triggers a UI refresh when received an "any" id', () => {
@ -82,7 +82,7 @@ describe('useResourceRefresh', () => {
useResourceRefresh()
expect(refresh).toHaveBeenCalledTimes(1)
expect(getOne).not.toHaveBeenCalled()
expect(getMany).not.toHaveBeenCalled()
})
it('triggers a refetch of the resources received', () => {
@ -95,11 +95,9 @@ describe('useResourceRefresh', () => {
useResourceRefresh()
expect(refresh).not.toHaveBeenCalled()
expect(getOne).toHaveBeenCalledTimes(4)
expect(getOne).toHaveBeenCalledWith('album', { id: 'al-1' })
expect(getOne).toHaveBeenCalledWith('album', { id: 'al-2' })
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-1' })
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-2' })
expect(getMany).toHaveBeenCalledTimes(2)
expect(getMany).toHaveBeenCalledWith('album', { ids: ['al-1', 'al-2'] })
expect(getMany).toHaveBeenCalledWith('song', { ids: ['sg-1', 'sg-2'] })
})
})
@ -114,7 +112,7 @@ describe('useResourceRefresh', () => {
useResourceRefresh('album')
expect(refresh).toHaveBeenCalledTimes(1)
expect(getOne).not.toHaveBeenCalled()
expect(getMany).not.toHaveBeenCalled()
})
it('triggers a refetch of the resources received if they are visible', () => {
@ -127,9 +125,8 @@ describe('useResourceRefresh', () => {
useResourceRefresh('song')
expect(refresh).not.toHaveBeenCalled()
expect(getOne).toHaveBeenCalledTimes(2)
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-1' })
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-2' })
expect(getMany).toHaveBeenCalledTimes(1)
expect(getMany).toHaveBeenCalledWith('song', { ids: ['sg-1', 'sg-2'] })
})
})
})