Skip to content

Commit

Permalink
refactor: change init order to defer DOM manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Jan 29, 2020
1 parent ef1dc5a commit eb04752
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/StripeBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitElement, property, html } from 'lit-element';
import { LitNotify } from '@morbidick/lit-element-notify';

import { ifDefined } from 'lit-html/directives/if-defined';

import bound from 'bound-decorator';

import { ReadOnlyPropertiesMixin } from './lib/read-only-properties';
Expand Down Expand Up @@ -377,29 +378,29 @@ export class StripeBase extends ReadOnlyPropertiesMixin(LitNotify(LitElement)) {
else return response;
}

/**
* Removes all mount points from the DOM
* @private
*/
destroyMountPoints() {
this.shadowHosts.forEach(removeAllMounts);
if (this.stripeMount) this.stripeMount.remove();
}

/**
* Reinitializes Stripe and mounts the element.
* @private
*/
async init() {
await this.unmount();
this.destroyMountPoints();
this.initMountPoints();
await this.initStripe();
await this.initElement();
this.initElementListeners();
this.destroyMountPoints();
this.initMountPoints();
this.mount();
}

/**
* Removes all mount points from the DOM
* @private
*/
destroyMountPoints() {
this.shadowHosts.forEach(removeAllMounts);
if (this.stripeMount) this.stripeMount.remove();
}

/**
* Adds `ready`, `focus`, and `blur` listeners to the Stripe Element
* @private
Expand Down
1 change: 1 addition & 0 deletions src/stripe-elements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ describe('<stripe-elements>', function() {
beforeEach(removeStripeMount);
describe('then publishable key is set', function() {
beforeEach(setProps({ publishableKey: PUBLISHABLE_KEY }));
beforeEach(nextFrame);
it('rebuilds its DOM', function() {
const { stripeMountId } = element;
expect(element).lightDom.to.equal(expectedLightDOM({ stripeMountId }));
Expand Down
1 change: 1 addition & 0 deletions src/stripe-payment-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ describe('<stripe-payment-request>', function() {
beforeEach(removeStripeMount);
describe('then publishable key is set', function() {
beforeEach(setProps({ publishableKey: PUBLISHABLE_KEY }));
beforeEach(nextFrame);
it('rebuilds its DOM', function() {
const { stripeMountId } = element;
expect(element).lightDom.to.equal(expectedLightDOM({ stripeMountId }));
Expand Down

0 comments on commit eb04752

Please sign in to comment.