Skip to content

Commit

Permalink
refactor postRequest method, display image in iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
palys committed Feb 19, 2024
1 parent 9605a59 commit 52f693d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
10 changes: 6 additions & 4 deletions sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@
getData: async data => {
const result = await cadenzaClient.getData(data.dataType);
if (data.dataType === 'png') {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const imageBitmap = await createImageBitmap(result)
ctx.drawImage(imageBitmap, 0, 0, 300, 150);
const url = URL.createObjectURL(result);
try {
iframe.src = url;
} finally {
URL.revokeObjectURL(url);
}
}
}
};
Expand Down
27 changes: 3 additions & 24 deletions src/cadenza.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,27 +524,6 @@ export class CadenzaClient {
return promise;
}

/**
* @template [T=void]
* @param {string} type
* @param {unknown} [detail]
* @returns {Promise<T>}
*/
#postRequest(type, detail) {
/** @type {(() => void)[]} */
let unsubscribes;
const promise = new Promise((resolve, reject) => {
unsubscribes = [
this.#on(`${type}:success`, (event) => resolve(event.detail)),
this.#on(`${type}:error`, () => reject()),
];
});
promise.finally(() => unsubscribes.forEach((unsubscribe) => unsubscribe()));
this.#postEvent(type, detail);
return promise;
}


/**
* Subscribe to a `postMessage()` event.
*
Expand Down Expand Up @@ -617,14 +596,14 @@ export class CadenzaClient {
*/
#postRequest(/** @type string */ type, /** @type unknown */ detail) {
const { port1, port2 } = new MessageChannel();
/** @type {Promise<void>} */
/** @type {Promise<never>} */
const promise = new Promise((resolve, reject) => {
port1.onmessage = (
/** @type MessageEvent<CadenzaEvent<never, never>> */ event,
) => {
const cadenzaEvent = event.data;
if (cadenzaEvent.type === `${type}:success`) {
resolve();
resolve(cadenzaEvent.detail);
} else if (cadenzaEvent.type === `${type}:error`) {
reject();
}
Expand Down Expand Up @@ -691,7 +670,7 @@ export class CadenzaClient {
return res;
}

/**
/**
* Get data from the currently shown workbook view.
*
* Currently, only map views are supported.
Expand Down
2 changes: 1 addition & 1 deletion src/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ button.textContent = 'Download Excel';
button.onclick = () => cadenzaClient.downloadData('{embeddingTargetId}', 'excel');
```
### Request Data From a Workbook View via PostMessage
### Request Data From a Workbook View
<small>API: [CadenzaClient#getData](./classes/CadenzaClient.html#getData)</small>
Expand Down

0 comments on commit 52f693d

Please sign in to comment.