@xen-orchestra/web-core 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ import type { FlagsConfig } from '@core/packages/collection/types.ts'
2
+ import { reactive } from 'vue'
3
+
4
+ export function useFlagRegistry<TFlag extends string>(_flags: FlagsConfig<TFlag> = []) {
5
+ const registry = reactive(new Map()) as Map<TFlag, Set<PropertyKey> | undefined>
6
+
7
+ const flags = Array.isArray(_flags) ? Object.fromEntries(_flags.map(flag => [flag, { multiple: true }])) : _flags
8
+
9
+ function isFlagDefined(flag: TFlag) {
10
+ return flags[flag] !== undefined
11
+ }
12
+
13
+ function isFlagged(id: PropertyKey, flag: TFlag) {
14
+ return registry.get(flag)?.has(id) ?? false
15
+ }
16
+
17
+ function toggleFlag(id: PropertyKey, flag: TFlag, forcedValue = !isFlagged(id, flag)) {
18
+ if (!registry.has(flag)) {
19
+ registry.set(flag, new Set())
20
+ }
21
+
22
+ if (forcedValue) {
23
+ if (!isMultipleAllowed(flag)) {
24
+ clearFlag(flag)
25
+ }
26
+ registry.get(flag)!.add(id)
27
+ } else {
28
+ registry.get(flag)!.delete(id)
29
+ }
30
+ }
31
+
32
+ function clearFlag(flag: TFlag) {
33
+ registry.set(flag, new Set())
34
+ }
35
+
36
+ function isMultipleAllowed(flag: TFlag) {
37
+ return flags[flag]?.multiple ?? false
38
+ }
39
+
40
+ return {
41
+ isFlagged,
42
+ isFlagDefined,
43
+ toggleFlag,
44
+ clearFlag,
45
+ isMultipleAllowed,
46
+ }
47
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xen-orchestra/web-core",
3
3
  "type": "module",
4
- "version": "0.18.0",
4
+ "version": "0.19.0",
5
5
  "private": false,
6
6
  "exports": {
7
7
  "./*": {