mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-16 09:13:13 -04:00
Add runtime event for when the project is loaded (#1669)
This commit is contained in:
parent
fd5e178d3b
commit
6ef600dc2c
3 changed files with 35 additions and 0 deletions
|
@ -402,6 +402,14 @@ class Runtime extends EventEmitter {
|
||||||
return 'VISUAL_REPORT';
|
return 'VISUAL_REPORT';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event name for project loaded report.
|
||||||
|
* @const {string}
|
||||||
|
*/
|
||||||
|
static get PROJECT_LOADED () {
|
||||||
|
return 'PROJECT_LOADED';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event name for targets update report.
|
* Event name for targets update report.
|
||||||
* @const {string}
|
* @const {string}
|
||||||
|
@ -1913,6 +1921,13 @@ class Runtime extends EventEmitter {
|
||||||
return this._cloneCounter < Runtime.MAX_CLONES;
|
return this._cloneCounter < Runtime.MAX_CLONES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report that the project has loaded in the Virtual Machine.
|
||||||
|
*/
|
||||||
|
emitProjectLoaded () {
|
||||||
|
this.emit(Runtime.PROJECT_LOADED);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report that a new target has been created, possibly by cloning an existing target.
|
* Report that a new target has been created, possibly by cloning an existing target.
|
||||||
* @param {Target} newTarget - the newly created target.
|
* @param {Target} newTarget - the newly created target.
|
||||||
|
|
|
@ -284,6 +284,7 @@ class VirtualMachine extends EventEmitter {
|
||||||
|
|
||||||
return validationPromise
|
return validationPromise
|
||||||
.then(validatedInput => this.deserializeProject(validatedInput[0], validatedInput[1]))
|
.then(validatedInput => this.deserializeProject(validatedInput[0], validatedInput[1]))
|
||||||
|
.then(() => this.runtime.emitProjectLoaded())
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
// Intentionally rejecting here (want errors to be handled by caller)
|
// Intentionally rejecting here (want errors to be handled by caller)
|
||||||
if (error.hasOwnProperty('validationError')) {
|
if (error.hasOwnProperty('validationError')) {
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
|
const path = require('path');
|
||||||
|
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
||||||
|
const VirtualMachine = require('../../src/virtual-machine');
|
||||||
const Runtime = require('../../src/engine/runtime');
|
const Runtime = require('../../src/engine/runtime');
|
||||||
const MonitorRecord = require('../../src/engine/monitor-record');
|
const MonitorRecord = require('../../src/engine/monitor-record');
|
||||||
const {Map} = require('immutable');
|
const {Map} = require('immutable');
|
||||||
|
@ -108,3 +111,19 @@ test('getLabelForOpcode', t => {
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Project loaded emits runtime event', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
const projectUri = path.resolve(__dirname, '../fixtures/default.sb2');
|
||||||
|
const project = readFileToBuffer(projectUri);
|
||||||
|
let projectLoaded = false;
|
||||||
|
|
||||||
|
vm.runtime.addListener('PROJECT_LOADED', () => {
|
||||||
|
projectLoaded = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
vm.loadProject(project).then(() => {
|
||||||
|
t.equal(projectLoaded, true, 'Project load event emitted');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue