Migrating variables functions to the "state" namespace.

This commit is contained in:
David Négrier 2021-07-07 22:14:59 +02:00
parent e65e8b2097
commit b1cb12861f
7 changed files with 117 additions and 69 deletions

View file

@ -1,20 +1,26 @@
WA.onInit().then(() => {
console.log('Trying to read variable "doorOpened" whose default property is true. This should display "true".');
console.log('doorOpened', WA.room.loadVariable('doorOpened'));
console.log('doorOpened', WA.state.loadVariable('doorOpened'));
console.log('Trying to set variable "not_exists". This should display an error in the console, followed by a log saying the error was caught.')
WA.room.saveVariable('not_exists', 'foo').catch((e) => {
WA.state.saveVariable('not_exists', 'foo').catch((e) => {
console.log('Successfully caught error: ', e);
});
console.log('Trying to set variable "myvar". This should work.');
WA.room.saveVariable('myvar', {'foo': 'bar'});
WA.state.saveVariable('myvar', {'foo': 'bar'});
console.log('Trying to read variable "myvar". This should display a {"foo": "bar"} object.');
console.log(WA.room.loadVariable('myvar'));
console.log(WA.state.loadVariable('myvar'));
console.log('Trying to set variable "myvar" using proxy. This should work.');
WA.state.myvar = {'baz': 42};
console.log('Trying to read variable "myvar" using proxy. This should display a {"baz": 42} object.');
console.log(WA.state.myvar);
console.log('Trying to set variable "config". This should not work because we are not logged as admin.');
WA.room.saveVariable('config', {'foo': 'bar'}).catch(e => {
WA.state.saveVariable('config', {'foo': 'bar'}).catch(e => {
console.log('Successfully caught error because variable "config" is not writable: ', e);
});
});

View file

@ -12,21 +12,21 @@
WA.onInit().then(() => {
console.log('After WA init');
const textField = document.getElementById('textField');
textField.value = WA.room.loadVariable('textField');
textField.value = WA.state.loadVariable('textField');
textField.addEventListener('change', function (evt) {
console.log('saving variable')
WA.room.saveVariable('textField', this.value);
WA.state.saveVariable('textField', this.value);
});
WA.room.onVariableChange('textField').subscribe((value) => {
WA.state.onVariableChange('textField').subscribe((value) => {
console.log('variable changed received')
textField.value = value;
});
document.getElementById('btn').addEventListener('click', () => {
console.log(WA.room.loadVariable('textField'));
document.getElementById('placeholder').innerText = WA.room.loadVariable('textField');
console.log(WA.state.loadVariable('textField'));
document.getElementById('placeholder').innerText = WA.state.loadVariable('textField');
});
});
})