Merge pull request #1387 from paulkaplan/userdata-io-device

Add UserData ioDevice for setting and getting username from blocks
This commit is contained in:
Paul Kaplan 2018-07-24 15:13:15 -04:00 committed by GitHub
commit ac550d2d38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 4 deletions

View file

@ -308,9 +308,8 @@ class Scratch3SensingBlocks {
return 0; return 0;
} }
getUsername () { getUsername (args, util) {
// Logged out users get empty string. Return that for now. return util.ioQuery('userData', 'getUsername');
return '';
} }
} }

View file

@ -21,6 +21,7 @@ const DeviceManager = require('../io/deviceManager');
const Keyboard = require('../io/keyboard'); const Keyboard = require('../io/keyboard');
const Mouse = require('../io/mouse'); const Mouse = require('../io/mouse');
const MouseWheel = require('../io/mouseWheel'); const MouseWheel = require('../io/mouseWheel');
const UserData = require('../io/userData');
const Video = require('../io/video'); const Video = require('../io/video');
const StringUtil = require('../util/string-util'); const StringUtil = require('../util/string-util');
@ -259,6 +260,7 @@ class Runtime extends EventEmitter {
keyboard: new Keyboard(this), keyboard: new Keyboard(this),
mouse: new Mouse(this), mouse: new Mouse(this),
mouseWheel: new MouseWheel(this), mouseWheel: new MouseWheel(this),
userData: new UserData(),
video: new Video(this) video: new Video(this)
}; };

24
src/io/userData.js Normal file
View file

@ -0,0 +1,24 @@
class UserData {
constructor () {
this._username = '';
}
/**
* Handler for updating the username
* @param {object} data Data posted to this ioDevice.
* @property {!string} username The new username.
*/
postData (data) {
this._username = data.username;
}
/**
* Getter for username. Initially empty string, until set via postData.
* @returns {!string} The current username
*/
getUsername () {
return this._username;
}
}
module.exports = UserData;

View file

@ -3,6 +3,7 @@ const Sensing = require('../../src/blocks/scratch3_sensing');
const Runtime = require('../../src/engine/runtime'); const Runtime = require('../../src/engine/runtime');
const Sprite = require('../../src/sprites/sprite'); const Sprite = require('../../src/sprites/sprite');
const RenderedTarget = require('../../src/sprites/rendered-target'); const RenderedTarget = require('../../src/sprites/rendered-target');
const BlockUtility = require('../../src/engine/block-utility');
test('getPrimitives', t => { test('getPrimitives', t => {
const rt = new Runtime(); const rt = new Runtime();
@ -147,7 +148,8 @@ test('loud? boolean', t => {
test('username block', t => { test('username block', t => {
const rt = new Runtime(); const rt = new Runtime();
const sensing = new Sensing(rt); const sensing = new Sensing(rt);
const util = new BlockUtility(rt.sequencer);
t.equal(sensing.getUsername(), ''); t.equal(sensing.getUsername({}, util), '');
t.end(); t.end();
}); });

25
test/unit/io_userData.js Normal file
View file

@ -0,0 +1,25 @@
const test = require('tap').test;
const UserData = require('../../src/io/userData');
test('spec', t => {
const userData = new UserData();
t.type(userData, 'object');
t.type(userData.postData, 'function');
t.type(userData.getUsername, 'function');
t.end();
});
test('getUsername returns empty string initially', t => {
const userData = new UserData();
t.strictEquals(userData.getUsername(), '');
t.end();
});
test('postData sets the username', t => {
const userData = new UserData();
userData.postData({username: 'TEST'});
t.strictEquals(userData.getUsername(), 'TEST');
t.end();
});