itwinjs-core icon indicating copy to clipboard operation
itwinjs-core copied to clipboard

Support asynchronous schema loading in `SchemaJsonLocater`

Open grigasp opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe.

At the moment SchemaJsonLocater loads requested schema synchronously, which causes the main thread to block when loaded schema is large or has many referenced schemas. Example:

image

Describe the solution you'd like

I'd like SchemaJsonLocater.getSchema to have an asynchronous implementation instead of relying on a synchronous one.

Describe alternatives you've considered

As a workaround, I extended SchemaJsonLocater and provided my own asynchronous implementation for getSchema.

grigasp avatar May 13 '24 11:05 grigasp

This is a known issue, #6674 I have bumped up the priority of this issue because you're seeing perf issues related to it. I'm also leaving both open because they have unique info.

Can you provide the async version you implemented?

ColinKerr avatar May 13 '24 13:05 ColinKerr

This is a known issue, #6674 I have bumped up the priority of this issue because you're seeing perf issues related to it. I'm also leaving both open because they have unique info.

Can you provide the async version you implemented?

class AsyncSchemaJsonLocater extends SchemaJsonLocater {
  #_getSchema: SchemaPropsGetter;
  public constructor(getSchema: SchemaPropsGetter) {
    super(getSchema);
    this.#_getSchema = getSchema;
  }
  public override async getSchema<T extends Schema>(
    schemaKey: Readonly<SchemaKey>,
    _matchType: SchemaMatchType,
    context: SchemaContext,
  ): Promise<T | undefined> {
    const schemaProps = this.#_getSchema(schemaKey.name);
    if (!schemaProps) {
      return undefined;
    }

    await BeDuration.wait(0);
    const schema = await Schema.fromJson(schemaProps, context);
    return schema as T;
  }
}

grigasp avatar May 15 '24 06:05 grigasp