Skip to content

Commit

Permalink
Introduce vue hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed Oct 29, 2023
1 parent af25bc4 commit 65e82ad
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 5 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,45 @@ export default function Page() {
);
}
```

## Use with Vue.js

This library exposes a hook to use this library in a reactive manner with
Vue.js. The below snippets give an example as to how you _could_ implement this
library. It might differ for your usecase.

#### `feature-flags.ts`

This file contains all configuration for the feature flags

```ts
import { FeatureFlags } from "@theopensource-company/feature-flags";
import { featureFlagsHookFactory } from "@theopensource-company/feature-flags/vue";

export const featureFlags = new FeatureFlags({
schema: {
test: {
options: [true, false],
},
},
});

export const useFeatureFlags = featureFlagsHookFactory(featureFlags);
```

#### `SomePage.vue`

Some page which needs feature flags

```html
<script>
import { useFeatureFlags } from 'feature-flags.ts';
const [flags, setFlags] = useFeatureFlags();
</script>

<template>
<button v-on:click="setFlags({ test: !flags.test })">
{{ flags.toString() }}
</button>
</template>
```
15 changes: 11 additions & 4 deletions compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import project from "./project.json" assert { type: "json" };
await emptyDir("./npm");

await build({
entryPoints: ["./mod.ts", {
name: "./react",
path: "react.ts",
}],
entryPoints: [
"./mod.ts",
{
name: "./react",
path: "react.ts",
},
{
name: "./vue",
path: "vue.ts",
},
],
outDir: "./npm",
shims: {
// see JS docs for overview and more options
Expand Down
139 changes: 138 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { onUnmounted, shallowRef } from "npm:vue";
import { FeatureFlags, TFeatureFlags } from "./mod.ts";

// deno-lint-ignore no-explicit-any
type AnyFeatureFlags = FeatureFlags<any, any>;

export function featureFlagsHookFactory<T extends AnyFeatureFlags>(
featureFlags: T,
) {
return () => {
const state = shallowRef({ ...featureFlags.store });

const setState = (
updates: Partial<TFeatureFlags<(typeof featureFlags)["schema"]>>,
) => {
const flags = Object.keys(updates) as (keyof typeof updates)[];
flags.forEach((flag) => {
const v = updates[flag];
if (FeatureFlags.isValidValue(v)) {
featureFlags.set(flag, v);
}
});
};

const listener = () => state.value = { ...featureFlags.store };
featureFlags.subscribe(listener);
onUnmounted(() => featureFlags.unsubscribe(listener));

return [state, setState] as const;
};
}

0 comments on commit 65e82ad

Please sign in to comment.