@@ -20,7 +20,7 @@ export interface RegistrarTicket {
20
20
export interface RegistrarContext {
21
21
tickets : Reactive < Map < ID , Reactive < any > > >
22
22
lookup : ( index : number ) => ID | undefined
23
- register : ( ticket : Partial < RegistrarTicket > , id ?: ID ) => Reactive < any >
23
+ register : ( ticket ? : Partial < RegistrarTicket > , id ?: ID ) => Reactive < any >
24
24
unregister : ( id : ID ) => void
25
25
reindex : ( ) => void
26
26
}
@@ -42,18 +42,19 @@ export function useRegistrar<
42
42
const [ useRegistrarContext , _provideRegistrarContext ] = createContext < E > ( namespace )
43
43
44
44
const tickets = reactive ( new Map < ID , Z > ( ) )
45
+ const directory = reactive ( new Map < number , ID > ( ) )
45
46
46
47
function lookup ( index : number ) {
47
- for ( const [ id , item ] of tickets ) {
48
- if ( item . index === index ) return id
49
- }
50
- return undefined
48
+ return directory . get ( index )
51
49
}
52
50
53
51
function reindex ( ) {
52
+ directory . clear ( )
54
53
let index = 0
55
54
for ( const item of tickets . values ( ) ) {
56
- item . index = index ++
55
+ item . index = index
56
+ directory . set ( index , item . id )
57
+ index ++
57
58
}
58
59
}
59
60
@@ -65,12 +66,18 @@ export function useRegistrar<
65
66
} ) as Reactive < Z >
66
67
67
68
tickets . set ( item . id , item as any )
69
+ directory . set ( item . index , item . id )
68
70
69
71
return item
70
72
}
71
73
72
74
function unregister ( id : ID ) {
73
- tickets . delete ( id )
75
+ const item = tickets . get ( id )
76
+
77
+ if ( ! item ) return
78
+
79
+ directory . delete ( item . index )
80
+ tickets . delete ( item . id )
74
81
reindex ( )
75
82
}
76
83
0 commit comments