Skip to content

Commit

Permalink
Merge pull request #9 from puzzle-js/feature/render-async-fragment
Browse files Browse the repository at this point in the history
renderAsyncFragment added to core
  • Loading branch information
Acanguven authored Apr 21, 2020
2 parents 37ed5a4 + 5604d91 commit 56190d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@puzzle-js/client-lib",
"main": "dist/index.js",
"version": "1.2.4",
"version": "1.3.0",
"author": "<[email protected]>",
"license": "MIT",
"repository": {
Expand Down
21 changes: 17 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ export class Core extends Module {
}

private static asyncLoadFragment(fragment: IPageFragmentConfig) {
if (fragment.asyncLoaded) return;
fragment.asyncLoaded = true;
const queryString = this.prepareQueryString(fragment.attributes);
const key = `${fragment.source}${location.pathname}${queryString}`;
const key = `${fragment.source}${window.location.pathname}${queryString}`;

if (!fragment.asyncDecentralized) {
return this.fetchGatewayFragment(fragment)
.then(res => this.asyncRenderResponse(fragment, res));
}


Core.gun
.get(key, (gunResponse: any) => {
if (gunResponse.err || !gunResponse.put) {
Expand Down Expand Up @@ -141,7 +142,7 @@ export class Core extends Module {

private static fetchGatewayFragment(fragment: IPageFragmentConfig) {
const queryString = this.prepareQueryString(fragment.attributes);
const fragmentRequestUrl = `${fragment.source}${location.pathname}${queryString}`;
const fragmentRequestUrl = `${fragment.source}${window.location.pathname}${queryString}`;
return fetch(fragmentRequestUrl, {
credentials: 'include'
})
Expand Down Expand Up @@ -183,7 +184,7 @@ export class Core extends Module {
}

private static prepareQueryString(fragmentAttributes: Record<string, string>) {
const attributes = Object.assign(location.search.slice(1).split('&').reduce((dict: { [name: string]: string }, i) => {
const attributes = Object.assign(window.location.search.slice(1).split('&').reduce((dict: { [name: string]: string }, i) => {
const [key, value] = i.split('=');
if (typeof value !== "undefined") {
dict[key] = value;
Expand Down Expand Up @@ -269,6 +270,18 @@ export class Core extends Module {
});
}

static renderAsyncFragment(fragmentName: string) {
const fragment = this.__pageConfiguration.fragments.find(item => item.name === fragmentName);
if (fragment) {
const selector = this.getFragmentContainerSelector(fragment, "main");
const fragmentContainer = window.document.querySelector(selector);
if (fragmentContainer ) {
if (this.observer) this.observer.unobserve(fragmentContainer);
return this.asyncLoadFragment(fragment);
}
}
}

private static isIntersectionObserverSupported() {
return 'IntersectionObserver' in window
&& 'IntersectionObserverEntry' in window
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IPageFragmentConfig {
asyncDecentralized: boolean;
attributes: { [name: string]: string };
source: string | undefined;
asyncLoaded?: boolean;
}

export interface IPageLibDependency {
Expand Down
19 changes: 13 additions & 6 deletions test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ declare var global: Global;
describe('Module - Core', () => {
beforeEach(() => {
global.window = (new JSDOM(``, {runScripts: "outside-only"})).window;
sandbox.verifyAndRestore();
});

afterEach(() => {
delete global.window;
PuzzleJs.clearListeners();
sandbox.verifyAndRestore();
(Core as any)._pageConfiguration = undefined;
});

Expand Down Expand Up @@ -170,7 +170,7 @@ describe('Module - Core', () => {
[]);
});

it('should create true load queue for js assets excluding conditional fragments', function () {
it('should render async fragment', async () => {
const assets = [
{
name: 'bundle1',
Expand Down Expand Up @@ -198,19 +198,26 @@ describe('Module - Core', () => {
if: "false"
},
chunked: true,
clientAsync: false,
clientAsync: true,
source: undefined,
asyncDecentralized: false
}],
page: 'page',
peers: []
} as IPageLibConfiguration;

const mockLoadJsSeries = sandbox.mock(AssetHelper);
const fragmentContainer = global.window.document.createElement('div');
fragmentContainer.setAttribute('puzzle-fragment', 'test');
global.window.document.body.appendChild(fragmentContainer);

const stubFetchGatewayFragment = sandbox.stub(Core as any, "fetchGatewayFragment").resolves();
const stubAsyncRenderResponse = sandbox.stub(Core as any, "asyncRenderResponse").resolves();

Core.config(JSON.stringify(config));
Core.pageLoaded();
await Core.renderAsyncFragment('test');
await Core.renderAsyncFragment('test');

mockLoadJsSeries.expects("loadJsSeries").calledWith([]);
expect(stubFetchGatewayFragment.calledOnce).to.eq(true);
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
});
});

0 comments on commit 56190d5

Please sign in to comment.