# Utilities AGS comes with some builtin utilities for common operations. ## File functions ### Reading files ```ts import { readFile, readFileAsync } from "ags/file" function readFile(path: string): string function readFileAsync(path: string): Promise ``` ### Writing files ```ts import { writeFile, writeFileAsync } from "ags/file" function writeFile(path: string, content: string): void function writeFileAsync(path: string, content: string): Promise ``` ### Monitoring files If `path` is a directory it will be recursively monitored. ```ts import { monitorFile } from "ags/file" function monitorFile( path: string, callback: (file: string, event: Gio.FileMonitorEvent) => void, ): Gio.FileMonitor ``` ## Timeouts and Intervals ```ts import { interval, timeout, idle, createPoll } from "ags/time" ``` You can use JavaScript native `setTimeout` or `setInterval` functions which return a [GLib.Source](https://docs.gtk.org/glib/struct.Source.html) instance. Alternatively you can use these functions provided by AGS, which return a `Timer` instance. ```ts class Timer extends GObject.Object { declare $signals: { now(): void cancelled(): void } cancel(): void } ``` ### Interval Will immediately execute the function and every `interval` millisecond. ```ts function interval(interval: number, callback?: () => void): Timer ``` ### Timeout Will execute the `callback` after `timeout` millisecond. ```ts function timeout(timeout: number, callback?: () => void): Timer ``` ### Idle Executes `callback` whenever there are no higher priority events pending. ```ts function idle(callback?: () => void): Timer ``` Example: ```ts const timer = interval(1000, () => { console.log("optional callback") }) timer.connect("now", () => { console.log("tick") }) timer.connect("cancelled", () => { console.log("cancelled") }) timer.cancel() ``` ### createPoll `createPoll` creates a signal that only polls when there is at least one subscriber. ```ts function createPoll( init: string, interval: number, exec: string | string[], ): Accessor function createPoll( init: T, interval: number, exec: string | string[], transform: (stdout: string, prev: T) => T, ): Accessor function createPoll( init: T, interval: number, fn: (prev: T) => T | Promise, ): Accessor ``` Example: ```tsx function Counter() { const counter = createPoll(0, 1000, (prev) => prev + 1) return