Add new api for customizing main loop