@@ -2,13 +2,16 @@ import fs from 'socket:fs'
2
2
import path from 'socket:path'
3
3
import process from 'socket:process'
4
4
import application from 'socket:application'
5
+ import { network , Encryption , sha256 } from 'socket:network'
5
6
import vm from 'socket:vm'
6
7
import { inspect , format } from 'socket:util'
7
8
import { spawn , exec } from 'socket:child_process'
8
9
9
10
import Tonic from '@socketsupply/tonic'
10
11
import components from '@socketsupply/components'
11
12
13
+ import Database from './db/index.js'
14
+
12
15
import { AppTerminal } from './components/terminal.js'
13
16
import { AppProject } from './components/project.js'
14
17
import { AppProperties } from './components/properties.js'
@@ -30,7 +33,7 @@ class AppView extends Tonic {
30
33
this . reloadPreviewWindows ( )
31
34
} )
32
35
33
- window . addEventListener ( 'close ' , async e => {
36
+ window . addEventListener ( 'window-closed ' , async e => {
34
37
const data = e . detail . data
35
38
36
39
const previewWindowSettings = this . state . settings . previewWindows [ data - 1 ]
@@ -142,11 +145,6 @@ class AppView extends Tonic {
142
145
return
143
146
}
144
147
145
- for ( const [ k , v ] of Object . entries ( this . previewWindows ) ) {
146
- delete this . state . zoom [ k ]
147
- await v . close ( ) // destroy any existing preview windows
148
- }
149
-
150
148
if ( ! this . state . userScript ) {
151
149
const res = await fetch ( './preview.js' )
152
150
this . state . userScript = await res . text ( )
@@ -156,19 +154,27 @@ class AppView extends Tonic {
156
154
const screenSize = await application . getScreenSize ( )
157
155
158
156
for ( let i = 0 ; i < this . state . settings . previewWindows . length ; i ++ ) {
157
+ const index = i + 1
159
158
const preview = this . state . settings . previewWindows [ i ]
160
- if ( ! preview . active ) continue
159
+
160
+ if ( ! preview . active ) {
161
+ const w = this . previewWindows [ index ]
162
+ if ( w ) {
163
+ delete this . state . zoom [ index ]
164
+ await w . close ( )
165
+ }
166
+ continue
167
+ }
161
168
162
169
let width = screenSize . width * 0.6
163
170
let height = screenSize . height * 0.6
164
- const index = i + 1
165
171
const scale = preview . scale || 1
166
172
const platform = preview . platform || process . platform
167
173
168
174
if ( / \d + x \d + / . test ( preview . resolution ) ) {
169
175
const size = preview . resolution . split ( 'x' )
170
- width = preview . resolution = size [ 0 ]
171
- height = preview . resolution = size [ 1 ]
176
+ width = size [ 0 ]
177
+ height = size [ 1 ]
172
178
}
173
179
174
180
let hostOS = process . platform
@@ -243,13 +249,55 @@ class AppView extends Tonic {
243
249
} )
244
250
245
251
this . previewWindows [ w . index ] = w
246
- } catch { }
252
+ } catch ( err ) {
253
+ console . log ( err )
254
+ }
247
255
}
248
256
}
249
257
250
- async init ( ) {
251
- await navigator . serviceWorker . ready
258
+ async initData ( ) {
259
+ if ( process . env . DEBUG === '1' ) {
260
+ const databases = await window . indexedDB . databases ( )
261
+ for ( const { name } of databases ) await Database . drop ( name )
262
+ }
263
+
264
+ this . db = {
265
+ channels : await Database . open ( 'shares' ) ,
266
+ state : await Database . open ( 'state' )
267
+ }
268
+
269
+ Database . onerror = err => {
270
+ console . error ( err )
252
271
272
+ const notifications = document . querySelector ( '#notifications' )
273
+ notifications ?. create ( {
274
+ type : 'error' ,
275
+ title : 'Unhandled Database Error' ,
276
+ message : err . message
277
+ } )
278
+ }
279
+
280
+ //
281
+ // Create a default clusterId (used as the default group)
282
+ //
283
+ const clusterId = await Encryption . createClusterId ( 'socket-app-studio' )
284
+
285
+ const { data : dataPeer } = await this . db . state . has ( 'peer' )
286
+
287
+ if ( ! dataPeer ) {
288
+ const signingKeys = await Encryption . createKeyPair ( )
289
+ await this . db . state . put ( 'peer' , {
290
+ config : {
291
+ peerId : await Encryption . createId ( ) ,
292
+ clusterId
293
+ }
294
+ } )
295
+ }
296
+
297
+
298
+ }
299
+
300
+ async initApplication ( ) {
253
301
const notifications = document . querySelector ( '#notifications' )
254
302
const settingsFile = path . join ( path . DATA , 'projects' , 'settings.json' )
255
303
@@ -523,7 +571,10 @@ class AppView extends Tonic {
523
571
}
524
572
525
573
async render ( ) {
526
- await this . init ( )
574
+ await navigator . serviceWorker . ready
575
+
576
+ await this . initData ( )
577
+ await this . initApplication ( )
527
578
528
579
return this . html `
529
580
< header >
0 commit comments