Skip to content

Commit

Permalink
feat: add waitForAssets method
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalsadhu committed Nov 12, 2024
1 parent fa85f8a commit c4aaaf2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
16 changes: 13 additions & 3 deletions lib/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { EventEmitter } from 'node:events';
export class Assets extends EventEmitter {
#expectedAssets = new Set();
#receivedAssets = new Map();
#js;
#css;

// pointless constructor with super to appease TS which cryptically errors otherwise.
constructor() {
Expand All @@ -29,12 +31,20 @@ export class Assets extends EventEmitter {
this.#receivedAssets.set(name, assets);
if (this.allAssetsReceived) {
const assets = Array.from(this.#receivedAssets.values());
const js = assets.flatMap(({ js = [] }) => js).filter(Boolean);
const css = assets.flatMap(({ css = [] }) => css).filter(Boolean);
this.emit('received', { js, css });
this.#js = assets.flatMap(({ js = [] }) => js).filter(Boolean);
this.#css = assets.flatMap(({ css = [] }) => css).filter(Boolean);
this.emit('received', { js: this.#js, css: this.#css });
}
}

get js() {
return this.#js;
}

get css() {
return this.#css;
}

/**
* Value will be true if all expected assets have been received
*/
Expand Down
19 changes: 19 additions & 0 deletions lib/http-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ export default class HttpIncoming {
return this.#js;
}

/**
* Wait for assets to be received from

Check failure on line 226 in lib/http-incoming.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Delete `·`
* @returns {Promise<{js: Array<import('./asset-js.js').JavaScriptAsset | string>, css: Array<import('./asset-css.js').CssAsset | string>}>}
*/
waitForAssets() {
if (this.assets.allAssetsReceived) {
return Promise.resolve({
js: this.assets.js,
css: this.assets.css,
});
}

return new Promise((resolve) => {
this.assets.on('received', (assets) => {
resolve(assets);
});
});
}

/**
* @returns {PodiumHttpIncoming<T>}
*/
Expand Down
54 changes: 51 additions & 3 deletions tests/http-incoming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,8 @@ tap.test('can read values from view with default types', (t) => {
t.end();
});

tap.test('can read values from view with default types', (t) => {
tap.test('can set asset expectations and receive assets', (t) => {
t.plan(1);
// really only here for tsc

/**
* @type {HttpIncoming}
*/
Expand All @@ -384,3 +382,53 @@ tap.test('can read values from view with default types', (t) => {
css: [{ value: 'foo.css' }],
});
});

tap.test('can wait for expected assets', (t) => {
t.plan(1);
/**
* @type {HttpIncoming}
*/
const incoming = new HttpIncoming(SIMPLE_REQ, SIMPLE_RES);
incoming.assets.addExpectedAsset('foo');
incoming.assets.addExpectedAsset('bar');

incoming.waitForAssets().then(() => {
t.ok(incoming.assets.allAssetsReceived);
t.end();
});

incoming.assets.addReceivedAsset('foo', {
js: [{ value: 'foo.js' }],
css: [{ value: 'foo.css' }],
});
incoming.assets.addReceivedAsset('bar', {
js: [{ value: 'foo.js' }],
css: [{ value: 'foo.css' }],
});
});

tap.test('waitForAssets will resolve even if all assets have already been received', async (t) => {

Check failure on line 410 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `'waitForAssets·will·resolve·even·if·all·assets·have·already·been·received',` with `⏎····'waitForAssets·will·resolve·even·if·all·assets·have·already·been·received',⏎···`
t.plan(3);

Check failure on line 411 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `····` with `········`
/**

Check failure on line 412 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Insert `····`
* @type {HttpIncoming}

Check failure on line 413 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `·····` with `·········`
*/

Check failure on line 414 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `·····` with `·········`
const incoming = new HttpIncoming(SIMPLE_REQ, SIMPLE_RES);

Check failure on line 415 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Insert `····`
incoming.assets.addExpectedAsset('foo');

Check failure on line 416 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Insert `····`
incoming.assets.addExpectedAsset('bar');

Check failure on line 417 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `····` with `········`

incoming.assets.addReceivedAsset('foo', {

Check failure on line 419 in tests/http-incoming.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Replace `····` with `········`
js: [{ value: 'foo.js' }],
css: [{ value: 'foo.css' }],
});
incoming.assets.addReceivedAsset('bar', {
js: [{ value: 'foo.js' }],
css: [{ value: 'foo.css' }],
});

const { js, css } = await incoming.waitForAssets();

t.ok(incoming.assets.allAssetsReceived);
t.same(js, [{ value: 'foo.js' }, { value: 'foo.js' }]);
t.same(css, [{ value: 'foo.css' }, { value: 'foo.css' }]);
t.end();
});

0 comments on commit c4aaaf2

Please sign in to comment.