forked from mirrors/gecko-dev
		
	 c0d0384218
			
		
	
	
		c0d0384218
		
	
	
	
	
		
			
			MozReview-Commit-ID: LjrRXr1thc8 --HG-- rename : toolkit/components/payments/.eslintrc.js => browser/components/payments/.eslintrc.js rename : toolkit/components/payments/content/paymentDialogFrameScript.js => browser/components/payments/content/paymentDialogFrameScript.js rename : toolkit/components/payments/content/paymentDialogWrapper.css => browser/components/payments/content/paymentDialogWrapper.css rename : toolkit/components/payments/content/paymentDialogWrapper.js => browser/components/payments/content/paymentDialogWrapper.js rename : toolkit/components/payments/content/paymentDialogWrapper.xhtml => browser/components/payments/content/paymentDialogWrapper.xhtml rename : toolkit/components/payments/docs/index.rst => browser/components/payments/docs/index.rst rename : toolkit/components/payments/jar.mn => browser/components/payments/jar.mn rename : toolkit/components/payments/moz.build => browser/components/payments/moz.build rename : toolkit/components/payments/paymentUIService.js => browser/components/payments/paymentUIService.js rename : toolkit/components/payments/payments.manifest => browser/components/payments/payments.manifest rename : toolkit/components/payments/res/PaymentsStore.js => browser/components/payments/res/PaymentsStore.js rename : toolkit/components/payments/res/components/address-option.css => browser/components/payments/res/components/address-option.css rename : toolkit/components/payments/res/components/address-option.js => browser/components/payments/res/components/address-option.js rename : toolkit/components/payments/res/components/basic-card-option.css => browser/components/payments/res/components/basic-card-option.css rename : toolkit/components/payments/res/components/basic-card-option.js => browser/components/payments/res/components/basic-card-option.js rename : toolkit/components/payments/res/components/currency-amount.js => browser/components/payments/res/components/currency-amount.js rename : toolkit/components/payments/res/components/labelled-checkbox.js => browser/components/payments/res/components/labelled-checkbox.js rename : toolkit/components/payments/res/components/payment-details-item.css => browser/components/payments/res/components/payment-details-item.css rename : toolkit/components/payments/res/components/payment-details-item.js => browser/components/payments/res/components/payment-details-item.js rename : toolkit/components/payments/res/components/rich-option.js => browser/components/payments/res/components/rich-option.js rename : toolkit/components/payments/res/components/rich-select.css => browser/components/payments/res/components/rich-select.css rename : toolkit/components/payments/res/components/rich-select.js => browser/components/payments/res/components/rich-select.js rename : toolkit/components/payments/res/components/shipping-option.css => browser/components/payments/res/components/shipping-option.css rename : toolkit/components/payments/res/components/shipping-option.js => browser/components/payments/res/components/shipping-option.js rename : toolkit/components/payments/res/containers/address-picker.js => browser/components/payments/res/containers/address-picker.js rename : toolkit/components/payments/res/containers/basic-card-form.js => browser/components/payments/res/containers/basic-card-form.js rename : toolkit/components/payments/res/containers/order-details.css => browser/components/payments/res/containers/order-details.css rename : toolkit/components/payments/res/containers/order-details.js => browser/components/payments/res/containers/order-details.js rename : toolkit/components/payments/res/containers/payment-dialog.js => browser/components/payments/res/containers/payment-dialog.js rename : toolkit/components/payments/res/containers/payment-method-picker.js => browser/components/payments/res/containers/payment-method-picker.js rename : toolkit/components/payments/res/containers/shipping-option-picker.js => browser/components/payments/res/containers/shipping-option-picker.js rename : toolkit/components/payments/res/debugging.css => browser/components/payments/res/debugging.css rename : toolkit/components/payments/res/debugging.html => browser/components/payments/res/debugging.html rename : toolkit/components/payments/res/debugging.js => browser/components/payments/res/debugging.js rename : toolkit/components/payments/res/mixins/ObservedPropertiesMixin.js => browser/components/payments/res/mixins/ObservedPropertiesMixin.js rename : toolkit/components/payments/res/mixins/PaymentStateSubscriberMixin.js => browser/components/payments/res/mixins/PaymentStateSubscriberMixin.js rename : toolkit/components/payments/res/paymentRequest.css => browser/components/payments/res/paymentRequest.css rename : toolkit/components/payments/res/paymentRequest.js => browser/components/payments/res/paymentRequest.js rename : toolkit/components/payments/res/paymentRequest.xhtml => browser/components/payments/res/paymentRequest.xhtml rename : toolkit/components/payments/res/unprivileged-fallbacks.js => browser/components/payments/res/unprivileged-fallbacks.js rename : toolkit/components/payments/res/vendor/custom-elements.min.js => browser/components/payments/res/vendor/custom-elements.min.js rename : toolkit/components/payments/res/vendor/custom-elements.min.js.map => browser/components/payments/res/vendor/custom-elements.min.js.map rename : toolkit/components/payments/server.py => browser/components/payments/server.py rename : toolkit/components/payments/test/PaymentTestUtils.jsm => browser/components/payments/test/PaymentTestUtils.jsm rename : toolkit/components/payments/test/browser/.eslintrc.js => browser/components/payments/test/browser/.eslintrc.js rename : toolkit/components/payments/test/browser/blank_page.html => browser/components/payments/test/browser/blank_page.html rename : toolkit/components/payments/test/browser/browser.ini => browser/components/payments/test/browser/browser.ini rename : toolkit/components/payments/test/browser/browser_card_edit.js => browser/components/payments/test/browser/browser_card_edit.js rename : toolkit/components/payments/test/browser/browser_change_shipping.js => browser/components/payments/test/browser/browser_change_shipping.js rename : toolkit/components/payments/test/browser/browser_host_name.js => browser/components/payments/test/browser/browser_host_name.js rename : toolkit/components/payments/test/browser/browser_profile_storage.js => browser/components/payments/test/browser/browser_profile_storage.js rename : toolkit/components/payments/test/browser/browser_request_serialization.js => browser/components/payments/test/browser/browser_request_serialization.js rename : toolkit/components/payments/test/browser/browser_request_shipping.js => browser/components/payments/test/browser/browser_request_shipping.js rename : toolkit/components/payments/test/browser/browser_request_summary.js => browser/components/payments/test/browser/browser_request_summary.js rename : toolkit/components/payments/test/browser/browser_shippingaddresschange_error.js => browser/components/payments/test/browser/browser_shippingaddresschange_error.js rename : toolkit/components/payments/test/browser/browser_show_dialog.js => browser/components/payments/test/browser/browser_show_dialog.js rename : toolkit/components/payments/test/browser/browser_total.js => browser/components/payments/test/browser/browser_total.js rename : toolkit/components/payments/test/browser/head.js => browser/components/payments/test/browser/head.js rename : toolkit/components/payments/test/mochitest/.eslintrc.js => browser/components/payments/test/mochitest/.eslintrc.js rename : toolkit/components/payments/test/mochitest/formautofill/mochitest.ini => browser/components/payments/test/mochitest/formautofill/mochitest.ini rename : toolkit/components/payments/test/mochitest/formautofill/test_editCreditCard.html => browser/components/payments/test/mochitest/formautofill/test_editCreditCard.html rename : toolkit/components/payments/test/mochitest/mochitest.ini => browser/components/payments/test/mochitest/mochitest.ini rename : toolkit/components/payments/test/mochitest/payments_common.js => browser/components/payments/test/mochitest/payments_common.js rename : toolkit/components/payments/test/mochitest/test_ObservedPropertiesMixin.html => browser/components/payments/test/mochitest/test_ObservedPropertiesMixin.html rename : toolkit/components/payments/test/mochitest/test_PaymentStateSubscriberMixin.html => browser/components/payments/test/mochitest/test_PaymentStateSubscriberMixin.html rename : toolkit/components/payments/test/mochitest/test_PaymentsStore.html => browser/components/payments/test/mochitest/test_PaymentsStore.html rename : toolkit/components/payments/test/mochitest/test_address_picker.html => browser/components/payments/test/mochitest/test_address_picker.html rename : toolkit/components/payments/test/mochitest/test_basic_card_form.html => browser/components/payments/test/mochitest/test_basic_card_form.html rename : toolkit/components/payments/test/mochitest/test_currency_amount.html => browser/components/payments/test/mochitest/test_currency_amount.html rename : toolkit/components/payments/test/mochitest/test_labelled_checkbox.html => browser/components/payments/test/mochitest/test_labelled_checkbox.html rename : toolkit/components/payments/test/mochitest/test_order_details.html => browser/components/payments/test/mochitest/test_order_details.html rename : toolkit/components/payments/test/mochitest/test_payer_address_picker.html => browser/components/payments/test/mochitest/test_payer_address_picker.html rename : toolkit/components/payments/test/mochitest/test_payment_details_item.html => browser/components/payments/test/mochitest/test_payment_details_item.html rename : toolkit/components/payments/test/mochitest/test_payment_dialog.html => browser/components/payments/test/mochitest/test_payment_dialog.html rename : toolkit/components/payments/test/mochitest/test_payment_method_picker.html => browser/components/payments/test/mochitest/test_payment_method_picker.html rename : toolkit/components/payments/test/mochitest/test_rich_select.html => browser/components/payments/test/mochitest/test_rich_select.html rename : toolkit/components/payments/test/mochitest/test_shipping_option_picker.html => browser/components/payments/test/mochitest/test_shipping_option_picker.html rename : toolkit/components/payments/test/unit/.eslintrc.js => browser/components/payments/test/unit/.eslintrc.js rename : toolkit/components/payments/test/unit/head.js => browser/components/payments/test/unit/head.js rename : toolkit/components/payments/test/unit/test_response_creation.js => browser/components/payments/test/unit/test_response_creation.js rename : toolkit/components/payments/test/unit/xpcshell.ini => browser/components/payments/test/unit/xpcshell.ini
		
			
				
	
	
		
			97 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* This Source Code Form is subject to the terms of the Mozilla Public
 | |
|  * License, v. 2.0. If a copy of the MPL was not distributed with this
 | |
|  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | |
| 
 | |
| /**
 | |
|  * The PaymentsStore class provides lightweight storage with an async publish/subscribe mechanism.
 | |
|  * Synchronous state changes are batched to improve application performance and to reduce partial
 | |
|  * state propagation.
 | |
|  */
 | |
| 
 | |
| export default class PaymentsStore {
 | |
|   /**
 | |
|    * @param {object} [defaultState = {}] The initial state of the store.
 | |
|    */
 | |
|   constructor(defaultState = {}) {
 | |
|     this._defaultState = Object.assign({}, defaultState);
 | |
|     this._state = defaultState;
 | |
|     this._nextNotifification = 0;
 | |
|     this._subscribers = new Set();
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Get the current state as a shallow clone with a shallow freeze.
 | |
|    * You shouldn't modify any part of the returned state object as that would bypass notifying
 | |
|    * subscribers and could lead to subscribers assuming old state.
 | |
|    *
 | |
|    * @returns {Object} containing the current state
 | |
|    */
 | |
|   getState() {
 | |
|     return Object.freeze(Object.assign({}, this._state));
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Used for testing to reset to the default state from the constructor.
 | |
|    * @returns {Promise} returned by setState.
 | |
|    */
 | |
|   async reset() {
 | |
|     return this.setState(this._defaultState);
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Augment the current state with the keys of `obj` and asynchronously notify
 | |
|    * state subscribers. As a result, multiple synchronous state changes will lead
 | |
|    * to a single subscriber notification which leads to better performance and
 | |
|    * reduces partial state changes.
 | |
|    *
 | |
|    * @param {Object} obj The object to augment the state with. Keys in the object
 | |
|    *                     will be shallow copied with Object.assign.
 | |
|    *
 | |
|    * @example If the state is currently {a:3} then setState({b:"abc"}) will result in a state of
 | |
|    *          {a:3, b:"abc"}.
 | |
|    */
 | |
|   async setState(obj) {
 | |
|     Object.assign(this._state, obj);
 | |
|     let thisChangeNum = ++this._nextNotifification;
 | |
| 
 | |
|     // Let any synchronous setState calls that happen after the current setState call
 | |
|     // complete first.
 | |
|     // Their effects on the state will be batched up before the callback is actually called below.
 | |
|     await Promise.resolve();
 | |
| 
 | |
|     // Don't notify for state changes that are no longer the most recent. We only want to call the
 | |
|     // callback once with the latest state.
 | |
|     if (thisChangeNum !== this._nextNotifification) {
 | |
|       return;
 | |
|     }
 | |
| 
 | |
|     for (let subscriber of this._subscribers) {
 | |
|       try {
 | |
|         subscriber.stateChangeCallback(this.getState());
 | |
|       } catch (ex) {
 | |
|         console.error(ex);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Subscribe the object to state changes notifications via a `stateChangeCallback` method.
 | |
|    *
 | |
|    * @param {Object} component to receive state change callbacks via a `stateChangeCallback` method.
 | |
|    *                           If the component is already subscribed, do nothing.
 | |
|    */
 | |
|   subscribe(component) {
 | |
|     if (this._subscribers.has(component)) {
 | |
|       return;
 | |
|     }
 | |
| 
 | |
|     this._subscribers.add(component);
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * @param {Object} component to stop receiving state change callbacks.
 | |
|    */
 | |
|   unsubscribe(component) {
 | |
|     this._subscribers.delete(component);
 | |
|   }
 | |
| }
 |