Bluesky app fork with some witchin' additions 💫

[Session] Persist updates from inactive agent

+22 -17
+15 -15
src/state/session/__tests__/session-test.ts
··· 872 872 expect(state.accounts[0].accessJwt).toBe('alice-access-jwt-3') 873 873 }) 874 874 875 - it('ignores updates from a stale agent', () => { 875 + it('accepts updates from a stale agent', () => { 876 876 let state = getInitialState([]) 877 877 878 878 const agent1 = new BskyAgent({service: 'https://alice.com'}) ··· 928 928 ]) 929 929 expect(state.accounts.length).toBe(2) 930 930 expect(state.accounts[1].did).toBe('alice-did') 931 - // Should retain the old values because Alice is not active. 932 - expect(state.accounts[1].handle).toBe('alice.test') 933 - expect(state.accounts[1].accessJwt).toBe('alice-access-jwt-1') 934 - expect(state.accounts[1].refreshJwt).toBe('alice-refresh-jwt-1') 931 + // Should update Alice's tokens because otherwise they'll be stale. 932 + expect(state.accounts[1].handle).toBe('alice-updated.test') 933 + expect(state.accounts[1].accessJwt).toBe('alice-access-jwt-2') 934 + expect(state.accounts[1].refreshJwt).toBe('alice-refresh-jwt-2') 935 935 expect(printState(state)).toMatchInlineSnapshot(` 936 936 { 937 937 "accounts": [ ··· 948 948 "service": "https://bob.com/", 949 949 }, 950 950 { 951 - "accessJwt": "alice-access-jwt-1", 951 + "accessJwt": "alice-access-jwt-2", 952 952 "deactivated": false, 953 953 "did": "alice-did", 954 - "email": undefined, 954 + "email": "alice@foo.bar", 955 955 "emailAuthFactor": false, 956 956 "emailConfirmed": false, 957 - "handle": "alice.test", 957 + "handle": "alice-updated.test", 958 958 "pdsUrl": undefined, 959 - "refreshJwt": "alice-refresh-jwt-1", 959 + "refreshJwt": "alice-refresh-jwt-2", 960 960 "service": "https://alice.com/", 961 961 }, 962 962 ], ··· 988 988 ]) 989 989 expect(state.accounts.length).toBe(2) 990 990 expect(state.accounts[0].did).toBe('bob-did') 991 - // Should update the values because Bob is active. 991 + // Should update Bob's tokens because otherwise they'll be stale. 992 992 expect(state.accounts[0].handle).toBe('bob-updated.test') 993 993 expect(state.accounts[0].accessJwt).toBe('bob-access-jwt-2') 994 994 expect(state.accounts[0].refreshJwt).toBe('bob-refresh-jwt-2') ··· 1008 1008 "service": "https://bob.com/", 1009 1009 }, 1010 1010 { 1011 - "accessJwt": "alice-access-jwt-1", 1011 + "accessJwt": "alice-access-jwt-2", 1012 1012 "deactivated": false, 1013 1013 "did": "alice-did", 1014 - "email": undefined, 1014 + "email": "alice@foo.bar", 1015 1015 "emailAuthFactor": false, 1016 1016 "emailConfirmed": false, 1017 - "handle": "alice.test", 1017 + "handle": "alice-updated.test", 1018 1018 "pdsUrl": undefined, 1019 - "refreshJwt": "alice-refresh-jwt-1", 1019 + "refreshJwt": "alice-refresh-jwt-2", 1020 1020 "service": "https://alice.com/", 1021 1021 }, 1022 1022 ], ··· 1030 1030 } 1031 1031 `) 1032 1032 1033 - // Ignore other events for inactive agent too. 1033 + // Ignore other events for inactive agent. 1034 1034 const lastState = state 1035 1035 agent1.session = undefined 1036 1036 state = run(state, [
+7 -2
src/state/session/reducer.ts
··· 68 68 switch (action.type) { 69 69 case 'received-agent-event': { 70 70 const {agent, accountDid, refreshedAccount, sessionEvent} = action 71 - if (agent !== state.currentAgentState.agent) { 72 - // Only consider events from the active agent. 71 + if ( 72 + refreshedAccount === undefined && 73 + agent !== state.currentAgentState.agent 74 + ) { 75 + // If the session got cleared out (e.g. due to expiry or network error) but 76 + // this account isn't the active one, don't clear it out at this time. 77 + // This way, if the problem is transient, it'll work on next resume. 73 78 return state 74 79 } 75 80 if (sessionEvent === 'network-error') {