attachDurableObjects function

void attachDurableObjects(
  1. CloudflareWorkersDurableObjects instances
)

Implementation

void attachDurableObjects(CloudflareWorkersDurableObjects instances) {
  globalDurableObjects = js_util.jsify({
    for (final instance in instances.entries)
      instance.key: allowInterop(
          (interop.DurableObjectState state, interop.Environment env) {
        final cls = instance.value();

        // Attach the state and environment to the delegate.
        final delegate = cls.delegate;
        delegate.state = state;
        delegate.env = env;

        // Call the instance fetch handler, and return the delegate request.
        delegate.fetch = allowInterop((interop.Request requestObj) {
          return futureToPromise(Future(() async {
            final response = await cls.fetch(requestFromJsObject(requestObj));
            return response.delegate;
          }));
        });

        // Call the instance alarm handler.
        delegate.alarm = allowInterop(() {
          return futureToPromise(Future(() async {
            await cls.alarm();
          }));
        });

        return delegate;
      })
  });
}