Skip to content

Commit 55ce356

Browse files
committed
switch to composition api
1 parent bec313a commit 55ce356

File tree

1 file changed

+79
-80
lines changed

1 file changed

+79
-80
lines changed

src/stores/contextKeyStore.ts

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,91 @@
11
import { defineStore } from 'pinia'
2+
import { reactive } from 'vue'
23

3-
/**
4-
* Store for context keys.
5-
* Holds arbitrary key-value pairs globally throughout the app.
6-
*/
7-
export const useContextKeyStore = defineStore('contextKeys', {
8-
state: () => ({
9-
contextKeys: {} as Record<string, any>
10-
}),
4+
export const useContextKeyStore = defineStore('contextKeys', () => {
5+
const contextKeys = reactive<Record<string, any>>({})
116

12-
getters: {
13-
/**
14-
* Get a stored context key by path
15-
* @param {string} path - The path to the context key (e.g., 'a.b.c').
16-
* @returns {any} The value of the context key, or undefined if not found.
17-
*/
18-
getContextKey:
19-
(state) =>
20-
(path: string): any => {
21-
if (!path) return undefined
22-
const keys = path.split('.')
23-
let node: any = state.contextKeys
24-
for (const key of keys) {
25-
if (!node || typeof node !== 'object' || !(key in node)) {
26-
return undefined
27-
}
28-
node = node[key]
29-
}
30-
return node
7+
/**
8+
* Get a stored context key by path
9+
* @param {string} path - The path to the context key (e.g., 'a.b.c').
10+
* @returns {any} The value of the context key, or undefined if not found.
11+
*/
12+
function getContextKey(path: string): any {
13+
if (!path) return undefined
14+
const keys = path.split('.')
15+
let node: any = contextKeys
16+
for (const key of keys) {
17+
if (!node || typeof node !== 'object' || !(key in node)) {
18+
return undefined
3119
}
32-
},
20+
node = node[key]
21+
}
22+
return node
23+
}
3324

34-
actions: {
35-
/**
36-
* Set or update a context key value at a given path
37-
* @param {string} path - The path to the context key (e.g., 'a.b.c').
38-
* @param {any} value - The value to set for the context key.
39-
*/
40-
setContextKey(path: string, value: any) {
41-
if (!path) return
42-
const keys = path.split('.')
43-
const last = keys.pop()!
44-
let node: any = this.contextKeys
45-
for (const key of keys) {
46-
if (typeof node[key] !== 'object' || node[key] === null) {
47-
node[key] = {}
48-
}
49-
node = node[key]
25+
/**
26+
* Set or update a context key value at a given path
27+
* @param {string} path - The path to the context key (e.g., 'a.b.c').
28+
* @param {any} value - The value to set for the context key.
29+
*/
30+
function setContextKey(path: string, value: any) {
31+
if (!path) return
32+
const keys = path.split('.')
33+
const last = keys.pop()!
34+
let node: any = contextKeys
35+
for (const key of keys) {
36+
if (typeof node[key] !== 'object' || node[key] === null) {
37+
node[key] = {}
5038
}
51-
node[last] = value
52-
},
39+
node = node[key]
40+
}
41+
node[last] = value
42+
}
5343

54-
/**
55-
* Remove a context key by path
56-
* @param {string} path - The path to the context key to remove (e.g., 'a.b.c').
57-
*/
58-
removeContextKey(path: string) {
59-
if (!path) return
60-
const keys = path.split('.')
61-
const last = keys.pop()
62-
if (!last) return
63-
const stack: any[] = [this.contextKeys]
64-
let node: any = this.contextKeys
65-
for (const key of keys) {
66-
if (typeof node[key] !== 'object' || node[key] === null) return
67-
node = node[key]
68-
stack.push(node)
69-
}
70-
delete node[last]
71-
// prune empty ancestors
72-
for (let i = stack.length - 1; i > 0; i--) {
73-
const parent = stack[i - 1]
74-
const key = keys[i - 1]
75-
if (
76-
parent[key] &&
77-
typeof parent[key] === 'object' &&
78-
Object.keys(parent[key]).length === 0
79-
) {
80-
delete parent[key]
81-
}
44+
/**
45+
* Remove a context key by path
46+
* @param {string} path - The path to the context key to remove (e.g., 'a.b.c').
47+
*/
48+
function removeContextKey(path: string) {
49+
if (!path) return
50+
const keys = path.split('.')
51+
const last = keys.pop()
52+
if (!last) return
53+
const stack: any[] = [contextKeys]
54+
let node: any = contextKeys
55+
for (const key of keys) {
56+
if (typeof node[key] !== 'object' || node[key] === null) return
57+
node = node[key]
58+
stack.push(node)
59+
}
60+
delete node[last]
61+
// prune empty ancestors
62+
for (let i = stack.length - 1; i > 0; i--) {
63+
const parent = stack[i - 1]
64+
const key = keys[i - 1]
65+
if (
66+
parent[key] &&
67+
typeof parent[key] === 'object' &&
68+
Object.keys(parent[key]).length === 0
69+
) {
70+
delete parent[key]
8271
}
83-
},
72+
}
73+
}
8474

85-
/**
86-
* Clear all context keys
87-
*/
88-
clearAllContextKeys() {
89-
this.contextKeys = {}
75+
/**
76+
* Clear all context keys
77+
*/
78+
function clearAllContextKeys() {
79+
for (const key in contextKeys) {
80+
delete contextKeys[key]
9081
}
9182
}
83+
84+
return {
85+
contextKeys,
86+
getContextKey,
87+
setContextKey,
88+
removeContextKey,
89+
clearAllContextKeys
90+
}
9291
})

0 commit comments

Comments
 (0)