Refech using getMany, reducing the number of API calls
This commit is contained in:
@@ -217,11 +217,11 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
err = tx.Album(ctx).SetStar(star, ids...)
|
err = tx.Album(ctx).SetStar(star, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
event = event.With("album", ids...)
|
event = event.With("album", id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
exist, err = tx.Artist(ctx).Exists(id)
|
exist, err = tx.Artist(ctx).Exists(id)
|
||||||
@@ -229,18 +229,18 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
err = tx.Artist(ctx).SetStar(star, ids...)
|
err = tx.Artist(ctx).SetStar(star, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
event = event.With("artist", ids...)
|
event = event.With("artist", id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err = tx.MediaFile(ctx).SetStar(star, ids...)
|
err = tx.MediaFile(ctx).SetStar(star, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
event = event.With("song", ids...)
|
event = event.With("song", id)
|
||||||
}
|
}
|
||||||
c.broker.SendMessage(ctx, event)
|
c.broker.SendMessage(ctx, event)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ export const useResourceRefresh = (...visibleResources) => {
|
|||||||
if (resources) {
|
if (resources) {
|
||||||
Object.keys(resources).forEach((r) => {
|
Object.keys(resources).forEach((r) => {
|
||||||
if (visibleResources.length === 0 || visibleResources?.includes(r)) {
|
if (visibleResources.length === 0 || visibleResources?.includes(r)) {
|
||||||
resources[r]?.forEach((id) => {
|
if (resources[r]?.length > 0) {
|
||||||
dataProvider.getOne(r, { id })
|
dataProvider.getMany(r, { ids: resources[r] })
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ describe('useResourceRefresh', () => {
|
|||||||
const useStateMock = (initState) => [initState, setState]
|
const useStateMock = (initState) => [initState, setState]
|
||||||
const refresh = jest.fn()
|
const refresh = jest.fn()
|
||||||
const useRefreshMock = () => refresh
|
const useRefreshMock = () => refresh
|
||||||
const getOne = jest.fn()
|
const getMany = jest.fn()
|
||||||
const useDataProviderMock = () => ({ getOne })
|
const useDataProviderMock = () => ({ getMany })
|
||||||
let lastTime
|
let lastTime
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -69,7 +69,7 @@ describe('useResourceRefresh', () => {
|
|||||||
useResourceRefresh()
|
useResourceRefresh()
|
||||||
|
|
||||||
expect(refresh).toHaveBeenCalledTimes(1)
|
expect(refresh).toHaveBeenCalledTimes(1)
|
||||||
expect(getOne).not.toHaveBeenCalled()
|
expect(getMany).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('triggers a UI refresh when received an "any" id', () => {
|
it('triggers a UI refresh when received an "any" id', () => {
|
||||||
@@ -82,7 +82,7 @@ describe('useResourceRefresh', () => {
|
|||||||
useResourceRefresh()
|
useResourceRefresh()
|
||||||
|
|
||||||
expect(refresh).toHaveBeenCalledTimes(1)
|
expect(refresh).toHaveBeenCalledTimes(1)
|
||||||
expect(getOne).not.toHaveBeenCalled()
|
expect(getMany).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('triggers a refetch of the resources received', () => {
|
it('triggers a refetch of the resources received', () => {
|
||||||
@@ -95,11 +95,9 @@ describe('useResourceRefresh', () => {
|
|||||||
useResourceRefresh()
|
useResourceRefresh()
|
||||||
|
|
||||||
expect(refresh).not.toHaveBeenCalled()
|
expect(refresh).not.toHaveBeenCalled()
|
||||||
expect(getOne).toHaveBeenCalledTimes(4)
|
expect(getMany).toHaveBeenCalledTimes(2)
|
||||||
expect(getOne).toHaveBeenCalledWith('album', { id: 'al-1' })
|
expect(getMany).toHaveBeenCalledWith('album', { ids: ['al-1', 'al-2'] })
|
||||||
expect(getOne).toHaveBeenCalledWith('album', { id: 'al-2' })
|
expect(getMany).toHaveBeenCalledWith('song', { ids: ['sg-1', 'sg-2'] })
|
||||||
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-1' })
|
|
||||||
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-2' })
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -114,7 +112,7 @@ describe('useResourceRefresh', () => {
|
|||||||
useResourceRefresh('album')
|
useResourceRefresh('album')
|
||||||
|
|
||||||
expect(refresh).toHaveBeenCalledTimes(1)
|
expect(refresh).toHaveBeenCalledTimes(1)
|
||||||
expect(getOne).not.toHaveBeenCalled()
|
expect(getMany).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('triggers a refetch of the resources received if they are visible', () => {
|
it('triggers a refetch of the resources received if they are visible', () => {
|
||||||
@@ -127,9 +125,8 @@ describe('useResourceRefresh', () => {
|
|||||||
useResourceRefresh('song')
|
useResourceRefresh('song')
|
||||||
|
|
||||||
expect(refresh).not.toHaveBeenCalled()
|
expect(refresh).not.toHaveBeenCalled()
|
||||||
expect(getOne).toHaveBeenCalledTimes(2)
|
expect(getMany).toHaveBeenCalledTimes(1)
|
||||||
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-1' })
|
expect(getMany).toHaveBeenCalledWith('song', { ids: ['sg-1', 'sg-2'] })
|
||||||
expect(getOne).toHaveBeenCalledWith('song', { id: 'sg-2' })
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user