@@ -44,32 +44,47 @@ export class IpfsTransformationUIAddonCode {
4444 }
4545 }
4646
47- private async processDocument ( document : any ) : Promise < void > {
48- if ( ! document || typeof ( document ) !== 'object' ) {
47+ private async processDocument ( rootDocument : any ) : Promise < void > {
48+ if ( ! rootDocument || typeof ( rootDocument ) !== 'object' ) {
4949 return ;
5050 }
5151
52- if ( Array . isArray ( document ) ) {
53- for ( let i = 0 ; i < document . length ; i ++ ) {
54- if ( typeof ( document [ i ] ) === 'string' && document [ i ] . startsWith ( 'ipfs://' ) ) {
55- document [ i ] = await this . processIpfsString ( document [ i ] ) ;
56- } else if ( typeof ( document [ i ] ) === 'object' && document [ i ] !== null ) {
57- await this . processDocument ( document [ i ] ) ;
52+ const stack : any [ ] = [ rootDocument ] ;
53+ const tasks : Promise < void | any > [ ] = [ ] ;
54+
55+ while ( stack . length ) {
56+ const documentObject = stack . pop ( ) ;
57+ if ( Array . isArray ( documentObject ) ) {
58+ for ( let i = 0 ; i < documentObject . length ; i ++ ) {
59+ const documentValue = documentObject [ i ] ;
60+ if ( typeof ( documentValue ) === 'string' && documentValue . startsWith ( 'ipfs://' ) ) {
61+ tasks . push ( this . processIpfsString ( documentValue ) . then ( res => {
62+ documentObject [ i ] = res ;
63+ } ) ) ;
64+ } else if ( documentValue && typeof ( documentValue ) === 'object' ) {
65+ stack . push ( documentValue ) ;
66+ }
5867 }
59- }
60- } else {
61- for ( const key in document ) {
62- const value = document [ key ] ;
63-
64- if ( typeof ( value ) === 'string' && value . startsWith ( 'ipfs://' ) ) {
65- document [ key ] = await this . processIpfsString ( value ) ;
66- } else if ( typeof ( value ) === 'object' && value !== null ) {
67- await this . processDocument ( value ) ;
68+ } else {
69+ for ( const key in documentObject ) {
70+ const value = documentObject [ key ] ;
71+ if ( typeof ( value ) === 'string' && value . startsWith ( 'ipfs://' ) ) {
72+ tasks . push ( this . processIpfsString ( value ) . then ( res => {
73+ documentObject [ key ] = res ;
74+ } ) ) ;
75+ } else if ( value && typeof ( value ) === 'object' ) {
76+ stack . push ( value ) ;
77+ }
6878 }
6979 }
7080 }
81+
82+ if ( tasks . length ) {
83+ await Promise . all ( tasks ) ;
84+ }
7185 }
7286
87+
7388 private async processIpfsString ( ipfsString : string ) : Promise < any > {
7489 const match = this . ipfsPattern . exec ( ipfsString ) ;
7590 if ( ! match ) {
0 commit comments