@@ -2,16 +2,17 @@ import _ from "lodash"
22import vtkWSLinkClient from "@kitware/vtk.js/IO/Core/WSLinkClient"
33import "@kitware/vtk.js/Rendering/OpenGL/Profiles/Geometry"
44import schemas from "@geode/opengeodeweb-viewer/schemas.json"
5+ import Status from "../utils/status"
56
67export const use_viewer_store = defineStore ( "viewer" , {
78 state : ( ) => ( {
89 default_local_port : "1234" ,
910 client : { } ,
1011 config : null ,
11- is_running : false ,
1212 picking_mode : false ,
1313 picked_point : { x : null , y : null } ,
1414 request_counter : 0 ,
15+ status : Status . NOT_CONNECTED ,
1516 } ) ,
1617 getters : {
1718 protocol ( ) {
@@ -55,71 +56,79 @@ export const use_viewer_store = defineStore("viewer", {
5556 this . picked_point . y = world_y
5657 this . picking_mode = false
5758 } ,
58- async ws_connect ( ) {
59+ async connect ( ) {
5960 if ( process . env . NODE_ENV == "test" ) {
6061 return
6162 }
62- const SmartConnect = await import ( "wslink/src/SmartConnect" )
63- vtkWSLinkClient . setSmartConnectClass ( SmartConnect )
63+ navigator . locks . request ( "viewer.connect" , async ( lock ) => {
64+ this . status = Status . CONNECTING
65+ if ( lock ) {
66+ this . status = Status . CONNECTING
67+ const SmartConnect = await import ( "wslink/src/SmartConnect" )
68+ vtkWSLinkClient . setSmartConnectClass ( SmartConnect )
6469
65- const config = { application : "Viewer" }
66- config . sessionURL = this . base_url
70+ const config = { application : "Viewer" }
71+ config . sessionURL = this . base_url
6772
68- const { client } = this
69- if ( this . is_running && client . isConnected ( ) ) {
70- client . disconnect ( - 1 )
71- this . is_running = false
72- }
73- let clientToConnect = client
74- if ( _ . isEmpty ( clientToConnect ) ) {
75- clientToConnect = vtkWSLinkClient . newInstance ( )
76- }
73+ const { client } = this
74+ if ( this . is_running && client . isConnected ( ) ) {
75+ client . disconnect ( - 1 )
76+ this . is_running = false
77+ }
78+ let clientToConnect = client
79+ if ( _ . isEmpty ( clientToConnect ) ) {
80+ clientToConnect = vtkWSLinkClient . newInstance ( )
81+ }
7782
78- // Connect to busy store
79- clientToConnect . onBusyChange ( ( count ) => {
80- this . buzy = count
81- } )
82- clientToConnect . beginBusy ( )
83+ // Connect to busy store
84+ clientToConnect . onBusyChange ( ( count ) => {
85+ this . buzy = count
86+ } )
87+ clientToConnect . beginBusy ( )
8388
84- // Error
85- clientToConnect . onConnectionError ( ( httpReq ) => {
86- const message =
87- ( httpReq && httpReq . response && httpReq . response . error ) ||
88- `Connection error`
89- console . error ( message )
90- } )
89+ // Error
90+ clientToConnect . onConnectionError ( ( httpReq ) => {
91+ const message =
92+ ( httpReq && httpReq . response && httpReq . response . error ) ||
93+ `Connection error`
94+ console . error ( message )
95+ } )
9196
92- // Close
93- clientToConnect . onConnectionClose ( ( httpReq ) => {
94- const message =
95- ( httpReq && httpReq . response && httpReq . response . error ) ||
96- `Connection close`
97- console . error ( message )
98- } )
97+ // Close
98+ clientToConnect . onConnectionClose ( ( httpReq ) => {
99+ const message =
100+ ( httpReq && httpReq . response && httpReq . response . error ) ||
101+ `Connection close`
102+ console . error ( message )
103+ } )
99104
100- // Connect
101- const { connectImageStream } = await import (
102- "@kitware/vtk.js/Rendering/Misc/RemoteView"
103- )
104- return new Promise ( ( resolve , reject ) => {
105- clientToConnect
106- . connect ( config )
107- . then ( ( validClient ) => {
108- connectImageStream ( validClient . getConnection ( ) . getSession ( ) )
109- this . client = validClient
110- clientToConnect . endBusy ( )
105+ // Connect
106+ const { connectImageStream } = await import (
107+ "@kitware/vtk.js/Rendering/Misc/RemoteView"
108+ )
109+ return new Promise ( ( resolve , reject ) => {
110+ clientToConnect
111+ . connect ( config )
112+ . then ( ( validClient ) => {
113+ connectImageStream ( validClient . getConnection ( ) . getSession ( ) )
114+ this . client = validClient
115+ clientToConnect . endBusy ( )
111116
112- // Now that the client is ready let's setup the server for us
113- viewer_call ( {
114- schema : schemas . opengeodeweb_viewer . viewer . reset_visualization ,
115- } )
116- this . is_running = true
117- resolve ( )
118- } )
119- . catch ( ( error ) => {
120- console . error ( error )
121- reject ( error )
117+ // Now that the client is ready let's setup the server for us
118+ viewer_call ( {
119+ schema :
120+ schemas . opengeodeweb_viewer . viewer . reset_visualization ,
121+ } )
122+ this . status = Status . CONNECTED
123+ resolve ( )
124+ } )
125+ . catch ( ( error ) => {
126+ console . error ( error )
127+ this . status = Status . NOT_CONNECTED
128+ reject ( error )
129+ } )
122130 } )
131+ }
123132 } )
124133 } ,
125134 start_request ( ) {
@@ -129,4 +138,7 @@ export const use_viewer_store = defineStore("viewer", {
129138 this . request_counter --
130139 } ,
131140 } ,
141+ share : {
142+ omit : [ "status" , "client" ] ,
143+ } ,
132144} )
0 commit comments