WebAssembly / WebAssembly/component-model

Defining client callback type in WIT

Open
#223 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
1.4k
Forks
130
Avg merge
2d 1h
Merged PRs (30d)
15

Description

I'm trying to define a component interface in WIT format so that components (client) can pass callbacks (closures) to host. Host can then store the callback somewhere and then invoke the callback when certain event happens.

I'm a bit confuse how can I describe this using WIT format. I'm thinking of something like this:

package foo:bar

world client {
  resource callback {
    // Invokes the callback
    call: func()
  }
  
  // Passes the callback from client to host
  import register-callback: func(callback: callback)
  
  // The main function for the client
  export main: func()
}

But because I didn't export the resource (I can't do export callback apparently), I guess this is treated as if callback is implemented by the host, which is not I want. I can change it to:

package foo:bar

interface host {
  use callback-types.{callback}

  register-callback: func(callback: callback)
}

interface callback-types {
  resource callback {
    call: func()
  }
}

interface client {
  use callback-types.{callback}
  run: func()
}

world client-world {
  import host
  export client
}

But then, will host/callback and client/callback be treated as the same type?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue’s two WIT examples are the concrete entry points; start by tracing how resources are declared, imported, exported, and shared across the shown world. Read the discussion and relevant component-model specification material to determine whether the host and client callback types are identical and what supported design applies; done means an unambiguous answer or updated WIT guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
api, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.