Repository: expo/expo
File: packages/expo-screen-orientation/src/ExpoScreenOrientation.web.ts (TypeScript)

Code before:
  // See: https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation
  if (typeof _legacyLockUniversal === 'function') {
    const legacyLock = _convertToLegacyOrientationLock(webOrientationLock);
    const isSuccess = _legacyLockUniversal.call(screen, legacyLock);
    if (!isSuccess) {
      throw new Error(
        `Applying orientation lock: ${JSON.stringify(webOrientationLock)} to device was denied`
      );
    }
    return;
  }

  throw new Error(
    `expo-screen-orientation: The browser doesn't support locking screen orientation.`
  );
}

let _lastWebOrientationLock: WebOrientationLock = WebOrientationLock.UNKNOWN;

class ExpoScreenOrientation extends NativeModule<ExpoOrientationEvents> {
  orientation: ScreenOrientation | null = Platform.canUseViewport
    ? screen.orientation || (screen as any).msOrientation || null
    : null;
  listener: (() => void) | undefined;

  async emitOrientationEvent() {
    const [orientationLock, orientation] = await Promise.all([
      getOrientationLockAsync(),
      getOrientationAsync(),
    ]);
    this.emit('expoDidUpdateDimensions', {
      orientationLock,
      orientationInfo: { orientation },
    });
  }
  startObserving() {
    this.listener = () => this.emitOrientationEvent();
    if (Platform.canUseEventListeners) {
      if (this.orientation && this.orientation.addEventListener) {
        this.orientation.addEventListener('change', this.listener);
      } else {
        window.addEventListener('orientationchange', this.listener);
      }
    }
  }
  stopObserving(): void {
    if (Platform.canUseEventListeners && this.listener) {
      if (this.orientation && this.orientation.removeEventListener) {
        this.orientation.removeEventListener('change', this.listener);
      } else {
        window.removeEventListener('orientationchange', this.listener);

Review comment (suggestion):
changed to arrow fn to keep `this` reference correct

Code after:
  // See: https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation
  if (typeof _legacyLockUniversal === 'function') {
    const legacyLock = _convertToLegacyOrientationLock(webOrientationLock);
    const isSuccess = _legacyLockUniversal.call(screen, legacyLock);
    if (!isSuccess) {
      throw new Error(
        `Applying orientation lock: ${JSON.stringify(webOrientationLock)} to device was denied`
      );
    }
    return;
  }

  throw new Error(
    `expo-screen-orientation: The browser doesn't support locking screen orientation.`
  );
}

let _lastWebOrientationLock: WebOrientationLock = WebOrientationLock.UNKNOWN;

class ExpoScreenOrientation extends NativeModule<ExpoOrientationEvents> {
  orientation: ScreenOrientation | null = Platform.canUseViewport
    ? screen.orientation || (screen as any).msOrientation || null
    : null;

  emitOrientationEvent = async () => {
    const [orientationLock, orientation] = await Promise.all([
      getOrientationLockAsync(),
      getOrientationAsync(),
    ]);
    this.emit('expoDidUpdateDimensions', {
      orientationLock,
      orientationInfo: { orientation },
    });
  };
  startObserving() {
    if (Platform.canUseEventListeners) {
      if (this.orientation?.addEventListener) {
        this.orientation.addEventListener('change', this.emitOrientationEvent);
      } else {
        window.addEventListener('orientationchange', this.emitOrientationEvent);
      }
    }
  }
  stopObserving(): void {
    if (Platform.canUseEventListeners) {
      if (this.orientation?.removeEventListener) {
        this.orientation.removeEventListener('change', this.emitOrientationEvent);
      } else {
        window.removeEventListener('orientationchange', this.emitOrientationEvent);
      }
    }