Skip to content

Allow to use useLDFlag in pinia store #16

@mslawins

Description

@mslawins

Hi!

I'm testing LaunchDarkly in Vue 3.2 through LaunchDarkly Client SDK v1.2.1. I don't want to pollute my application code with SDK calls directly in components. My idea is to move all LD handling into separate Pinia store slice. Rough idea shown below:

import type { Ref } from 'vue';
import { defineStore } from 'pinia';
import { useLDFlag } from 'launchdarkly-vue-client-sdk';

export const useFeatureFlagsStore = defineStore(
  'LaunchDarklyFeatureFlags',
  () => {
    const flagOne: Readonly<Ref<boolean>> = useLDFlag('flagOne', false);
    const flagTwo: () => Readonly<Ref<boolean>> = () =>
      useLDFlag('flagTwo', false);
    const flagThree = computed(() => useLDFlag('flagThree', false));

    return {
      flagOne,
      flagTwo,
      flagThree,
    };
  }
);

This does not work. useLDFlag description states:

Evaluates a single feature flag. Automatically subscribes to streamed updates
unless the `streaming` option was set to false.
Uses Vue's inject API, so will only work if run inside a Vue setup hook or `<script setup>`.

I was not able to find solution in the docs. Is there any known way to work around that? Unfortunately this is make or break situation for me when it comes to using LaunchDarkly altogether.

From my understanding this is somewhere between feature request and bug report. SDK should allow to do this kind of stuff otherwise it is incomplete at best.

Any help is appreciated. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions