Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(core): Prevent result cloning in Client (#3109)

authored by

Phil Pluckthun and committed by
GitHub
f0168aba 461d9702

+47 -39
+20 -2
packages/core/src/client.test.ts
··· 709 709 expect(resultTwo).toHaveBeenCalledWith({ 710 710 data: 1, 711 711 operation: queryOperation, 712 - stale: false, 712 + stale: true, 713 713 hasNext: false, 714 714 }); 715 715 716 716 vi.advanceTimersByTime(1); 717 717 718 718 // With cache-first we don't expect a new operation to be issued 719 - expect(resultTwo).toHaveBeenCalledTimes(1); 719 + expect(resultTwo).toHaveBeenCalledTimes(2); 720 720 }); 721 721 722 722 it('dispatches the correct request policy on subsequent sources', async () => { ··· 834 834 hasNext: false, 835 835 }); 836 836 837 + expect(resultOne).toHaveBeenCalledWith({ 838 + data: 1, 839 + operation, 840 + stale: true, 841 + hasNext: false, 842 + }); 843 + 844 + expect(resultTwo).toHaveBeenCalledTimes(1); 845 + expect(resultOne).toHaveBeenCalledTimes(2); 846 + 837 847 vi.advanceTimersByTime(1); 838 848 839 849 // With network-only we expect a new operation to be issued, hence a new result 840 850 expect(resultTwo).toHaveBeenCalledTimes(2); 851 + expect(resultOne).toHaveBeenCalledTimes(3); 841 852 842 853 expect(resultTwo).toHaveBeenCalledWith({ 854 + data: 2, 855 + operation, 856 + stale: false, 857 + hasNext: false, 858 + }); 859 + 860 + expect(resultOne).toHaveBeenCalledWith({ 843 861 data: 2, 844 862 operation, 845 863 stale: false,
+27 -37
packages/core/src/client.ts
··· 631 631 // Add `stale: true` flag when a new operation is sent for queries 632 632 switchMap(result => { 633 633 const value$ = fromValue(result); 634 - return result.stale 634 + return result.stale || result.hasNext 635 635 ? value$ 636 636 : merge([ 637 637 value$, 638 638 pipe( 639 639 operations.source, 640 - filter( 641 - op => 642 - op.kind === 'query' && 643 - op.key === operation.key && 644 - op.context.requestPolicy !== 'cache-only' 645 - ), 640 + filter(op => op.key === operation.key), 646 641 take(1), 647 - map(() => ({ ...result, stale: true })) 642 + map(() => { 643 + result.stale = true; 644 + return result; 645 + }) 648 646 ), 649 647 ]); 650 648 }) ··· 750 748 active.set(operation.key, (source = makeResultSource(operation))); 751 749 } 752 750 753 - const isNetworkOperation = 754 - operation.context.requestPolicy === 'cache-and-network' || 755 - operation.context.requestPolicy === 'network-only'; 751 + source = pipe( 752 + source, 753 + onStart(() => { 754 + dispatchOperation(operation); 755 + }) 756 + ); 757 + 756 758 const replay = replays.get(operation.key); 757 - 758 - if (operation.kind !== 'query' || !replay || isNetworkOperation) { 759 - source = pipe( 760 - source, 761 - onStart(() => { 762 - dispatchOperation(operation); 763 - }) 759 + if ( 760 + operation.kind === 'query' && 761 + replay && 762 + (replay.stale || replay.hasNext) 763 + ) { 764 + return pipe( 765 + merge([ 766 + source, 767 + pipe( 768 + fromValue(replay), 769 + filter(replay => replay === replays.get(operation.key)) 770 + ), 771 + ]), 772 + switchMap(fromValue) 764 773 ); 765 - } 766 - 767 - if (operation.kind === 'query' && replay) { 768 - return merge([ 769 - source, 770 - pipe( 771 - fromValue(replay), 772 - filter(replay => { 773 - if (replay === replays.get(operation.key)) { 774 - if (isNetworkOperation && !replay.hasNext) 775 - replay.stale = true; 776 - return true; 777 - } else { 778 - if (!isNetworkOperation) dispatchOperation(operation); 779 - return false; 780 - } 781 - }) 782 - ), 783 - ]); 784 774 } else { 785 775 return source; 786 776 }