Add UserData ioDevice for setting and getting username from blocks

This commit is contained in:
Paul Kaplan 2018-07-23 09:38:58 -04:00
parent 378fd672fc
commit 6d4c6df692
5 changed files with 56 additions and 4 deletions

View file

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

View file

@ -21,6 +21,7 @@ const DeviceManager = require('../io/deviceManager');
const Keyboard = require('../io/keyboard');
const Mouse = require('../io/mouse');
const MouseWheel = require('../io/mouseWheel');
const UserData = require('../io/userData');
const Video = require('../io/video');
const StringUtil = require('../util/string-util');
@ -259,6 +260,7 @@ class Runtime extends EventEmitter {
keyboard: new Keyboard(this),
mouse: new Mouse(this),
mouseWheel: new MouseWheel(this),
userData: new UserData(),
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 Sprite = require('../../src/sprites/sprite');
const RenderedTarget = require('../../src/sprites/rendered-target');
const BlockUtility = require('../../src/engine/block-utility');
test('getPrimitives', t => {
const rt = new Runtime();
@ -147,7 +148,8 @@ test('loud? boolean', t => {
test('username block', t => {
const rt = new Runtime();
const sensing = new Sensing(rt);
const util = new BlockUtility(rt.sequencer);
t.equal(sensing.getUsername(), '');
t.equal(sensing.getUsername({}, util), '');
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();
});