-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/core-common/__tests__/remote-service/data-store/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { InMemoryDataStore } from '../../../src/remote-service/data-store'; | ||
|
||
describe('InMemoryDataStore', () => { | ||
it('should work', () => { | ||
const users = [ | ||
{ user: 'barney', age: 36, active: true }, | ||
{ user: 'fred', age: 40, active: false }, | ||
{ user: 'pebbles', age: 1, active: true }, | ||
]; | ||
|
||
const store = new InMemoryDataStore<(typeof users)[0]>({ | ||
id: 'user', | ||
}); | ||
|
||
let addCount = 0; | ||
store.on('created', (i) => { | ||
addCount++; | ||
}); | ||
|
||
users.forEach((u) => { | ||
store.create(u); | ||
}); | ||
|
||
const userBarney = store.get('barney'); | ||
expect(userBarney).toEqual(users[0]); | ||
expect(store.size()).toBe(3); | ||
expect(addCount).toBe(3); | ||
|
||
const items = store.find({ active: true }); | ||
expect(items).toEqual([users[0], users[2]]); | ||
expect(store.size({ active: true })).toBe(2); | ||
|
||
store.on('updated', (oldValue, newValue) => { | ||
expect(oldValue.user).toBe('barney'); | ||
expect(oldValue.age).toBe(36); | ||
expect(newValue.age).toBe(37); | ||
}); | ||
|
||
store.update('barney', { age: 37 }); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/core-common/__tests__/remote-service/data-store/select.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import _ from 'lodash'; | ||
|
||
import { select } from '../../../src/remote-service/data-store/select'; | ||
|
||
describe('select', () => { | ||
it('should work', () => { | ||
const users = [ | ||
{ user: 'barney', age: 36, active: true }, | ||
{ user: 'fred', age: 40, active: false }, | ||
{ user: 'pebbles', age: 1, active: true }, | ||
]; | ||
|
||
const result = select(users, { age: 1, active: true }); | ||
expect(result).toEqual([{ user: 'pebbles', age: 1, active: true }]); | ||
|
||
const userObj = _.keyBy(users, 'user'); | ||
const result2 = select(userObj, { age: 1, active: true }); | ||
expect(result2).toEqual([{ user: 'pebbles', age: 1, active: true }]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/core-common/src/remote-service/data-store/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import extend from 'lodash/extend'; | ||
|
||
import { EventEmitter } from '@opensumi/events'; | ||
|
||
import { select } from './select'; | ||
|
||
export interface DataStore<Item> { | ||
create(item: Item): Item; | ||
find(query: Record<string, any>): Item[] | undefined; | ||
size(query: Record<string, any>): number; | ||
get(id: string, query?: Record<string, any>): Item | undefined; | ||
update(id: string, item: Partial<Item>): void; | ||
remove(id: string): void; | ||
} | ||
|
||
export interface DataStoreEvent<Item> extends Record<string, any> { | ||
created: [Item]; | ||
updated: [oldValue: Item, newValue: Item]; | ||
removed: [Item]; | ||
} | ||
|
||
export interface DataStoreOptions { | ||
id?: string; | ||
} | ||
|
||
export class InMemoryDataStore<Item> extends EventEmitter<DataStoreEvent<Item>> implements DataStore<Item> { | ||
private store = new Map<string, Item>(); | ||
private _uId = 0; | ||
private id: string; | ||
|
||
constructor(protected options?: DataStoreOptions) { | ||
super(); | ||
this.id = options?.id || 'id'; | ||
} | ||
|
||
create(item: Item): Item { | ||
const id = item[this.id] || String(this._uId++); | ||
const result = extend({}, item, { [this.id]: id }) as Item; | ||
|
||
this.store.set(id, result); | ||
|
||
this.emit('created', result); | ||
return result; | ||
} | ||
|
||
find(query: Record<string, any>): Item[] | undefined { | ||
return select(this.store, query); | ||
} | ||
|
||
size(query?: Record<string, any>): number { | ||
if (!query) { | ||
return this.store.size; | ||
} | ||
|
||
return this.find(query)?.length || 0; | ||
} | ||
|
||
get(id: string): Item | undefined { | ||
return this.store.get(id); | ||
} | ||
|
||
update(id: string, item: Partial<Item>): void { | ||
const current = this.store.get(id); | ||
if (!current) { | ||
return; | ||
} | ||
|
||
const result = extend({}, current, item); | ||
this.emit('updated', current, result); | ||
|
||
this.store.set(id, result); | ||
} | ||
|
||
remove(id: string): void { | ||
const item = this.store.get(id); | ||
if (item) { | ||
this.emit('removed', item); | ||
} | ||
|
||
this.store.delete(id); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/core-common/src/remote-service/data-store/select.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { isIterable } from '@opensumi/ide-utils'; | ||
|
||
export type Query = Record<string, any>; | ||
export type Store<T> = Iterable<T> | Record<string, T> | Map<string, T>; | ||
|
||
function makeMatcher(query: Query) { | ||
const statements = [] as string[]; | ||
Object.entries(query).forEach(([key, value]) => { | ||
statements.push(`item['${key}'] === ${value}`); | ||
}); | ||
|
||
const matcher = ` | ||
return ${statements.join(' && ')}; | ||
`; | ||
|
||
const func = new Function('item', matcher) as (item: Query) => boolean; | ||
|
||
return (item: Query) => func(item); | ||
} | ||
|
||
export function select<I, T extends Store<I>>(items: T, query: Query): I[] { | ||
const matcher = makeMatcher(query); | ||
const result = [] as I[]; | ||
|
||
let _iterable: Iterable<any> | undefined; | ||
|
||
if (items instanceof Map) { | ||
_iterable = items.values(); | ||
} else if (isIterable(items)) { | ||
_iterable = items; | ||
} else { | ||
_iterable = Object.values(items); | ||
} | ||
|
||
for (const item of _iterable) { | ||
if (matcher(item)) { | ||
result.push(item); | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters