Skip to content

Commit 72f8500

Browse files
authored
Merge pull request #114 from simonsobs/dev
Extract `onReady()` from duplicated code and related changes
2 parents 5ddf4b8 + 84b93c8 commit 72f8500

20 files changed

+198
-173
lines changed

eslint.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ export default [
116116
"no-debugger": isProduction ? "warn" : "off",
117117
"no-unused-vars": "off",
118118
"@typescript-eslint/no-var-requires": "off",
119+
"@typescript-eslint/no-explicit-any": "off",
120+
"@typescript-eslint/ban-ts-comment": "off",
119121
"prettier/prettier": ["warn", { printWidth: 88 }],
120122
},
121123
},

src/api/use-continuous-enabled-subscription.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useCtrlContinuousEnabledQuery,
66
useCtrlContinuousEnabledSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
interface _ContinuousEnabledSubscription {
1012
continuousEnabled: ComputedRef<boolean | undefined>;
@@ -13,8 +15,7 @@ interface _ContinuousEnabledSubscription {
1315
query: ReturnType<typeof useCtrlContinuousEnabledQuery>;
1416
}
1517

16-
type ContinuousEnabledSubscription = _ContinuousEnabledSubscription &
17-
PromiseLike<_ContinuousEnabledSubscription>;
18+
type ContinuousEnabledSubscription = OnReady<_ContinuousEnabledSubscription>;
1819

1920
export function useSubscribeContinuousEnabled(): ContinuousEnabledSubscription {
2021
const query = useCtrlContinuousEnabledQuery({
@@ -34,11 +35,5 @@ export function useSubscribeContinuousEnabled(): ContinuousEnabledSubscription {
3435

3536
const ret = { continuousEnabled, error, subscription, query };
3637

37-
return {
38-
...ret,
39-
async then(onFulfilled, onRejected) {
40-
await query;
41-
return Promise.resolve(ret).then(onFulfilled, onRejected);
42-
},
43-
};
38+
return onReady(ret, query);
4439
}

src/api/use-run-no-subscription.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useCtrlRunNoQuery,
66
useCtrlRunNoSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
interface _RunNoSubscription {
1012
runNo: ComputedRef<number | undefined>;
@@ -13,7 +15,7 @@ interface _RunNoSubscription {
1315
query: ReturnType<typeof useCtrlRunNoQuery>;
1416
}
1517

16-
type RunNoSubscription = _RunNoSubscription & PromiseLike<_RunNoSubscription>;
18+
type RunNoSubscription = OnReady<_RunNoSubscription>;
1719

1820
export function useSubscribeRunNo(): RunNoSubscription {
1921
const query = useCtrlRunNoQuery({
@@ -32,11 +34,5 @@ export function useSubscribeRunNo(): RunNoSubscription {
3234

3335
const ret = { runNo, error, subscription, query };
3436

35-
return {
36-
...ret,
37-
async then(onFulfilled, onRejected) {
38-
await query;
39-
return Promise.resolve(ret).then(onFulfilled, onRejected);
40-
},
41-
};
37+
return onReady(ret, query);
4238
}

src/api/use-schedule-auto-mode-mode-subscription.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useScheduleAutoModeModeQuery,
66
useScheduleAutoModeModeSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
// type AutoMode = "off" | "scheduler" | "queue";
1012

@@ -15,8 +17,7 @@ interface _ScheduleAutoModeModeSubscription {
1517
query: ReturnType<typeof useScheduleAutoModeModeQuery>;
1618
}
1719

18-
type ScheduleAutoModeModeSubscription = _ScheduleAutoModeModeSubscription &
19-
PromiseLike<_ScheduleAutoModeModeSubscription>;
20+
type ScheduleAutoModeModeSubscription = OnReady<_ScheduleAutoModeModeSubscription>;
2021

2122
export function useSubscribeScheduleAutoModeMode(): ScheduleAutoModeModeSubscription {
2223
const query = useScheduleAutoModeModeQuery({
@@ -36,11 +37,5 @@ export function useSubscribeScheduleAutoModeMode(): ScheduleAutoModeModeSubscrip
3637

3738
const ret = { autoModeMode, error, subscription, query };
3839

39-
return {
40-
...ret,
41-
async then(onFulfilled, onRejected) {
42-
await query;
43-
return Promise.resolve(ret).then(onFulfilled, onRejected);
44-
},
45-
};
40+
return onReady(ret, query);
4641
}

src/api/use-schedule-auto-mode-state-subscription.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useQScheduleAutoModeStateQuery,
66
useScheduleAutoModeStateSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
interface _ScheduleAutoModeStateSubscription {
1012
autoModeState: ComputedRef<string | undefined>;
@@ -13,8 +15,7 @@ interface _ScheduleAutoModeStateSubscription {
1315
query: ReturnType<typeof useQScheduleAutoModeStateQuery>;
1416
}
1517

16-
type ScheduleAutoModeStateSubscription = _ScheduleAutoModeStateSubscription &
17-
PromiseLike<_ScheduleAutoModeStateSubscription>;
18+
type ScheduleAutoModeStateSubscription = OnReady<_ScheduleAutoModeStateSubscription>;
1819

1920
export function useSubscribeScheduleAutoModeState(): ScheduleAutoModeStateSubscription {
2021
const query = useQScheduleAutoModeStateQuery({
@@ -34,11 +35,5 @@ export function useSubscribeScheduleAutoModeState(): ScheduleAutoModeStateSubscr
3435

3536
const ret = { autoModeState, error, subscription, query };
3637

37-
return {
38-
...ret,
39-
async then(onFulfilled, onRejected) {
40-
await query;
41-
return Promise.resolve(ret).then(onFulfilled, onRejected);
42-
},
43-
};
38+
return onReady(ret, query);
4439
}

src/api/use-schedule-queue-items-subscription.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
useScheduleQueueItemsQuery,
77
useScheduleQueueItemsSSubscription,
88
} from "@/graphql/codegen/generated";
9+
import { onReady } from "@/utils/on-ready";
10+
import type { OnReady } from "@/utils/on-ready";
911

1012
interface _ScheduleQueueItemsSubscription {
1113
items: ComputedRef<ScheduleQueueItem[] | undefined>;
@@ -15,8 +17,7 @@ interface _ScheduleQueueItemsSubscription {
1517
query: ReturnType<typeof useScheduleQueueItemsQuery>;
1618
}
1719

18-
type ScheduleQueueItemsSubscription = _ScheduleQueueItemsSubscription &
19-
PromiseLike<_ScheduleQueueItemsSubscription>;
20+
type ScheduleQueueItemsSubscription = OnReady<_ScheduleQueueItemsSubscription>;
2021

2122
export function useSubscribeScheduleQueueItems(): ScheduleQueueItemsSubscription {
2223
const query = useScheduleQueueItemsQuery({
@@ -37,11 +38,5 @@ export function useSubscribeScheduleQueueItems(): ScheduleQueueItemsSubscription
3738

3839
const ret = { items, loading, error, subscription, query };
3940

40-
return {
41-
...ret,
42-
async then(onFulfilled, onRejected) {
43-
await query;
44-
return Promise.resolve(ret).then(onFulfilled, onRejected);
45-
},
46-
};
41+
return onReady(ret, query);
4742
}

src/api/use-state-subscription.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useCtrlStateQuery,
66
useCtrlStateSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
interface _StateSubscription {
1012
state: ComputedRef<string | undefined>;
@@ -13,7 +15,7 @@ interface _StateSubscription {
1315
query: ReturnType<typeof useCtrlStateQuery>;
1416
}
1517

16-
type StateSubscription = _StateSubscription & PromiseLike<_StateSubscription>;
18+
type StateSubscription = OnReady<_StateSubscription>;
1719

1820
export function useSubscribeState(): StateSubscription {
1921
const query = useCtrlStateQuery({
@@ -32,11 +34,5 @@ export function useSubscribeState(): StateSubscription {
3234

3335
const ret = { state, error, subscription, query };
3436

35-
return {
36-
...ret,
37-
async then(onFulfilled, onRejected) {
38-
await query;
39-
return Promise.resolve(ret).then(onFulfilled, onRejected);
40-
},
41-
};
37+
return onReady(ret, query);
4238
}

src/api/use-trace_ids-subscription.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useCtrlTraceIdsQuery,
66
useCtrlTraceIdsSSubscription,
77
} from "@/graphql/codegen/generated";
8+
import { onReady } from "@/utils/on-ready";
9+
import type { OnReady } from "@/utils/on-ready";
810

911
interface _TraceIdsSubscription {
1012
traceIds: ComputedRef<number[] | undefined>;
@@ -13,7 +15,7 @@ interface _TraceIdsSubscription {
1315
query: ReturnType<typeof useCtrlTraceIdsQuery>;
1416
}
1517

16-
type TraceIdsSubscription = _TraceIdsSubscription & PromiseLike<_TraceIdsSubscription>;
18+
type TraceIdsSubscription = OnReady<_TraceIdsSubscription>;
1719

1820
export function useSubscribeTraceIds(): TraceIdsSubscription {
1921
const query = useCtrlTraceIdsQuery({
@@ -32,11 +34,5 @@ export function useSubscribeTraceIds(): TraceIdsSubscription {
3234

3335
const ret = { traceIds, error, subscription, query };
3436

35-
return {
36-
...ret,
37-
async then(onFulfilled, onRejected) {
38-
await query;
39-
return Promise.resolve(ret).then(onFulfilled, onRejected);
40-
},
41-
};
37+
return onReady(ret, query);
4238
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { ref } from "vue";
3+
import fc from "fast-check";
4+
5+
import { useSubscribeScheduleAutoModeState } from "@/api";
6+
import { onReady } from "@/utils/on-ready";
7+
8+
import { usePulling } from "../use-pulling";
9+
10+
vi.mock("@/api", () => ({
11+
useSubscribeScheduleAutoModeState: vi.fn(),
12+
}));
13+
14+
const AUTO_MODE_STATES = [
15+
"created",
16+
"off",
17+
"auto_waiting",
18+
"auto_pulling",
19+
"auto_running",
20+
];
21+
22+
const fcAutoModeState = () => fc.constantFrom(...AUTO_MODE_STATES);
23+
24+
describe("scratch", () => {
25+
beforeEach(() => {
26+
vi.clearAllMocks();
27+
});
28+
29+
afterEach(() => {
30+
vi.resetAllMocks();
31+
});
32+
33+
it("Property test", async () => {
34+
await fc.assert(
35+
fc.asyncProperty(fcAutoModeState(), async (auto_mode_state) => {
36+
// Initially `undefined`
37+
const autoModeState = ref(undefined as string | undefined);
38+
39+
// A value set when ready
40+
const ready = (async () => {
41+
await Promise.resolve();
42+
autoModeState.value = auto_mode_state;
43+
})();
44+
45+
type Sub = ReturnType<typeof useSubscribeScheduleAutoModeState>;
46+
47+
// Thenable
48+
const sub = onReady({ autoModeState }, ready) as unknown as Sub;
49+
50+
vi.mocked(useSubscribeScheduleAutoModeState).mockReturnValue(sub);
51+
52+
const { pulling } = await usePulling();
53+
const expected = auto_mode_state === "auto_pulling";
54+
expect(pulling.value).toBe(expected);
55+
}),
56+
);
57+
});
58+
});

src/components/schedule/auto-mode/auto-mode-button/modes/queue/Button.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { useDisplay } from "vuetify";
1717
import { VBottomSheet } from "vuetify/components/VBottomSheet";
1818
import { VMenu } from "vuetify/components/VMenu";
1919
20-
import { usePulling } from "../../usePulling";
20+
import { usePulling } from "../../use-pulling";
2121
import Dialog from "./Dialog.vue";
2222
2323
const { mobile } = useDisplay();

0 commit comments

Comments
 (0)