@opensumi/ide-core-common 3.3.4-next-1727344885.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/lib/index.d.ts +1 -0
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/index.js +1 -0
  4. package/lib/index.js.map +1 -1
  5. package/lib/module.d.ts +4 -8
  6. package/lib/module.d.ts.map +1 -1
  7. package/lib/module.js +12 -4
  8. package/lib/module.js.map +1 -1
  9. package/lib/remote-service/README.md +377 -0
  10. package/lib/remote-service/data-store/decorators.d.ts +9 -0
  11. package/lib/remote-service/data-store/decorators.d.ts.map +1 -0
  12. package/lib/remote-service/data-store/decorators.js +51 -0
  13. package/lib/remote-service/data-store/decorators.js.map +1 -0
  14. package/lib/remote-service/data-store/index.d.ts +3 -0
  15. package/lib/remote-service/data-store/index.d.ts.map +1 -0
  16. package/lib/remote-service/data-store/index.js +6 -0
  17. package/lib/remote-service/data-store/index.js.map +1 -0
  18. package/lib/remote-service/data-store/select.d.ts +4 -0
  19. package/lib/remote-service/data-store/select.d.ts.map +1 -0
  20. package/lib/remote-service/data-store/select.js +36 -0
  21. package/lib/remote-service/data-store/select.js.map +1 -0
  22. package/lib/remote-service/data-store/store.d.ts +35 -0
  23. package/lib/remote-service/data-store/store.d.ts.map +1 -0
  24. package/lib/remote-service/data-store/store.js +57 -0
  25. package/lib/remote-service/data-store/store.js.map +1 -0
  26. package/lib/remote-service/index.d.ts +18 -0
  27. package/lib/remote-service/index.d.ts.map +1 -0
  28. package/lib/remote-service/index.js +44 -0
  29. package/lib/remote-service/index.js.map +1 -0
  30. package/package.json +5 -4
  31. package/src/index.ts +1 -0
  32. package/src/module.ts +12 -10
  33. package/src/remote-service/README.md +377 -0
  34. package/src/remote-service/data-store/decorators.ts +59 -0
  35. package/src/remote-service/data-store/index.ts +2 -0
  36. package/src/remote-service/data-store/select.ts +40 -0
  37. package/src/remote-service/data-store/store.ts +89 -0
  38. package/src/remote-service/index.ts +55 -0
@@ -0,0 +1,40 @@
1
+ import { isIterable } from '@opensumi/ide-utils';
2
+
3
+ export type Query = Record<string, any>;
4
+ export type Store<T> = Iterable<T> | Record<string, T> | Map<string, T>;
5
+
6
+ function makeMatcher(query: Query) {
7
+ const statements = [] as string[];
8
+ Object.entries(query).forEach(([key, value]) => {
9
+ statements.push(`item[${JSON.stringify(key)}] === ${JSON.stringify(value)}`);
10
+ });
11
+
12
+ const matcher = `
13
+ return ${statements.join(' && ')};
14
+ `;
15
+
16
+ return new Function('item', matcher) as (item: Query) => boolean;
17
+ }
18
+
19
+ export function select<I, T extends Store<I>>(items: T, query: Query): I[] {
20
+ const matcher = makeMatcher(query);
21
+ const result = [] as I[];
22
+
23
+ let _iterable: Iterable<any> | undefined;
24
+
25
+ if (items instanceof Map) {
26
+ _iterable = items.values();
27
+ } else if (isIterable(items)) {
28
+ _iterable = items;
29
+ } else {
30
+ _iterable = Object.values(items);
31
+ }
32
+
33
+ for (const item of _iterable) {
34
+ if (matcher(item)) {
35
+ result.push(item);
36
+ }
37
+ }
38
+
39
+ return result;
40
+ }
@@ -0,0 +1,89 @@
1
+ import extend from 'lodash/extend';
2
+
3
+ import { EventEmitter } from '@opensumi/events';
4
+
5
+ import { select } from './select';
6
+
7
+ export interface DataStore<Item> {
8
+ create(item: Item): Item;
9
+ find(query: Record<string, any>): Item[] | undefined;
10
+ size(query: Record<string, any>): number;
11
+ get(id: string, query?: Record<string, any>): Item | undefined;
12
+ update(id: string, item: Partial<Item>): void;
13
+ remove(id: string): void;
14
+ }
15
+
16
+ export interface DataStoreEvent<Item> extends Record<string, any> {
17
+ created: [item: Item];
18
+ updated: [oldItem: Item, newItem: Item];
19
+ removed: [item: Item];
20
+ }
21
+
22
+ export interface DataStoreOptions {
23
+ id?: string;
24
+ }
25
+
26
+ export class InMemoryDataStore<Item> extends EventEmitter<DataStoreEvent<Item>> implements DataStore<Item> {
27
+ private store = new Map<string, Item>();
28
+ private _uid = 0;
29
+ /**
30
+ * primary key
31
+ */
32
+ private id: string;
33
+
34
+ constructor(protected options?: DataStoreOptions) {
35
+ super();
36
+ this.id = options?.id || 'id';
37
+ }
38
+
39
+ create(item: Item): Item {
40
+ const id = item[this.id] || String(this._uid++);
41
+ const result = extend({}, item, { [this.id]: id }) as Item;
42
+
43
+ this.store.set(id, result);
44
+
45
+ this.emit('created', result);
46
+ return result;
47
+ }
48
+
49
+ find(query: Record<string, any>): Item[] | undefined {
50
+ return select(this.store, query);
51
+ }
52
+
53
+ size(query?: Record<string, any>): number {
54
+ if (!query) {
55
+ return this.store.size;
56
+ }
57
+
58
+ return this.find(query)?.length || 0;
59
+ }
60
+
61
+ get(id: string): Item | undefined {
62
+ return this.store.get(id);
63
+ }
64
+
65
+ has(id: string): boolean {
66
+ return this.store.has(id);
67
+ }
68
+
69
+ update(id: string, item: Partial<Item>): void {
70
+ const current = this.store.get(id);
71
+ if (!current) {
72
+ return;
73
+ }
74
+
75
+ const result = extend({}, current, item);
76
+ this.emit('updated', current, result);
77
+
78
+ this.store.set(id, result);
79
+ }
80
+
81
+ remove(id: string): void {
82
+ const item = this.store.get(id);
83
+ if (item) {
84
+ this.emit('removed', item);
85
+ }
86
+
87
+ this.store.delete(id);
88
+ }
89
+ }
@@ -0,0 +1,55 @@
1
+ import { ConstructorOf, Inject, Injector, Token, markInjectable, setParameters } from '@opensumi/di';
2
+
3
+ import { RPCProtocol } from '../types/rpc';
4
+
5
+ export * from './data-store';
6
+
7
+ export const CLIENT_ID_TOKEN = Symbol('CLIENT_ID_TOKEN');
8
+
9
+ const RemoteServiceInstantiateFlag = Symbol('Do_Not_Allow_Instantiate_RemoteService');
10
+
11
+ const RemoteServiceDataSymbol = Symbol('RemoteServiceData');
12
+
13
+ function storeRemoteServiceData(target: any, servicePath: string, protocol?: RPCProtocol<any>) {
14
+ Reflect.defineMetadata(RemoteServiceDataSymbol, { servicePath, protocol }, target);
15
+ }
16
+
17
+ export function getRemoteServiceData(target: any): { servicePath: string; protocol?: RPCProtocol<any> } | undefined {
18
+ return Reflect.getMetadata(RemoteServiceDataSymbol, target);
19
+ }
20
+
21
+ export function RemoteService(servicePath: string, protocol?: RPCProtocol<any>) {
22
+ return function <T extends new (...args: any[]) => any>(constructor: T) {
23
+ markInjectable(constructor);
24
+ setParameters(constructor, [Symbol]);
25
+ Inject(RemoteServiceInstantiateFlag)(constructor, '', 0);
26
+ storeRemoteServiceData(constructor, servicePath, protocol);
27
+ };
28
+ }
29
+
30
+ RemoteService.getName = function (service: Token | ConstructorOf<any>): string {
31
+ if (typeof service === 'function') {
32
+ return service.name;
33
+ }
34
+ return String(service);
35
+ };
36
+
37
+ export interface RemoteServiceInternal<Client = any> {
38
+ rpcClient: Client[];
39
+
40
+ setConnectionClientId?(clientId: string): void;
41
+ }
42
+
43
+ export function runInRemoteServiceContext(injector: Injector, fn: () => void): Injector {
44
+ injector.overrideProviders({
45
+ token: RemoteServiceInstantiateFlag,
46
+ useValue: RemoteServiceInstantiateFlag,
47
+ override: true,
48
+ });
49
+
50
+ fn();
51
+
52
+ injector.disposeOne(RemoteServiceInstantiateFlag);
53
+ injector.creatorMap.delete(RemoteServiceInstantiateFlag);
54
+ return injector;
55
+ }