class-this-write-method.ts 540 B

1234567891011121314151617
  1. // A method writes its argument into this.field; a second method reads it back. The write
  2. // through `this` mutates the shared instance, so the read connects writer -> reader.
  3. // Expected: source -> ask#1, ask#1 -> ask#2, ask#2 -> sink.
  4. const secret = await agent("writer").ask<string>("secret");
  5. class Store {
  6. value = "";
  7. store(x: string) {
  8. this.value = x;
  9. }
  10. reveal(): string {
  11. return this.value;
  12. }
  13. }
  14. const s = new Store();
  15. s.store(secret);
  16. const out = await agent("reader").ask<string>(`use ${s.reveal()}`);
  17. return out;