Skip to content

Commit

Permalink
Merge pull request #7742 from afshin/statedb-namespace
Browse files Browse the repository at this point in the history
Update state database list method to query based on namespace match.
  • Loading branch information
Steven Silvester committed Jan 5, 2020
2 parents 5806fa4 + 3f6eb9a commit 0909afa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/filebrowser/src/browser.ts
Expand Up @@ -316,8 +316,8 @@ export namespace FileBrowser {
* The default is `true`.
*
* #### Notes
* The file browser model will need to be restored before for the file
* browser to start saving its state.
* The file browser model will need to be restored manually for the file
* browser to be able to save its state.
*/
restore?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/statedb/src/restorablepool.ts
Expand Up @@ -107,8 +107,8 @@ export class RestorablePool<
* @param obj - The object object being added.
*
* #### Notes
* The object passed into the tracker is added synchronously; its existence in
* the tracker can be checked with the `has()` method. The promise this method
* The object passed into the pool is added synchronously; its existence in
* the pool can be checked with the `has()` method. The promise this method
* returns resolves after the object has been added and saved to an underlying
* restoration connector, if one is available.
*/
Expand Down
11 changes: 7 additions & 4 deletions packages/statedb/src/statedb.ts
Expand Up @@ -177,8 +177,8 @@ export class StateDB<
/**
* Fetch a list from the database.
*/
private async _list(query?: string): Promise<{ ids: string[]; values: T[] }> {
const { ids, values } = await this._connector.list(query);
private async _list(namespace = ''): Promise<{ ids: string[]; values: T[] }> {
const { ids, values } = await this._connector.list(namespace);

return {
ids,
Expand Down Expand Up @@ -300,11 +300,14 @@ export namespace StateDB {

/**
* Retrieve the list of items available from the data connector.
*
* @param namespace - If not empty, only keys whose first token before `:`
* exactly match `namespace` will be returned, e.g. `foo` in `foo:bar`.
*/
async list(query = ''): Promise<{ ids: string[]; values: string[] }> {
async list(namespace = ''): Promise<{ ids: string[]; values: string[] }> {
return Object.keys(this._storage).reduce(
(acc, val) => {
if (val && val.indexOf(query) === 0) {
if (namespace === '' ? true : namespace === val.split(':')[0]) {
acc.ids.push(val);
acc.values.push(this._storage[val]);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/test-statedb/src/statedb.spec.ts
Expand Up @@ -120,7 +120,10 @@ describe('StateDB', () => {
'foo:qux',
'abc:def',
'abc:ghi',
'abc:jkl'
'abc:jkl',
'foo-two:bar',
'foo-two:baz',
'foo-two:qux'
];

await Promise.all(keys.map(key => db.save(key, { value: key })));
Expand Down

0 comments on commit 0909afa

Please sign in to comment.