From fb7229a53e64d55c518791335c3cfa19718f50e0 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 16 Jun 2021 10:00:31 -0400 Subject: [PATCH] Refech using getMany, reducing the number of API calls --- server/subsonic/media_annotation.go | 12 ++++++------ ui/src/common/useResourceRefresh.js | 6 +++--- ui/src/common/useResourceRefresh.test.js | 23 ++++++++++------------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/server/subsonic/media_annotation.go b/server/subsonic/media_annotation.go index 77969b53..36ee28d8 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 e12aa26b..4c200808 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 248405ab..fca8ddfc 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'] }) }) }) })