@@ -139,7 +139,7 @@ studio.protocol = (function(ProtoBuf) {
139139 var response = new obj . AuthRequestChallengeResponse ( ) ;
140140 response . type = "PasswordHash" ;
141141 response . response = new Uint8Array ( challenge_digest ) ;
142- authReq . challenge_response = new Array ( ) ;
142+ authReq . challenge_response = [ ] ;
143143 authReq . challenge_response . push ( response ) ;
144144 resolve ( authReq ) ;
145145 } )
@@ -335,10 +335,10 @@ studio.internal = (function(proto) {
335335 var structureFetched = false ;
336336 var childMap = new Map ( ) ;
337337 var givenPromises = new Map ( ) ;
338- var childIterators = new Array ( ) ;
339- var valueSubscriptions = new Array ( ) ;
340- var structureSubscriptions = new Array ( ) ;
341- var eventSubscriptions = new Array ( ) ;
338+ var childIterators = [ ] ;
339+ var valueSubscriptions = [ ] ;
340+ var structureSubscriptions = [ ] ;
341+ var eventSubscriptions = [ ] ;
342342 var lastValue ;
343343 var lastInfo = null ; //when we get this, if there are any child requests we need to fetch child fetch too
344344 var valid = true ;
@@ -367,7 +367,6 @@ studio.internal = (function(proto) {
367367 this . setIsStructureFetched = function ( value ) {
368368 structureFetched = value ;
369369 } ;
370-
371370 this . isStructureFetched = function ( ) {
372371 return structureFetched ;
373372 } ;
@@ -542,7 +541,7 @@ studio.internal = (function(proto) {
542541 var maxFs = Math . max . apply ( Math , valueSubscriptions . map ( v => v [ 1 ] ) ) ;
543542 var maxSampleRate = Math . max . apply ( Math , valueSubscriptions . map ( v => v [ 2 ] ) ) ;
544543 //by studio api protocol 0 is the highest sample rate (all samples), so override maxSampleRate if 0 is found
545- const zeroRate = valueSubscriptions . find ( e => e [ 2 ] == 0 ) ;
544+ const zeroRate = valueSubscriptions . find ( e => e [ 2 ] === 0 ) ;
546545 maxSampleRate = zeroRate ? zeroRate [ 2 ] : maxSampleRate ;
547546 app . makeGetterRequest ( id , maxFs , maxSampleRate , false ) ;
548547 } else {
@@ -551,6 +550,169 @@ studio.internal = (function(proto) {
551550 }
552551 }
553552
553+ obj . SystemNode = function ( studioURL , notificationListener ) {
554+ var appConnections = [ ] ;
555+ var pendingConnects = [ ] ;
556+ var connected = false ;
557+ var connecting = false ;
558+ var this_ = this ;
559+
560+ this . onAppConnect = function ( url , notificationListener ) {
561+ return new Promise ( function ( resolve , reject ) {
562+ var appConnection = new obj . AppConnection ( url , notificationListener ) ;
563+ appConnections . push ( appConnection ) ;
564+ var sys = appConnection . root ( ) ;
565+ sys . async . onDone ( resolve , reject , sys ) ;
566+ } ) ;
567+ } ;
568+
569+ this . onConnect = function ( resolve , reject ) {
570+ if ( connected ) {
571+ resolve ( this_ ) ;
572+ return ;
573+ }
574+
575+ if ( connecting ) {
576+ pendingConnects . push ( { resolve : resolve , reject : reject } ) ;
577+ return ;
578+ }
579+
580+ connecting = true ;
581+ pendingConnects . push ( { resolve : resolve , reject : reject } ) ;
582+
583+ this . onAppConnect ( studioURL , notificationListener ) . then ( function ( system ) {
584+ var promises = [ ] ;
585+ system . forEachChild ( function ( app ) {
586+ if ( ! app . info ( ) . is_local )
587+ {
588+ var appUrl = app . info ( ) . server_addr + ":" + app . info ( ) . server_port ;
589+ promises . push ( this_ . onAppConnect ( appUrl , notificationListener ) ) ;
590+ }
591+ } ) ;
592+ Promise . all ( promises ) . then ( function ( ) {
593+ pendingConnects . forEach ( function ( con ) {
594+ con . resolve ( this_ ) ;
595+ } ) ;
596+ pendingConnects = [ ] ;
597+ connecting = false ;
598+ connected = true ;
599+ } ) ;
600+ } , reject ) ;
601+ }
602+
603+ this . applicationNodes = function ( ) {
604+ var nodes = [ ] ;
605+ appConnections . forEach ( function ( con ) {
606+ con . root ( ) . forEachChild ( function ( app ) {
607+ if ( app . info ( ) . is_local )
608+ nodes . push ( app ) ;
609+ } ) ;
610+ } ) ;
611+ return nodes ;
612+ }
613+
614+ this . isValid = function ( ) {
615+ return true ;
616+ }
617+
618+ this . name = function ( ) {
619+ return appConnections [ 0 ] . root ( ) . name ( ) ;
620+ } ;
621+
622+ this . info = function ( ) {
623+ return appConnections [ 0 ] . root ( ) . info ( ) ;
624+ } ;
625+
626+ this . lastValue = function ( ) {
627+ return appConnections [ 0 ] . root ( ) . lastValue ( ) ;
628+ } ;
629+
630+ this . child = function ( name ) {
631+ return this . applicationNodes ( ) . find ( function ( app ) {
632+ return app . name ( ) === name ;
633+ } ) ;
634+ }
635+
636+ this . isStructureFetched = function ( ) {
637+ return true ;
638+ }
639+
640+ this . forEachChild = function ( iteratorFunction ) {
641+ this . applicationNodes ( ) . forEach ( function ( app ) {
642+ iteratorFunction ( app ) ;
643+ } ) ;
644+ } ;
645+
646+ this . async = { } ;
647+
648+ this . async . fetch = function ( ) {
649+ this . applicationNodes ( ) . forEach ( function ( app ) {
650+ pendingFetches . push ( app ) ;
651+ app . fetch ( ) ;
652+ } ) ;
653+ } ;
654+
655+ this . async . onDone = function ( resolve , reject , apiNode ) {
656+ const index = pendingFetches . indexOf ( apiNode ) ;
657+ if ( index > - 1 ) {
658+ pendingFetches [ index ] . onDone ( resolve , reject , apiNode ) ;
659+ pendingFetches . splice ( index , 1 ) ;
660+ }
661+ } ;
662+
663+ this . async . subscribeToValues = function ( valueConsumer , fs = 5 , sampleRate = 0 ) {
664+
665+ } ;
666+
667+ this . async . subscribeToChildValues = function ( name , valueConsumer , fs = 5 , sampleRate = 0 ) {
668+
669+ } ;
670+
671+ this . async . unsubscribeFromValues = function ( valueConsumer ) {
672+
673+ } ;
674+
675+ this . async . unsubscribeFromChildValues = function ( name , valueConsumer ) {
676+
677+ } ;
678+
679+ this . async . subscribeToStructure = function ( structureConsumer ) {
680+ this . applicationNodes ( ) . forEach ( function ( app ) {
681+ app . subscribeToStructure ( structureConsumer ) ;
682+ } ) ;
683+ } ;
684+
685+ this . async . unsubscribeFromStructure = function ( structureConsumer ) {
686+ this . applicationNodes ( ) . forEach ( function ( app ) {
687+ app . unsubscribeFromStructure ( structureConsumer ) ;
688+ } ) ;
689+ } ;
690+
691+ this . async . subscribeToEvents = function ( eventConsumer , startingFrom ) {
692+ this . applicationNodes ( ) . forEach ( function ( app ) {
693+ app . subscribeToEvents ( eventConsumer , startingFrom ) ;
694+ } ) ;
695+ } ;
696+
697+ this . async . unsubscribeFromEvents = function ( eventConsumer ) {
698+ this . applicationNodes ( ) . forEach ( function ( app ) {
699+ app . unsubscribeFromEvents ( eventConsumer ) ;
700+ } ) ;
701+ } ;
702+
703+ this . async . addChild = function ( name , modelName ) {
704+
705+ } ;
706+
707+ this . async . removeChild = function ( name ) {
708+
709+ } ;
710+
711+ this . async . setValue = function ( value , timestamp ) {
712+
713+ } ;
714+ } ;
715+
554716 obj . AppConnection = function ( url , notificationListener ) {
555717 var appConnection = this ;
556718 var appName = "" ;
@@ -661,7 +823,7 @@ studio.internal = (function(proto) {
661823 var msg = new proto . Container ( ) ;
662824 msg . message_type = proto . ContainerType . eStructureRequest ;
663825 if ( id != proto . SYSTEM_NODE_ID ) {
664- msg . structure_request = new Array ( ) ;
826+ msg . structure_request = [ ] ;
665827 msg . structure_request . push ( id ) ;
666828 }
667829 send ( msg ) ;
@@ -907,7 +1069,6 @@ studio.internal = (function(proto) {
9071069 return obj ;
9081070} ) ( studio . protocol ) ;
9091071
910-
9111072/**
9121073 * The studio.api namespace.
9131074 * @exports studio.api
@@ -1216,7 +1377,7 @@ studio.api = (function(internal) {
12161377 * @constructor
12171378 */
12181379 obj . Client = function ( studioURL , notificationListener ) {
1219- var appConnection = new internal . AppConnection ( studioURL , notificationListener ) ;
1380+ var system = new internal . SystemNode ( studioURL , notificationListener ) ;
12201381
12211382 /**
12221383 * Request root node.
@@ -1225,8 +1386,9 @@ studio.api = (function(internal) {
12251386 */
12261387 this . root = function ( ) {
12271388 return new Promise ( function ( resolve , reject ) {
1228- var node = appConnection . root ( ) ;
1229- node . async . onDone ( resolve , reject , new INode ( node ) ) ;
1389+ system . onConnect ( function ( system ) {
1390+ resolve ( new INode ( system ) ) ;
1391+ } , reject ) ;
12301392 } ) ;
12311393 } ;
12321394
0 commit comments