Skip to content

🎧 Your Personal Streaming Service

Refech using getMany, reducing the number of API calls

Deluanfb7229a53e64
diff --git a/server/subsonic/media_annotation.go b/server/subsonic/media_annotation.go
index 77969b530..36ee28d86 100644
--- a/server/subsonic/media_annotation.go
+++ b/server/subsonic/media_annotation.go
@@ -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
diff --git a/ui/src/common/useResourceRefresh.js b/ui/src/common/useResourceRefresh.js
index e12aa26b0..4c2008089 100644
--- a/ui/src/common/useResourceRefresh.js
+++ b/ui/src/common/useResourceRefresh.js
@@ -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] })
+ }
}
})
}
diff --git a/ui/src/common/useResourceRefresh.test.js b/ui/src/common/useResourceRefresh.test.js
index 248405ab1..fca8ddfcd 100644
--- a/ui/src/common/useResourceRefresh.test.js
+++ b/ui/src/common/useResourceRefresh.test.js
@@ -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'] })
})
})
})