Skip to content
Source
Sign inSign up
Open History to see the last commit

incoming-share-presentation.ts

66 lines1.8 KB

1export interface IncomingSharePresentationState {
2 readonly presentedShareId: string | null;
3 readonly dismissedShareId: string | null;
4}
5​
6export interface IncomingSharePresentationTransition {
7 readonly state: IncomingSharePresentationState;
8 readonly shareIdToPresent: string | null;
9}
10​
11export const EMPTY_INCOMING_SHARE_PRESENTATION_STATE: IncomingSharePresentationState = {
12 presentedShareId: null,
13 dismissedShareId: null,
14};
15​
16export function transitionIncomingSharePresentation(
17 state: IncomingSharePresentationState,
18 input: {
19 readonly isShareSheetPresented: boolean;
20 readonly pendingShareId: string | null;
21 },
22): IncomingSharePresentationTransition {
23 if (input.isShareSheetPresented) {
24 if (state.presentedShareId !== null && input.pendingShareId !== state.presentedShareId) {
25 // Forget consumed presentations immediately so later handoffs can reuse their ids.
26 return {
27 state: EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
28 shareIdToPresent: null,
29 };
30 }
31 return { state, shareIdToPresent: null };
32 }
33​
34 let nextState = state;
35 if (state.presentedShareId !== null) {
36 if (input.pendingShareId === state.presentedShareId) {
37 return {
38 state: {
39 presentedShareId: null,
40 dismissedShareId: state.presentedShareId,
41 },
42 shareIdToPresent: null,
43 };
44 }
45 nextState = { ...state, presentedShareId: null };
46 }
47​
48 if (input.pendingShareId === null) {
49 return {
50 state: EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
51 shareIdToPresent: null,
52 };
53 }
54​
55 if (nextState.dismissedShareId === input.pendingShareId) {
56 return { state: nextState, shareIdToPresent: null };
57 }
58​
59 return {
60 state: {
61 presentedShareId: input.pendingShareId,
62 dismissedShareId: null,
63 },
64 shareIdToPresent: input.pendingShareId,
65 };