Skip to content

Using Durable Object #120

@universse

Description

@universse

Hi, was trying out Durable Object with Router. It seems to work fine so just wondering if there's any gotcha, apart from some type issues.

class Multiplayer extends Actor {
  API = new Router()

  constructor(public state: Durable.State, public bindings: Bindings) {
    super(state, bindings)
    this.setupRouter()
  }


  setupRouter() {
    this.API.add(`GET`, `/`, () => {
      return send(200, `hello`)
    })
  }

  receive = this.API.run
}

Also, when I use setup function, miniflare@rc is throwing an error. I haven't managed to deploy yet.

export class Multiplayer extends Actor {
  value = 0

  constructor(public state: Durable.State, public bindings: Bindings) {
    super(state, bindings)

    // this works
    state.blockConcurrencyWhile(async () => {
       const stored = await state.storage.get(`value`)
       this.value = stored || 0
    })
  }
  
  // this doesn't
  async setup(state: Durable.State, bindings: Bindings) {
    const stored = await state.storage.get(`value`)
    this.value = stored || 0
  }
  
  receive(req: Request): Promise<Response> | Response {
    const url = new URL(req.url)

    switch (url.pathname) {
      case `/`:
        return send(200, this.value)
      default:
        return send(404, STATUS_CODES[`404`])
    }
  }
}}

This is the error. I will log at miniflare repo as well.

TypeError: Function.prototype.apply was called on [object Promise], which is a object and not a function
    at AsyncLocalStorage.run (node:async_hooks:320:14)
    at InputGate.runWithClosed (file:///C:/Users/.../node_modules/.pnpm/@miniflare+shared@2.0.0-rc.3/node_modules/@miniflare/shared/src/sync/gate.ts:101:37)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)   
C:\Users\...\worker-project:
 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  worker-rpoject@1.0.0 dev: `cross-env NODE_ENV=development miniflare --modules --debug --live-reload --port 3001`      
Exit status 1
 ELIFECYCLE  Command failed with exit 
code 1.

I'm not the most proficient in TypeScript so please let me know if I missed out something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions