@@ -2,10 +2,11 @@ import {Module} from "./module";
22import { EVENT , RESOURCE_LOADING_TYPE } from "./enums" ;
33import { IPageFragmentConfig , IPageLibAsset , IPageLibConfiguration } from "./types" ;
44import { on } from "./decorators" ;
5- import { AssetHelper } from "./assetHelper" ;
5+ import { AssetHelper } from "./assetHelper" ;
66
77export 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 ) {
0 commit comments