mobx#isComputedProp TypeScript Examples

The following examples show how to use mobx#isComputedProp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChangeDetectionState.ts    From legend-studio with Apache License 2.0 6 votes vote down vote up
/**
   * Call `get hashCode()` on each element once so we trigger the first time we compute the hash for that element.
   * Notice that we do this in asynchronous manner to not block the main execution thread, as this is quiet an expensive
   * task.
   *
   * We also want to take advantage of `mobx computed` here so we save time when starting change detection. However,
   * since `mobx computed` does not track async contexts, we have to use the `keepAlive` option for `computed`
   *
   * To avoid memory leak potentially caused by `keepAlive`, we use `keepAlive` utility from `mobx-utils`
   * so we could manually dispose `keepAlive` later after we already done with starting change detection.
   */
  async preComputeGraphElementHashes(): Promise<void> {
    const startTime = Date.now();
    const disposers: IDisposer[] = [];
    if (this.editorStore.graphManagerState.graph.allOwnElements.length) {
      await Promise.all<void>(
        this.editorStore.graphManagerState.graph.allOwnElements.map((element) =>
          promisify(() => {
            if (isComputedProp(element, 'hashCode')) {
              disposers.push(keepAlive(element, 'hashCode'));
            }
            // manually trigger hash code computation
            element.hashCode;
          }),
        ),
      );
    }
    // save the `keepAlive` computation disposers to dispose after we start change detection
    // so the first call of change detection still get the benefit of computed value
    this.graphElementHashCodeKeepAliveComputationDisposers = disposers;
    this.editorStore.applicationStore.log.info(
      LogEvent.create(
        CHANGE_DETECTION_EVENT.CHANGE_DETECTION_GRAPH_HASHES_PRECOMPUTED,
      ),
      '[ASYNC]',
      Date.now() - startTime,
      'ms',
    );
  }