Files
wren/test/await/await_fn_class_service.wren
T
retoor eadfc8e9cb feat: add await syntax for direct async function calls and new generator/phonebook apps
Add compiler support for calling async functions directly with `await fn(args)` syntax, automatically translating to `await fn.call(args)`. Introduce `create_merge` Makefile target for merging Wren source files. Include new `apps/generator.wren` source code generator using OpenRouter API, `apps/phonebook.wren` CLI phonebook with SQLite backend, and `apps/minitut.md` Wren tutorial reference. Update `example/await_demo.wren` with parameterized async functions, nested awaits, and expression usage. Fix JSON parsing in `example/openrouter_demo.wren` to strip markdown code fences. Document new await syntax in `manual/api/scheduler.html`.
2026-01-25 09:50:20 +00:00

66 lines
1.4 KiB
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "scheduler" for Scheduler, Future
import "timer" for Timer
class CacheService {
construct new() {
_cache = {}
}
get(key) {
var op = async { |k|
Timer.sleep(1)
return _cache.containsKey(k) ? _cache[k] : null
}
return await op(key)
}
set(key, value) {
var op = async { |k, v|
Timer.sleep(1)
_cache[k] = v
return true
}
return await op(key, value)
}
has(key) {
var op = async { |k|
return _cache.containsKey(k)
}
return await op(key)
}
clear() {
var op = async {
_cache = {}
return true
}
return await op()
}
getOrSet(key, defaultValue) {
if (this.has(key)) {
return this.get(key)
}
this.set(key, defaultValue)
return defaultValue
}
}
var cache = CacheService.new()
System.print(cache.has("foo")) // expect: false
System.print(cache.get("foo")) // expect: null
System.print(cache.set("foo", "bar")) // expect: true
System.print(cache.has("foo")) // expect: true
System.print(cache.get("foo")) // expect: bar
System.print(cache.clear()) // expect: true
System.print(cache.has("foo")) // expect: false
System.print(cache.getOrSet("baz", "default")) // expect: default
System.print(cache.get("baz")) // expect: default