xrsh-com/com/isoterminal/feat/autorestore.js

107 lines
3.4 KiB
JavaScript
Raw Normal View History

2025-05-22 16:24:53 +02:00
// this is restoring state to/from the v86 emulator
// however instead of passing the huge blob between webworker/browser
// we transfer it via localforage as a base64 string
if( typeof emulator != 'undefined' ){
// inside worker-thread
importScripts("localforage.js") // we don't instance it again here (just use its functions)
this.restore_state = async function(data){
2025-05-22 16:24:53 +02:00
// fastforward instance state
this.opts.muteUntilPrompt = false
this.ready = true
return new Promise( (resolve,reject) => {
localforage.getItem("state", async (err,stateBase64) => {
if( stateBase64 && !err ){
state = ISOTerminal.prototype.convert.base64ToArrayBuffer( stateBase64 )
await emulator.restore_state(state)
console.log("restored state")
}else return reject("worker.js: emulator.restore_state (could not get state from localforage)")
resolve()
})
})
}
this.save_state = async function(){
2025-05-22 16:24:53 +02:00
return new Promise( async (resolve,reject ) => {
console.log("saving session")
let state = await emulator.save_state()
localforage.setDriver([
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
])
.then( () => {
localforage.setItem("state", ISOTerminal.prototype.convert.arrayBufferToBase64(state) )
console.log("state saved")
resolve()
})
.catch( reject )
})
}
2024-09-23 14:58:48 +00:00
}else{
// inside browser-thread
ISOTerminal.addEventListener('emulator-started', function(e){
this.autorestore(e)
2025-05-22 16:24:53 +02:00
this.emit("autorestore-installed")
})
2025-05-22 16:24:53 +02:00
ISOTerminal.prototype.restore = async function(e){
2025-05-22 16:24:53 +02:00
const onGetItem = (err,stateBase64) => {
const askConfirm = () => {
if( window.localStorage.getItem("restorestate") == "true" ) return true
try{
const scene = document.querySelector('a-scene');
if( scene.is('ar-mode') ) scene.exitAR()
if( scene.is('vr-mode') ) scene.exitVR()
}catch(e){}
return confirm( "Continue old session?" )
}
2025-05-22 16:24:53 +02:00
if( stateBase64 && !err && document.location.hash.length < 2 && askConfirm() ){
this.noboot = true // see feat/boot.js
try{
this.worker.restore_state()
.then( () => {
// simulate / fastforward boot events
2024-10-18 11:50:56 +00:00
this.postBoot( () => {
2025-05-22 16:24:53 +02:00
// force redraw terminal issue
this.send("l")
setTimeout( () => this.send("l"), 200 )
//this.send("12")
this.emit("exec",["source /etc/profile.sh; hook wakeup\n"])
this.emit("restored")
2024-10-18 11:50:56 +00:00
})
2025-05-22 16:24:53 +02:00
})
}catch(e){ console.error(e) }
}
2025-05-22 16:24:53 +02:00
}
const doRestore = () => {
localforage.getItem("state", (err,stateBase64) => onGetItem(err,stateBase64) )
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Sure you want to leave?\nTIP: enter 'save' to continue this session later";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
2025-05-22 16:24:53 +02:00
}
localforage.setDriver([
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
])
.then( () => doRestore() )
}
2025-05-22 16:24:53 +02:00
ISOTerminal.prototype.autorestore = ISOTerminal.prototype.restore // alias to launch during boot
}