File size: 4,630 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import debugFactory from 'debug';
import { convertTempResponseCartToResponseCart } from './cart-functions';
import { getEmptyResponseCart } from './empty-carts';
import type {
	ShoppingCartManager,
	ShoppingCartState,
	SavedActionPromise,
	ShoppingCartManagerState,
	ShoppingCartManagerGetState,
	SubscribeCallback,
	UnsubscribeFunction,
	SubscriptionManager,
	ActionPromises,
	ShoppingCartManagerActions,
	CartKey,
} from './types';

const debug = debugFactory( 'shopping-cart:managers' );

export function getShoppingCartManagerState( state: ShoppingCartState ): ShoppingCartManagerState {
	const {
		cacheStatus,
		queuedActions,
		couponStatus,
		loadingErrorType,
		loadingError,
		lastValidResponseCart,
	} = state;
	const isLoading = cacheStatus === 'fresh' || cacheStatus === 'fresh-pending';
	const isPendingUpdate = queuedActions.length > 0 || cacheStatus !== 'valid';
	const loadingErrorForManager = cacheStatus === 'error' ? loadingError : null;
	debug(
		'manager isLoading',
		isLoading,
		'isPendingUpdate',
		isPendingUpdate,
		'loadingError',
		loadingErrorForManager
	);

	return {
		isLoading,
		loadingError: loadingErrorForManager,
		loadingErrorType,
		isPendingUpdate,
		couponStatus,
		responseCart: lastValidResponseCart,
	};
}

export function createSubscriptionManager( cartKey: CartKey | undefined ): SubscriptionManager {
	let subscribedClients: SubscribeCallback[] = [];
	const subscribe = ( callback: SubscribeCallback ): UnsubscribeFunction => {
		debug( `adding subscriber for cartKey ${ cartKey }` );
		subscribedClients.push( callback );
		return () => {
			debug( `removing subscriber for cartKey ${ cartKey }` );
			subscribedClients = subscribedClients.filter( ( prevCallback ) => prevCallback !== callback );
		};
	};
	const notifySubscribers = () => {
		debug( `notifying ${ subscribedClients.length } subscribers for cartKey ${ cartKey }` );
		subscribedClients.forEach( ( clientCallback ) => {
			try {
				clientCallback();
			} catch ( err ) {
				// eslint-disable-next-line no-console
				console.error(
					'An error ocurred while notifying a subscriber of a ShoppingCartManager change',
					( err as Error ).message
				);
				throw err;
			}
		} );
		debug( `completed notification of subscribers for cartKey ${ cartKey }` );
	};

	return { subscribe, notifySubscribers };
}

/**
 * Create an object that manages the Promises returned by cart actions.
 *
 * When an action is requested, we create a Promise for that action dispatcher
 * to return and store it in this object. Later when we know if the action
 * resulted in a success or a failure, the resolve or reject methods are called
 * on this object; those methods then resolve or reject each Promise that was
 * waiting for a response.
 */
export function createActionPromisesManager(): ActionPromises {
	let actionPromises: SavedActionPromise[] = [];

	return {
		resolve( tempResponseCart ) {
			if ( actionPromises.length > 0 ) {
				debug( `resolving ${ actionPromises.length } action promises` );
				const responseCart = convertTempResponseCartToResponseCart( tempResponseCart );
				actionPromises.forEach( ( actionPromise ) => actionPromise.resolve( responseCart ) );
				actionPromises = [];
			}
		},

		reject( error ) {
			if ( actionPromises.length > 0 ) {
				debug( `rejecting ${ actionPromises.length } action promises` );
				actionPromises.forEach( ( actionPromise ) => actionPromise.reject( error ) );
				actionPromises = [];
			}
		},

		add( actionPromise: SavedActionPromise ) {
			actionPromises.push( actionPromise );
		},
	};
}

const emptyCart = getEmptyResponseCart();
const noopCartAction = (): ReturnType< ShoppingCartManagerActions[ 'addProductsToCart' ] > =>
	Promise.reject( 'Cart actions cannot be taken without a cart key.' );
const noopState: ShoppingCartManagerState = {
	isLoading: true,
	loadingError: undefined,
	loadingErrorType: undefined,
	isPendingUpdate: true,
	couponStatus: 'fresh',
	responseCart: emptyCart,
};
const noopGetState: ShoppingCartManagerGetState = () => noopState;
const noopActions: ShoppingCartManagerActions = {
	addProductsToCart: noopCartAction,
	removeProductFromCart: noopCartAction,
	applyCoupon: noopCartAction,
	removeCoupon: noopCartAction,
	updateLocation: noopCartAction,
	replaceProductInCart: noopCartAction,
	replaceProductsInCart: noopCartAction,
	reloadFromServer: () => Promise.resolve( emptyCart ),
	clearMessages: () => Promise.resolve( emptyCart ),
};
export const noopManager: ShoppingCartManager = {
	actions: noopActions,
	getState: noopGetState,
	subscribe: () => () => null,
	fetchInitialCart: () => Promise.resolve( emptyCart ),
};