@@ -36,66 +36,40 @@ export default class BookmarkManager {
36
36
* Method called when the bookmarks get updated when a transaction finished.
37
37
*
38
38
* This method will be called when auto-commit queries finish and when explicit transactions
39
- * get commited .
39
+ * get committed .
40
40
*
41
- * @param {string } database The database which the bookmarks belongs to
42
41
* @param {Iterable<string> } previousBookmarks The bookmarks used when starting the transaction
43
42
* @param {Iterable<string> } newBookmarks The new bookmarks received at the end of the transaction.
44
43
* @returns {void }
45
44
*/
46
- async updateBookmarks ( database : string , previousBookmarks : Iterable < string > , newBookmarks : Iterable < string > ) : Promise < void > {
45
+ async updateBookmarks ( previousBookmarks : Iterable < string > , newBookmarks : Iterable < string > ) : Promise < void > {
47
46
throw new Error ( 'Not implemented' )
48
47
}
49
48
50
49
/**
51
- * Method called by the driver to get the bookmarks for one specific database
50
+ * Method called by the driver to get the bookmarks.
52
51
*
53
- * @param {string } database The database which the bookmarks belong to
54
52
* @returns {Iterable<string> } The set of bookmarks
55
53
*/
56
- async getBookmarks ( database : string ) : Promise < Iterable < string > > {
57
- throw new Error ( 'Not implemented' )
58
- }
59
-
60
- /**
61
- * Method called by the driver for getting all the bookmarks.
62
- *
63
- * This method should return all bookmarks for all databases present in the BookmarkManager.
64
- *
65
- * @returns {Iterable<string> } The set of bookmarks
66
- */
67
- async getAllBookmarks ( ) : Promise < Iterable < string > > {
68
- throw new Error ( 'Not implemented' )
69
- }
70
-
71
- /**
72
- * Forget the databases and its bookmarks
73
- *
74
- * This method is not called by the driver. Forgetting unused databases is the user's responsibility.
75
- *
76
- * @param {Iterable<string> } databases The databases which the bookmarks will be removed for.
77
- */
78
- async forget ( databases : Iterable < string > ) : Promise < void > {
54
+ async getBookmarks ( ) : Promise < Iterable < string > > {
79
55
throw new Error ( 'Not implemented' )
80
56
}
81
57
}
82
58
83
59
export interface BookmarkManagerConfig {
84
- initialBookmarks ?: Map < string , Iterable < string > >
85
- bookmarksSupplier ?: ( database ?: string ) => Promise < Iterable < string > >
86
- bookmarksConsumer ?: ( database : string , bookmarks : Iterable < string > ) => Promise < void >
60
+ initialBookmarks ?: Iterable < string >
61
+ bookmarksSupplier ?: ( ) => Promise < Iterable < string > >
62
+ bookmarksConsumer ?: ( bookmarks : Iterable < string > ) => Promise < void >
87
63
}
88
64
89
65
/**
90
66
* @typedef {Object } BookmarkManagerConfig
91
67
*
92
68
* @since 5.0
93
69
* @experimental
94
- * @property {Map<string,Iterable<string>> } [initialBookmarks@experimental ] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
95
- * @property {function([database]: string):Promise<Iterable<string>> } [bookmarksSupplier] Called for supplying extra bookmarks to the BookmarkManager
96
- * 1. supplying bookmarks from the given database when the default BookmarkManager's `.getBookmarks(database)` gets called.
97
- * 2. supplying all the bookmarks when the default BookmarkManager's `.getAllBookmarks()` gets called
98
- * @property {function(database: string, bookmarks: Iterable<string>): Promise<void> } [bookmarksConsumer] Called when the set of bookmarks for database get updated
70
+ * @property {Iterable<string> } [initialBookmarks@experimental ] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
71
+ * @property {function():Promise<Iterable<string>> } [bookmarksSupplier] Called for supplying extra bookmarks to the BookmarkManager
72
+ * @property {function(bookmarks: Iterable<string>): Promise<void> } [bookmarksConsumer] Called when the set of bookmarks get updated
99
73
*/
100
74
/**
101
75
* Provides an configured {@link BookmarkManager} instance.
@@ -106,9 +80,7 @@ export interface BookmarkManagerConfig {
106
80
* @returns {BookmarkManager }
107
81
*/
108
82
export function bookmarkManager ( config : BookmarkManagerConfig = { } ) : BookmarkManager {
109
- const initialBookmarks = new Map < string , Set < string > > ( )
110
-
111
- config . initialBookmarks ?. forEach ( ( v , k ) => initialBookmarks . set ( k , new Set ( v ) ) )
83
+ const initialBookmarks = new Set ( config . initialBookmarks )
112
84
113
85
return new Neo4jBookmarkManager (
114
86
initialBookmarks ,
@@ -119,69 +91,36 @@ export function bookmarkManager (config: BookmarkManagerConfig = {}): BookmarkMa
119
91
120
92
class Neo4jBookmarkManager implements BookmarkManager {
121
93
constructor (
122
- private readonly _bookmarksPerDb : Map < string , Set < string > > ,
123
- private readonly _bookmarksSupplier ?: ( database ?: string ) => Promise < Iterable < string > > ,
124
- private readonly _bookmarksConsumer ?: ( database : string , bookmark : Iterable < string > ) => Promise < void >
94
+ private readonly _bookmarks : Set < string > ,
95
+ private readonly _bookmarksSupplier ?: ( ) => Promise < Iterable < string > > ,
96
+ private readonly _bookmarksConsumer ?: ( bookmark : Iterable < string > ) => Promise < void >
125
97
) {
126
98
127
99
}
128
100
129
- async updateBookmarks ( database : string , previousBookmarks : Iterable < string > , newBookmarks : Iterable < string > ) : Promise < void > {
130
- const bookmarks = this . _getOrInitializeBookmarks ( database )
101
+ async updateBookmarks ( previousBookmarks : Iterable < string > , newBookmarks : Iterable < string > ) : Promise < void > {
102
+ const bookmarks = this . _bookmarks
131
103
for ( const bm of previousBookmarks ) {
132
104
bookmarks . delete ( bm )
133
105
}
134
106
for ( const bm of newBookmarks ) {
135
107
bookmarks . add ( bm )
136
108
}
137
109
if ( typeof this . _bookmarksConsumer === 'function' ) {
138
- await this . _bookmarksConsumer ( database , [ ...bookmarks ] )
139
- }
140
- }
141
-
142
- private _getOrInitializeBookmarks ( database : string ) : Set < string > {
143
- let maybeBookmarks = this . _bookmarksPerDb . get ( database )
144
- if ( maybeBookmarks === undefined ) {
145
- maybeBookmarks = new Set ( )
146
- this . _bookmarksPerDb . set ( database , maybeBookmarks )
147
- }
148
- return maybeBookmarks
149
- }
150
-
151
- async getBookmarks ( database : string ) : Promise < Iterable < string > > {
152
- const bookmarks = new Set ( this . _bookmarksPerDb . get ( database ) )
153
-
154
- if ( typeof this . _bookmarksSupplier === 'function' ) {
155
- const suppliedBookmarks = await this . _bookmarksSupplier ( database ) ?? [ ]
156
- for ( const bm of suppliedBookmarks ) {
157
- bookmarks . add ( bm )
158
- }
110
+ await this . _bookmarksConsumer ( [ ...bookmarks ] )
159
111
}
160
-
161
- return [ ...bookmarks ]
162
112
}
163
113
164
- async getAllBookmarks ( ) : Promise < Iterable < string > > {
165
- const bookmarks = new Set < string > ( )
114
+ async getBookmarks ( ) : Promise < Iterable < string > > {
115
+ const bookmarks = new Set ( this . _bookmarks )
166
116
167
- for ( const [ , dbBookmarks ] of this . _bookmarksPerDb ) {
168
- for ( const bm of dbBookmarks ) {
169
- bookmarks . add ( bm )
170
- }
171
- }
172
117
if ( typeof this . _bookmarksSupplier === 'function' ) {
173
118
const suppliedBookmarks = await this . _bookmarksSupplier ( ) ?? [ ]
174
119
for ( const bm of suppliedBookmarks ) {
175
120
bookmarks . add ( bm )
176
121
}
177
122
}
178
123
179
- return bookmarks
180
- }
181
-
182
- async forget ( databases : Iterable < string > ) : Promise < void > {
183
- for ( const database of databases ) {
184
- this . _bookmarksPerDb . delete ( database )
185
- }
124
+ return [ ...bookmarks ]
186
125
}
187
126
}
0 commit comments