elevatorsaga icon indicating copy to clipboard operation
elevatorsaga copied to clipboard

Add a `floor.hall_button_pressed` event to the API

Open roryokane opened this issue 10 years ago • 1 comments

As a more convenient alternative to the existing up_button_pressed and down_button_pressed events, also fire a hall_button_pressed event.

up_button_pressed and down_button_pressed don’t pass any parameters to their callbacks. hall_button_pressed would pass one parameter, direction, which would be either "up" or "down". This allows it to do the work of both of the other events.

Right now, my algorithm handles hall button presses with this code:

floors.forEach(function(floor) {
    floor.on("up_button_pressed", function() {
        handleHallButtonPress(floor, "up");
    });
    floor.on("down_button_pressed", function() {
        handleHallButtonPress(floor, "down");
    });
});

function handleHallButtonPress(floor, direction) {
    // …
}

With the addition of hall_button_pressed, I could simplify it to this:

floors.forEach(function(floor) {
    floor.on("hall_button_pressed", function(direction) {
        // …
    });
}

It is true that hall_button_pressed is redundant with the other events, but I think that’s okay because it’s the more fundamental and generally useful event. I think most people will use just the hall_button_pressed event, and only specialized algorithms that treat up and down movement very differently will use the other two events instead.

Does this sound like a good addition to the API?

roryokane avatar Jan 25 '15 06:01 roryokane

Currently you can workaround it like this:

floors.forEach(function(floor) {
    floor.on("up_button_pressed down_button_pressed", function(event) {
        var direction = event.indexOf('up') == 0 ? 'up' : 'down';
        handleHallButtonPress(floor, direction);
    });
});

Envek avatar Jan 25 '15 14:01 Envek