Skip to content

Commit

Permalink
better types, add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
sashankaryal committed Feb 4, 2025
1 parent e8980a2 commit 2aeab12
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/packages/core/src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React, {
import { useRecoilCallback, useRecoilValue } from "recoil";
import { v4 as uuid } from "uuid";
import { QP_WAIT, QueryPerformanceToastEvent } from "../QueryPerformanceToast";
import { gridActivePathsLUT } from "../Sidebar/useShouldReloadSample";
import { gridActivePathsLUT } from "../Sidebar/useDetectNewActiveLabelFields";
import { gridCrop, gridSpacing, pageParameters } from "./recoil";
import useAt from "./useAt";
import useEscape from "./useEscape";
Expand Down
2 changes: 1 addition & 1 deletion app/packages/core/src/components/Grid/useRefreshers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LRUCache } from "lru-cache";
import { useEffect, useMemo } from "react";
import uuid from "react-uuid";
import { useRecoilValue } from "recoil";
import { gridActivePathsLUT } from "../Sidebar/useShouldReloadSample";
import { gridActivePathsLUT } from "../Sidebar/useDetectNewActiveLabelFields";
import { gridAt, gridOffset, gridPage } from "./recoil";

const MAX_LRU_CACHE_ITEMS = 510;
Expand Down
4 changes: 2 additions & 2 deletions app/packages/core/src/components/Grid/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fos from "@fiftyone/state";
import type { LRUCache } from "lru-cache";
import { useEffect } from "react";
import { useRecoilValue } from "recoil";
import { useShouldReloadSampleOnActiveFieldsChange } from "../Sidebar/useShouldReloadSample";
import { useDetectNewActiveLabelFields } from "../Sidebar/useDetectNewActiveLabelFields";

export default function useSelect(
getFontSize: () => number,
Expand All @@ -13,7 +13,7 @@ export default function useSelect(
) {
const { init, deferred } = fos.useDeferrer();

const getNewFields = useShouldReloadSampleOnActiveFieldsChange({
const getNewFields = useDetectNewActiveLabelFields({
modal: false,
});

Expand Down
4 changes: 2 additions & 2 deletions app/packages/core/src/components/Modal/use-looker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useRef, useState } from "react";
import { useErrorHandler } from "react-error-boundary";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { v4 as uuid } from "uuid";
import { useShouldReloadSampleOnActiveFieldsChange } from "../Sidebar/useShouldReloadSample";
import { useDetectNewActiveLabelFields } from "../Sidebar/useDetectNewActiveLabelFields";
import { useClearSelectedLabels, useShowOverlays } from "./ModalLooker";
import { useLookerOptionsUpdate, useModalContext } from "./hooks";
import useKeyEvents from "./use-key-events";
Expand Down Expand Up @@ -52,7 +52,7 @@ function useLooker<L extends fos.Lookers>({
!initialRef.current && looker.updateOptions(lookerOptions);
}, [looker, lookerOptions]);

const getNewFields = useShouldReloadSampleOnActiveFieldsChange({
const getNewFields = useDetectNewActiveLabelFields({
modal: true,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CachedLabels, LookerId } from "./useShouldReloadSample";
import { CachedLabels, LookerId } from "./useDetectNewActiveLabelFields";

/**
* Synchronizes which label fields a given looker has seen so far. Returns any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ export type CachedLabels = Set<string>;
export const gridActivePathsLUT = new Map<LookerId, CachedLabels>();
export const modalActivePathsLUT = new Map<LookerId, CachedLabels>();

export const useShouldReloadSampleOnActiveFieldsChange = ({
/**
* Detects newly introduced active label fields for a given looker. Returns a
* callback that, given a looker ID, merges and returns any fields not yet in
* the cache. Clears its cache when unmounted.
*
* @param modal - Whether this hook is used in a modal context
* @returns A function that accepts a looker ID and returns newly added fields
* or null if there are none
*/
export const useDetectNewActiveLabelFields = ({
modal,
}: {
modal: boolean;
Expand Down Expand Up @@ -38,7 +47,7 @@ export const useShouldReloadSampleOnActiveFieldsChange = ({
gridActivePathsLUT.clear();
}
};
}, []);
}, [modal]);

return getNewFields;
};
19 changes: 10 additions & 9 deletions app/packages/looker/src/worker/async-labels-rendering-manager.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { Lookers } from "@fiftyone/state";
import { v4 as uuid } from "uuid";
import { ProcessSample } from ".";
import { Coloring, Sample } from "..";
import { LookerUtils } from "../lookers/shared";
import { createWorker } from "../util";

export type AsyncLabelsRenderingJob = {
sample: any;
sample: Sample;
labels: string[];
lookerRef: Lookers;
resolve: (data: any) => void;
resolve: (data: Omit<WorkerResponse, "uuid">) => void;
reject: (error: Error) => void;
};

export type AsyncJobResolutionResult = {
sample: any;
coloring: any;
sample: Sample;
coloring: Coloring;
};

export type WorkerResponse = {
sample: any;
coloring: any;
sample: Sample;
coloring: Coloring;
uuid: string;
};

Expand All @@ -28,8 +29,8 @@ const MAX_WORKERS =

// global job queue and indexes
const jobQueue: AsyncLabelsRenderingJob[] = [];
const pendingJobs = new Map<any, AsyncLabelsRenderingJob>();
const processingSamples = new Set<any>();
const pendingJobs = new Map<Sample, AsyncLabelsRenderingJob>();
const processingSamples = new Set<Sample>();

const workerPool: Worker[] = Array.from({ length: MAX_WORKERS }, () =>
createWorker(LookerUtils.workerCallbacks)
Expand Down Expand Up @@ -107,8 +108,8 @@ const assignJobToFreeWorker = (job: AsyncLabelsRenderingJob) => {
});

const workerArgs: ProcessSample & { method: "processSample" } = {
sample,
method: "processSample",
sample: sample as ProcessSample["sample"],
coloring: job.lookerRef.state.options.coloring,
customizeColorSetting: job.lookerRef.state.options.customizeColorSetting,
colorscale: job.lookerRef.state.options.colorscale,
Expand Down

0 comments on commit 2aeab12

Please sign in to comment.