Skip to content

Documentation in TypeScript interfaces

Jakub Boháček edited this page Oct 18, 2021 · 1 revision

Just the documentation in TypeScript form.

For that sweet sweet intellisense in your code editor:

type Direction = "up" | "down" | "stopped";

interface Elevator {
  /**
   * Queue the elevator to go to specified floor number. If you specify true as second argument, the elevator will go to that floor directly, and then go to any other queued floors.
   *
   * @example
   * elevator.goToFloor(3); // Do it after anything else
   * elevator.goToFloor(2, true); // Do it before anything else
   */
  goToFloor: (floorNum: number, priority?: boolean) => void;
  /**
   * Clear the destination queue and stop the elevator if it is moving. Note that you normally don't need to stop elevators - it is intended for advanced solutions with in-transit rescheduling logic. Also, note that the elevator will probably not stop at a floor, so passengers will not get out.
   *
   * @example
   * elevator.stop();
   */
  stop: () => void;
  /**
   * Gets the floor number that the elevator currently is on.
   *
   * @example
   * if(elevator.currentFloor() === 0) {
   *   // Do something special?
   * }
   */
  currentFloor: () => number;
  /**
   * Gets or sets the going up indicator, which will affect passenger behaviour when stopping at floors.
   *
   * @example
   * if(elevator.goingUpIndicator()) {
   *   elevator.goingDownIndicator(false);
   * }
   */
  goingUpIndicator: (isGoingUp: boolean) => boolean;
  /**
   * Gets or sets the going down indicator, which will affect passenger behaviour when stopping at floors.
   *
   * @example
   * if(elevator.goingDownIndicator()) {
   *   elevator.goingUpIndicator(false);
   * }
   */
  goingDownIndicator: (isGoingDown: boolean) => boolean;
  /**
   * Gets the maximum number of passengers that can occupy the elevator at the same time.
   *
   * @example
   * if(elevator.maxPassengerCount() > 5) {
   *   // Use this elevator for something special, because it's big
   * }
   */
  maxPassengerCount: () => number;
  /**
   * Gets the load factor of the elevator. 0 means empty, 1 means full. Varies with passenger weights, which vary - not an exact measure.
   *
   * @example
   * if(elevator.loadFactor() < 0.4) {
   *   // Maybe use this elevator, since it's not full yet?
   * }
   */
  loadFactor: () => number;
  /**
   * Gets the direction the elevator is currently going to move toward. Can be "up", "down" or "stopped".
   */
  destinationDirection: () => Direction;
  /**
   * The current destination queue, meaning the floor numbers the elevator is scheduled to go to. Can be modified and emptied if desired. Note that you need to call checkDestinationQueue() for the change to take effect immediately.
   *
   * @example
   * elevator.destinationQueue = [];
   * elevator.checkDestinationQueue();
   */
  destinationQueue: number[];
  /**
   * Checks the destination queue for any new destinations to go to. Note that you only need to call this if you modify the destination queue explicitly.
   *
   * @example
   *
   * elevator.checkDestinationQueue();
   */
  checkDestinationQueue: () => void;
  /**
   * Gets the currently pressed floor numbers as an array.
   *
   * @example
   * if(elevator.getPressedFloors().length > 0) {
   *   // Maybe go to some chosen floor first?
   * }
   */
  getPressedFloors: () => number[];
  /**
   * Triggered when the elevator has completed all its tasks and is not doing anything.
   *
   * @example
   * elevator.on("idle", function() { ... });
   */
  on(event: "idle", cb: () => void): void;
  /**
   * Triggered when a passenger has pressed a button inside the elevator.
   *
   * @example
   * elevator.on("floor_button_pressed", function(floorNum) {
   *   // Maybe tell the elevator to go to that floor?
   * })
   */
  on(event: "floor_button_pressed", cb: (floorNum: number) => void): void;
  /**
   * Triggered slightly before the elevator will pass a floor. A good time to decide whether to stop at that floor. Note that this event is not triggered for the destination floor. Direction is either "up" or "down".
   *
   * @example
   * elevator.on("passing_floor", function(floorNum, direction) { ... });
   */
  on(
    event: "passing_floor",
    cb: (floorNum: number, direction: Direction) => void,
  ): void;
  /**
   * Triggered when the elevator has arrived at a floor.
   *
   * @example
   * elevator.on("stopped_at_floor", function(floorNum) {
   *   // Maybe decide where to go next?
   * })
   */
  on(event: "stopped_at_floor", cb: (floorNum: number) => void): void;
}

interface Floor {
  /**
   * Gets the floor number of the floor object.
   *
   * @example
   * if(floor.floorNum() > 3) { ... }
   */
  floorNum: () => number;
  /**
   * Triggered when someone has pressed the up button at a floor. Note that passengers will press the button again if they fail to enter an elevator.
   *
   * @example
   * floor.on("up_button_pressed", function() {
   *   // Maybe tell an elevator to go to this floor?
   * })
   */
  on(event: "up_button_pressed", cb: () => void): void;
  /**
   * Triggered when someone has pressed the down button at a floor. Note that passengers will press the button again if they fail to enter an elevator.
   *
   * @example
   * floor.on("down_button_pressed", function() {
   *   // Maybe tell an elevator to go to this floor?
   * })
   */
  on(event: "down_button_pressed", cb: () => void): void;
}
Clone this wiki locally