chevcast/shotgun

Proposed improvements to the contextChanged event

Closed this issue · 2 comments

It would be nice if the contextChanged event was triggered with the name of the variable.

setVar: function (name, value) {
    this.data[name] = value;
    shell.emit('contextChanged', this.data, name);
}
```js

I am not sure whether a setVar and delVar should be explicitly distinguishable from the event - we could find out from name and data which occurred but it may be neater for shotgun to  provide that info directly in the event signature.

v7.5.0 contains the change.

As a second argument you will now get a hash representing the change that occurred and the data for that change. The object contains an operation property that will be equal to "set" or "delete". It will also have a name property with the name of the context variable that was set or deleted. Lastly, if it is a set operation then it will contain a third property called value which will have the value that name is being set to.

shell.on('contextChanged', function (context, info) {
    switch (info.operation) {
        case 'set':
            console.log('Context variable "%s" was set to "%s".', info.name, info.value);
            break;
        case 'delete':
            console.log('Context variable "%s" was deleted.', info.name);
            break;
    }
}

Actually, v7.5.2 contains the real change. I've modified it slightly. I've removed the "set" operation and added "add" and "update". If the property being set is a new property then the operation will be "add". If the property being set already existed on the context then the operation will be "update".

shell.on('contextChanged', function (context, info) {
    switch (info.operation) {
        case 'add':
            console.log('New context variable "%s" was set to "%s".', info.name, info.value);
            break;
        case 'update':
            console.log('Existing context variable "%s" was set to "%s".', info.name, info.value);
            break;
        case 'delete':
            console.log('Context variable "%s" was deleted.', info.name);
            break;
    }
}