1+ // this is restoring state to/from the v86 emulator
2+ // however instead of passing the huge blob between webworker/browser
3+ // we transfer it via localforage as a base64 string
4+
15if ( typeof emulator != 'undefined' ) {
26 // inside worker-thread
37 importScripts ( "localforage.js" ) // we don't instance it again here (just use its functions)
48
59 this . restore_state = async function ( data ) {
10+ // fastforward instance state
11+ this . opts . muteUntilPrompt = false
12+ this . ready = true
13+
614 return new Promise ( ( resolve , reject ) => {
715 localforage . getItem ( "state" , async ( err , stateBase64 ) => {
816 if ( stateBase64 && ! err ) {
@@ -15,15 +23,20 @@ if( typeof emulator != 'undefined' ){
1523 } )
1624 }
1725 this . save_state = async function ( ) {
18- console . log ( "saving session" )
19- let state = await emulator . save_state ( )
20- localforage . setDriver ( [
21- localforage . INDEXEDDB ,
22- localforage . WEBSQL ,
23- localforage . LOCALSTORAGE
24- ] ) . then ( ( ) => {
25- localforage . setItem ( "state" , ISOTerminal . prototype . convert . arrayBufferToBase64 ( state ) )
26- console . log ( "state saved" )
26+ return new Promise ( async ( resolve , reject ) => {
27+ console . log ( "saving session" )
28+ let state = await emulator . save_state ( )
29+ localforage . setDriver ( [
30+ localforage . INDEXEDDB ,
31+ localforage . WEBSQL ,
32+ localforage . LOCALSTORAGE
33+ ] )
34+ . then ( ( ) => {
35+ localforage . setItem ( "state" , ISOTerminal . prototype . convert . arrayBufferToBase64 ( state ) )
36+ console . log ( "state saved" )
37+ resolve ( )
38+ } )
39+ . catch ( reject )
2740 } )
2841 }
2942
@@ -32,50 +45,62 @@ if( typeof emulator != 'undefined' ){
3245 // inside browser-thread
3346 ISOTerminal . addEventListener ( 'emulator-started' , function ( e ) {
3447 this . autorestore ( e )
48+ this . emit ( "autorestore-installed" )
3549 } )
3650
37- ISOTerminal . prototype . autorestore = async function ( e ) {
51+ ISOTerminal . prototype . restore = async function ( e ) {
3852
39- localforage . setDriver ( [
40- localforage . INDEXEDDB ,
41- localforage . WEBSQL ,
42- localforage . LOCALSTORAGE
43- ] ) . then ( ( ) => {
53+ const onGetItem = ( err , stateBase64 ) => {
54+ const askConfirm = ( ) => {
55+ if ( window . localStorage . getItem ( "restorestate" ) == "true" ) return true
56+ try {
57+ const scene = document . querySelector ( 'a-scene' ) ;
58+ if ( scene . is ( 'ar-mode' ) ) scene . exitAR ( )
59+ if ( scene . is ( 'vr-mode' ) ) scene . exitVR ( )
60+ } catch ( e ) { }
61+ return confirm ( "Continue old session?" )
62+ }
4463
45- localforage . getItem ( "state" , async ( err , stateBase64 ) => {
46- const askConfirm = ( ) => {
47- if ( window . localStorage . getItem ( "restorestate" ) == "true" ) return true
48- try {
49- const scene = document . querySelector ( 'a-scene' ) ;
50- if ( scene . is ( 'ar-mode' ) ) scene . exitAR ( )
51- if ( scene . is ( 'vr-mode' ) ) scene . exitVR ( )
52- } catch ( e ) { }
53- return confirm ( 'continue last session?' )
54- }
55- if ( stateBase64 && ! err && document . location . hash . length < 2 && askConfirm ( ) ) {
56- this . noboot = true // see feat/boot.js
57- try {
58- await this . worker . restore_state ( )
64+ if ( stateBase64 && ! err && document . location . hash . length < 2 && askConfirm ( ) ) {
65+ this . noboot = true // see feat/boot.js
66+ try {
67+ this . worker . restore_state ( )
68+ . then ( ( ) => {
5969 // simulate / fastforward boot events
6070 this . postBoot ( ( ) => {
61- this . send ( "l\n" )
62- this . send ( "hook wakeup\n" )
71+ // force redraw terminal issue
72+ this . send ( "l" )
73+ setTimeout ( ( ) => this . send ( "l" ) , 200 )
74+ //this.send("12")
75+ this . emit ( "exec" , [ "source /etc/profile.sh; hook wakeup\n" ] )
76+ this . emit ( "restored" )
6377 } )
64- } catch ( e ) { console . error ( e ) }
65- }
66- } )
67-
68- this . save = async ( ) => {
69- await this . worker . save_state ( )
78+ } )
79+ } catch ( e ) { console . error ( e ) }
7080 }
81+ }
82+
83+ const doRestore = ( ) => {
84+
85+ localforage . getItem ( "state" , ( err , stateBase64 ) => onGetItem ( err , stateBase64 ) )
7186
7287 window . addEventListener ( "beforeunload" , function ( e ) {
7388 var confirmationMessage = "Sure you want to leave?\nTIP: enter 'save' to continue this session later" ;
7489 ( e || window . event ) . returnValue = confirmationMessage ; //Gecko + IE
7590 return confirmationMessage ; //Webkit, Safari, Chrome
7691 } ) ;
7792
78- } )
93+ }
94+
95+ localforage . setDriver ( [
96+ localforage . INDEXEDDB ,
97+ localforage . WEBSQL ,
98+ localforage . LOCALSTORAGE
99+ ] )
100+ . then ( ( ) => doRestore ( ) )
101+
79102 }
80103
104+ ISOTerminal . prototype . autorestore = ISOTerminal . prototype . restore // alias to launch during boot
105+
81106}
0 commit comments