Skip to content

Commit

Permalink
Unify ims init and fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
karlpauls committed Feb 14, 2024
1 parent adc18d9 commit f482782
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
5 changes: 3 additions & 2 deletions blocks/aec-shell/aec-shell.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { LitElement, html } from '../../deps/lit/lit-core.min.js';
import getSheet from '../shared/sheet.js';
import { initIms } from '../shared/utils.js';

const sheet = await getSheet('/blocks/aec-shell/aec-shell-wc.css');

// Milo Imports
const { getLibs } = await import('../../scripts/utils.js');
const { getConfig, loadIms } = await import(`${getLibs()}/utils/utils.js`);
const { getConfig } = await import(`${getLibs()}/utils/utils.js`);

class AECShell extends LitElement {
static properties = {
Expand All @@ -21,7 +22,7 @@ class AECShell extends LitElement {
connectedCallback() {
super.connectedCallback();
this.shadowRoot.adoptedStyleSheets = [sheet];
loadIms().then(() => { this.imsReady(); });
initIms().then(() => { this.imsReady(); });
}

async imsReady() {
Expand Down
13 changes: 6 additions & 7 deletions blocks/edit/da-editor/da-editor.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { LitElement, html } from '../../../deps/lit/lit-core.min.js';
import initProse from '../prose/index.js';
import getSheet from '../../shared/sheet.js';
import { initIms } from '../../shared/utils.js';

const sheet = await getSheet('/blocks/edit/da-editor/da-editor.css');

// Milo Imports
const { getLibs } = await import('../../../scripts/utils.js');
const { loadIms } = await import(`${getLibs()}/utils/utils.js`);

export default class DaEditor extends LitElement {
static properties = {
path: {},
Expand All @@ -18,9 +15,11 @@ export default class DaEditor extends LitElement {
super.connectedCallback();
this.shadowRoot.adoptedStyleSheets = [sheet];
this.shadowRoot.createRange = () => document.createRange();
loadIms().then(() => {
this._imsLoaded = true;
});
if (!this._imsLoaded) {
initIms().then(() => {
this._imsLoaded = true;
});
}
}

attributeChangedCallback(name, _old, value) {
Expand Down
6 changes: 3 additions & 3 deletions blocks/edit/prose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export default function initProse({ editor, path }) {

const opts = {};

if (window.adobeIMS.isSignedInUser()) {
opts.params = { Authorization: `Bearer ${window.adobeIMS.getAccessToken()?.token}` };
if (window.adobeIMS?.isSignedInUser()) {
opts.params = { Authorization: `Bearer ${window.adobeIMS.getAccessToken().token}` };
}

const wsProvider = new WebsocketProvider(server, roomName, ydoc, opts);
Expand Down Expand Up @@ -122,7 +122,7 @@ export default function initProse({ editor, path }) {
}
});

if (window.adobeIMS.isSignedInUser()) {
if (window.adobeIMS?.isSignedInUser()) {
window.adobeIMS.getProfile().then(
(profile) => {
wsProvider.awareness.setLocalStateField('user', { color: '#008833', name: profile.displayName });
Expand Down
5 changes: 4 additions & 1 deletion blocks/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DA_ORIGIN } from './constants.js';

let imsLoaded;

export const daFetch = async (url, opts = {}) => {
export async function initIms() {
if (!imsLoaded) {
const { getLibs } = await import('../../scripts/utils.js');
const { loadIms } = await import(`${getLibs()}/utils/utils.js`);
Expand All @@ -13,7 +13,10 @@ export const daFetch = async (url, opts = {}) => {
/* die silently */
}
}
}

export const daFetch = async (url, opts = {}) => {
await initIms();
const accessToken = window.adobeIMS?.getAccessToken();
opts.headers = opts.headers || {};
if (accessToken) {
Expand Down

0 comments on commit f482782

Please sign in to comment.