Skip to content

Commit 7a9e0c0

Browse files
committed
decentralization
1 parent d84fb2a commit 7a9e0c0

File tree

5 files changed

+175
-112
lines changed

5 files changed

+175
-112
lines changed

package-lock.json

Lines changed: 37 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"devDependencies": {
1919
"@types/chai": "^4.1.7",
2020
"@types/faker": "^4.1.5",
21+
"@types/gun": "^0.9.2",
2122
"@types/jest": "^24.0.13",
2223
"@types/jsdom": "^12.2.3",
2324
"@types/sinon": "^7.0.11",

src/core.ts

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import {Module} from "./module";
22
import {EVENT, RESOURCE_LOADING_TYPE} from "./enums";
33
import {IPageFragmentConfig, IPageLibAsset, IPageLibConfiguration} from "./types";
44
import {on} from "./decorators";
5-
import { AssetHelper } from "./assetHelper";
5+
import {AssetHelper} from "./assetHelper";
66

77
export class Core extends Module {
88
private static observer: IntersectionObserver | undefined;
9+
private static gun: any | undefined;
910

1011
static get _pageConfiguration() {
1112
return this.__pageConfiguration;
@@ -22,6 +23,15 @@ export class Core extends Module {
2223
static config(pageConfiguration: string) {
2324
Core.__pageConfiguration = JSON.parse(pageConfiguration) as IPageLibConfiguration;
2425

26+
const decentralizedFragmentsExists = Core.__pageConfiguration.fragments.some(fragment => fragment.asyncDecentralized);
27+
28+
if (decentralizedFragmentsExists) {
29+
Core.gun = (window as any).Gun({
30+
peers: Core.__pageConfiguration.peers,
31+
localStorage: false
32+
});
33+
}
34+
2535
if (this.isIntersectionObserverSupported()) {
2636
const asyncFragments = Core.__pageConfiguration.fragments.some(i => i.clientAsync);
2737

@@ -56,7 +66,7 @@ export class Core extends Module {
5666
@on(EVENT.ON_PAGE_LOAD)
5767
static pageLoaded() {
5868
const onFragmentRenderAssets = Core.__pageConfiguration.assets.filter(asset => {
59-
if(asset.loadMethod === RESOURCE_LOADING_TYPE.ON_PAGE_RENDER && !asset.preLoaded) {
69+
if (asset.loadMethod === RESOURCE_LOADING_TYPE.ON_PAGE_RENDER && !asset.preLoaded) {
6070
const fragment = Core.__pageConfiguration.fragments.find(fragment => fragment.name === asset.fragment);
6171
return fragment && fragment.attributes.if !== "false";
6272
}
@@ -87,31 +97,54 @@ export class Core extends Module {
8797

8898
private static asyncLoadFragment(fragment: IPageFragmentConfig) {
8999
const queryString = this.prepareQueryString(fragment.attributes);
90-
fetch(`${fragment.source}${location.pathname}${queryString}`, {
100+
const key = `${fragment.source}${location.pathname}${queryString}`;
101+
102+
if (!fragment.asyncDecentralized) {
103+
return this.fetchGatewayFragment(fragment)
104+
.then(res => this.asyncRenderResponse(fragment, res));
105+
}
106+
107+
108+
Core.gun
109+
.get(key, (gunResponse: any) => {
110+
if (gunResponse.err || !gunResponse.put) {
111+
this.fetchGatewayFragment(fragment)
112+
.then(gatewayResponse => Core.gun.get(key).put(gatewayResponse));
113+
}
114+
})
115+
.on((gunResponse: any) => this.asyncRenderResponse(fragment, gunResponse));
116+
}
117+
118+
private static fetchGatewayFragment(fragment: IPageFragmentConfig) {
119+
const queryString = this.prepareQueryString(fragment.attributes);
120+
const fragmentRequestUrl = `${fragment.source}${location.pathname}${queryString}`;
121+
return fetch(fragmentRequestUrl, {
91122
credentials: 'include'
92-
}).then(res => {
93-
return res.json();
94123
})
95124
.then(res => {
96-
if (res['$model']) {
97-
Object.keys(res['$model']).forEach(key => {
98-
(window as any)[key] = res['$model'][key];
99-
});
100-
}
101-
102-
Object.keys(res).forEach(key => {
103-
if (!key.startsWith('$')) {
104-
const container = document.querySelector(this.getFragmentContainerSelector(fragment, key));
105-
if (container) {
106-
this.setEvalInnerHtml(container, res[key]);
107-
}
108-
}
109-
});
125+
return res.json();
126+
});
127+
}
110128

111-
const fragmentAssets = Core.__pageConfiguration.assets.filter(asset => asset.fragment === fragment.name);
112-
const scripts = Core.createLoadQueue(fragmentAssets, true);
113-
AssetHelper.loadJsSeries(scripts);
129+
private static asyncRenderResponse(fragment: IPageFragmentConfig, res: any) {
130+
if (res['$model']) {
131+
Object.keys(res['$model']).forEach(key => {
132+
(window as any)[key] = res['$model'][key];
114133
});
134+
}
135+
136+
Object.keys(res).forEach(key => {
137+
if (!key.startsWith('$')) {
138+
const container = document.querySelector(this.getFragmentContainerSelector(fragment, key));
139+
if (container) {
140+
this.setEvalInnerHtml(container, res[key]);
141+
}
142+
}
143+
});
144+
145+
const fragmentAssets = Core.__pageConfiguration.assets.filter(asset => asset.fragment === fragment.name);
146+
const scripts = Core.createLoadQueue(fragmentAssets, true);
147+
AssetHelper.loadJsSeries(scripts);
115148
}
116149

117150
private static getFragmentContainerSelector(fragment: IPageFragmentConfig, partial: string) {

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface IPageFragmentConfig {
3131
name: string;
3232
chunked: boolean;
3333
clientAsync: boolean;
34+
asyncDecentralized: boolean;
3435
attributes: { [name: string]: string };
3536
source: string | undefined;
3637
}
@@ -47,6 +48,7 @@ export interface IPageLibConfiguration {
4748
fragments: IPageFragmentConfig[];
4849
assets: IPageLibAsset[];
4950
dependencies: IPageLibDependency[];
51+
peers: string[];
5052
}
5153

5254
export interface IEventListener {

0 commit comments

Comments
 (0)