Skip to content

Allow user to define network status handler #660

@theangchen

Description

@theangchen

Learned from reference app. Address challenge.

Interface goal

Provide a network configuration property that allows users to define a network status check handler, which will provide additional network information if it exists (default null), and a callback that should be invoked with a boolean value. The application will set its isOnline property to this value.

import { Application } from "@nmfs-radfish/radfish";

const app = new Application({
  network: {
    setIsOnline: async (networkInformation, callback) => {
      const response = await fetch("https://example.com")
      return callback(response.status === 200);
    }
  }
});

Additional the react-radfish useOfflineStatus hook should add an event listener for the online and offline events.

Warning

application.addEventListener and application.removeEventListener need to be implemented.

  const application = useApplication();
  const [isOffline, setIsOffline] = useState(!application.isOnline);


  const updateOnlineStatus = () => {
    setIsOffline(!application.isOnline);
  };

  useEffect(() => {
    updateOnlineStatus();

    application.addEventListener("online", updateOnlineStatus);
    application.addEventListener("offline", updateOnlineStatus);

    return () => {
      application.removeEventListener("online", updateOnlineStatus);
      application.removeEventListener("offline", updateOnlineStatus);
    };
  }, []);

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

Status

Needs Review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions