Skip to content

Conversation

@Slessi
Copy link
Member

@Slessi Slessi commented Nov 26, 2025

Create useResourceList and useResourceGet composables to get a useWatch (now useResourceWatch) like API for get/list tasks (i.e. ResourceService.Get / ResourceService.List)

Will allow you do to things like this:

const { data, error, loading } = useResourceList<PlatformConfigSpec>({
  runtime: Runtime.Omni,
  resource: {
    namespace: VirtualNamespace,
    type: CloudPlatformConfigType,
  },
})

If you need fine grained control to run the query lazily or with abortion, you can add skip: true and use the supplied lazy function:

// Get the loadData function from the composable
const { data, error, loading, loadData } = useResourceList<PlatformConfigSpec>({
  skip: true, // prevents eager loading (like in `useResourceWatch`)
  runtime: Runtime.Omni,
  resource: {
    namespace: VirtualNamespace,
    type: CloudPlatformConfigType,
  },
})

async function onManualAction() {
  const abort = new AbortController()

  // newData will be the same data returned from the composable.
  // In here you will also have access to that data imperatively if useful.
  // Error / loading from composable will still be updated when calling lazily.
  // If an error was thrown newData will be undefined here.
  const newData = await loadData(abort)
}

Example refactor

Before:

const node = ref<string>()

watch(
  () => route.params.machine as string,
  async (machineId) => {
    if (!machineId) {
      node.value = undefined
      return
    }

    const nodename = await ResourceService.Get<Resource<ClusterMachineIdentitySpec>>(
      {
        type: ClusterMachineIdentityType,
        id: machineId,
        namespace: DefaultNamespace,
      },
      withRuntime(Runtime.Omni),
    )

    node.value = nodename.spec.nodename
  },
  { immediate: true },
)

After:

const { data } = useResourceGet<ClusterMachineIdentitySpec>(() => ({
  skip: !route.params.machine,
  runtime: Runtime.Omni,
  resource: {
    type: ClusterMachineIdentityType,
    id: route.params.machine as string,
    namespace: DefaultNamespace,
  },
}))

const node = computed(() => data.value?.spec.nodename)

@Slessi Slessi force-pushed the use-resource-get-list branch from 972c04c to 956bd10 Compare November 26, 2025 16:35
Create useResourceList and useResourceGet composables to get a useWatch (now useResourceWatch) like API for get/list tasks

Signed-off-by: Edward Sammut Alessi <[email protected]>
@Slessi Slessi force-pushed the use-resource-get-list branch from 956bd10 to fbbd9b8 Compare November 26, 2025 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant