UNPKG

@stimulus-library/controllers

Version:

A library of useful controllers for Stimulus

25 lines (24 loc) 727 B
import { LoadBlockController } from "./load_block_controller"; import { useInterval } from "@stimulus-library/mixins"; export class PollBlockController extends LoadBlockController { connect() { if (!this.hasSecondsValue) { throw new Error("Required value `seconds` is missing"); } requestAnimationFrame(() => { this._timeout(); useInterval(this, this._timeout, this.secondsValue * 1000); }); } async _timeout() { await this.loadContent(); } } PollBlockController.targets = ["replace"]; PollBlockController.values = { endpoint: String, errorMessage: String, selector: String, maxRetries: Number, seconds: Number, };