githubEdit

Object Run

Executing objects as tasks.

In addition to schemas, Stof's capabilities also present an interesting abstraction above the functional layer - executing entire objects as tasks.

This is accomplished with the Obj.run(..) function in the Object Library (Obj), which executes all #[run] functions and fields, optionally in a user-specified order.

#[type]
Task: {
    ok: false
    result: null
    config: {
        endpoint: "https://myendpoint"
        schema: {
            // some schema to apply to the result
        }
    }
    
    #[run]
    fn exec() {
        const res = await Http.fetch(self.config.endpoint);
        
        const result = new {};
        parse(res.remove("text"), result, "json");
        
        self.ok = self.config.schema.schemafy(result);
        self.result = result;
    }
}

#[main]
fn main() {
    const task = new Task {
        config: new {
            endpoint: 'https://restcountries.com/v3.1/region/europe'
        }
    };
    task.run();

    assert(task.ok);
    for (const country in task.result.field) {
        pln(country.name);
    }
}

Ordering

Running Fields

Last updated