@lorion-org/registry-hub 1.0.0-beta.1 → 1.0.0-beta.2

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 (2) hide show
  1. package/package.json +8 -2
  2. package/src/index.ts +83 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lorion-org/registry-hub",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "Framework-free typed registry and registry-hub primitives.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,10 @@
22
22
  "types": "./dist/index.d.ts",
23
23
  "exports": {
24
24
  ".": {
25
+ "lorion-source": {
26
+ "types": "./src/index.ts",
27
+ "default": "./src/index.ts"
28
+ },
25
29
  "import": {
26
30
  "types": "./dist/index.d.ts",
27
31
  "default": "./dist/index.js"
@@ -35,7 +39,9 @@
35
39
  "files": [
36
40
  "dist",
37
41
  "examples",
38
- "LICENSE"
42
+ "LICENSE",
43
+ "src/**/*.ts",
44
+ "!src/**/*.spec.ts"
39
45
  ],
40
46
  "keywords": [
41
47
  "registry",
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ export type RegistryItem = {
2
+ id: string;
3
+ };
4
+
5
+ export type Registry<T extends RegistryItem = RegistryItem> = {
6
+ id: string;
7
+ get: (id: string) => T | undefined;
8
+ list: () => T[];
9
+ register: (items: T | T[]) => T[];
10
+ entries: () => Array<[string, T]>;
11
+ };
12
+
13
+ export type RegistryHub = {
14
+ createRegistry: <T extends RegistryItem>(id: string) => Registry<T>;
15
+ getRegistry: <T extends RegistryItem>(id: string) => Registry<T> | undefined;
16
+ register: <T extends RegistryItem>(registryId: string, items: T | T[]) => T[];
17
+ get: <T extends RegistryItem>(registryId: string, itemId: string) => T | undefined;
18
+ list: <T extends RegistryItem>(registryId: string) => T[];
19
+ entries: () => Array<[string, Registry<RegistryItem>]>;
20
+ };
21
+
22
+ type AnyRegistry = Registry<RegistryItem>;
23
+
24
+ export function createRegistry<T extends RegistryItem>(id: string): Registry<T> {
25
+ const map: Map<string, T> = new Map();
26
+
27
+ return {
28
+ id,
29
+ get: (itemId: string) => map.get(itemId),
30
+ list: () => Array.from(map.values()),
31
+ entries: () => Array.from(map.entries()),
32
+ register: (items: T | T[]) => {
33
+ const normalized: T[] = Array.isArray(items) ? items : [items];
34
+
35
+ for (const item of normalized) map.set(item.id, item);
36
+
37
+ return normalized;
38
+ },
39
+ };
40
+ }
41
+
42
+ export function createRegistryHub(): RegistryHub {
43
+ const registries: Map<string, AnyRegistry> = new Map();
44
+
45
+ return {
46
+ createRegistry: <T extends RegistryItem>(id: string) => {
47
+ const existing: AnyRegistry | undefined = registries.get(id);
48
+
49
+ if (existing) return existing as unknown as Registry<T>;
50
+
51
+ const created: Registry<T> = createRegistry<T>(id);
52
+ registries.set(id, created as unknown as AnyRegistry);
53
+
54
+ return created;
55
+ },
56
+ getRegistry: <T extends RegistryItem>(id: string) =>
57
+ registries.get(id) as Registry<T> | undefined,
58
+ register: <T extends RegistryItem>(registryId: string, items: T | T[]) => {
59
+ const registry: Registry<T> =
60
+ (registries.get(registryId) as Registry<T> | undefined) ?? createRegistry<T>(registryId);
61
+
62
+ if (!registries.has(registryId))
63
+ registries.set(registryId, registry as unknown as AnyRegistry);
64
+
65
+ return registry.register(items);
66
+ },
67
+ get: <T extends RegistryItem>(registryId: string, itemId: string) => {
68
+ const registry: Registry<T> | undefined = registries.get(registryId) as
69
+ | Registry<T>
70
+ | undefined;
71
+
72
+ return registry?.get(itemId);
73
+ },
74
+ list: <T extends RegistryItem>(registryId: string) => {
75
+ const registry: Registry<T> | undefined = registries.get(registryId) as
76
+ | Registry<T>
77
+ | undefined;
78
+
79
+ return registry?.list() ?? [];
80
+ },
81
+ entries: () => Array.from(registries.entries()),
82
+ };
83
+ }