-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.ts
33 lines (29 loc) · 1008 Bytes
/
vue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { onUnmounted, shallowRef } from "npm:vue";
import {
type AnyFeatureFlags,
FeatureFlags,
type TFeatureFlags,
} from "./mod.ts";
export function featureFlagsHookFactory<T extends AnyFeatureFlags>(
featureFlags: T,
) {
return () => {
type Schema = typeof featureFlags["schema"];
const state = shallowRef<Schema>({ ...featureFlags.store });
const setState = (
updates: Partial<TFeatureFlags<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;
};
}