Skip to content

Commit

Permalink
Smarter VM stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Aug 3, 2023
1 parent a387926 commit d225ffc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/containers/tw-restore-point-manager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ class TWRestorePointManager extends React.Component {
return;
}
this._startLoading();
RestorePointAPI.loadRestorePoint(id)
.then(buffer => this.props.vm.loadProject(buffer))
RestorePointAPI.loadRestorePoint(this.props.vm, id)
.then(() => {
this._finishLoading(true);
})
Expand Down
38 changes: 28 additions & 10 deletions src/lib/tw-restore-point-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,14 @@ const removeExtraneousRestorePoints = () => openDB().then(db => new Promise((res
* @returns {Promise<{type: string; data: ArrayBuffer;}>} Thumbnail data
*/
const generateThumbnail = vm => new Promise(resolve => {
// Piggyback off of the next draw if we can, otherwise just force it to render
const drawTimeout = setTimeout(() => {
vm.renderer.draw();
}, 100);

vm.renderer.requestSnapshot(dataURL => {
clearTimeout(drawTimeout);

const index = dataURL.indexOf(',');
const base64 = dataURL.substring(index + 1);
const arrayBuffer = base64ToArrayBuffer(base64);
Expand All @@ -204,9 +211,6 @@ const generateThumbnail = vm => new Promise(resolve => {
data: arrayBuffer
});
});

// Force the snapshot to be processed immediately, even if the project is not running yet.
vm.renderer.draw();
});

/**
Expand Down Expand Up @@ -356,10 +360,11 @@ const deleteAllRestorePoints = () => openDB().then(db => new Promise((resolveTra
}));

/**
* @param {VirtualMachine} vm scratch-vm instance
* @param {number} id the restore point's ID
* @returns {Promise<ArrayBuffer>} Resolves with sb3 file
*/
const loadRestorePoint = id => openDB().then(db => new Promise((resolveTransaction, rejectTransaction) => {
const loadRestorePoint = (vm, id) => openDB().then(db => new Promise((resolveTransaction, rejectTransaction) => {
const transaction = db.transaction([METADATA_STORE, PROJECT_STORE, ASSET_STORE], 'readonly');
transaction.onerror = () => {
rejectTransaction(new Error(`Transaction error: ${transaction.error}`));
Expand All @@ -369,11 +374,22 @@ const loadRestorePoint = id => openDB().then(db => new Promise((resolveTransacti
/** @type {Metadata} */
let metadata;

const generate = () => {
resolveTransaction(zip.generateAsync({
// Don't bother compressing it since it will be immediately decompressed
type: 'arraybuffer'
}));
// TODO: we should be able to use a custom scratch-storage helper to avoid putting the
// zip in memory.

const loadVM = () => {
resolveTransaction(
zip.generateAsync({
// Don't bother compressing it since it will be immediately decompressed
type: 'arraybuffer'
})
.then(sb3 => vm.loadProject(sb3))
.then(() => {
setTimeout(() => {
vm.renderer.draw();
});
})
);
};

const loadAssets = async () => {
Expand All @@ -389,7 +405,7 @@ const loadRestorePoint = id => openDB().then(db => new Promise((resolveTransacti
});
}

generate();
loadVM();
};

const loadProjectJSON = () => {
Expand All @@ -410,6 +426,8 @@ const loadRestorePoint = id => openDB().then(db => new Promise((resolveTransacti
};
};

vm.stop();

loadMetadata();
}));

Expand Down

0 comments on commit d225ffc

Please sign in to comment.