cap:switchable-mode - switch mode

Capability used for things that can switch their mode.

if(thing.matches('cap:switchable-mode')) {
        console.log('Mode is', await thing.mode());

        // Switch the mode
        await thing.mode('new-mode');
}

API

mode([newMode])

Get or set the mode of the thing. Will return the mode as a string if no mode is specified. Will return a promise if a mode is specified.

Arguments:
  • newMode (string) – Optional mode to change to.
Returns:

Promise when switching mode, string if getting.

Example:

// Getting returns a string
const currentMode = await thing.mode();

// Switching returns a promise
thing.mode('new-mode')
        .then(result => console.log('Mode is now', result))
        .catch(err => console.log('Error occurred', err);

Protected methods

changeMode(newMode)

Abstract. Change to a new mode. Will be called whenever a change to the mode is requested. Implementations should call updateMode(newMode) before resolving to indicate that the mode has changed.

Arguments:
  • newMode (string) – The new mode of the thing.
Returns:

Promise if asynchronous.

Implementing capability

Implementations require that the method changeMode is implemented.

const { Thing, SwitchableMode } = require('abstract-things');

class Example extends Thing.with(SwitchableMode) {

        changeMode(newMode) {
                return swithcWithPromise(newMode)
                        .then(() => this.updateMode(newMode));
        }

}