@travetto/registry 3.0.0-rc.22 → 3.0.0-rc.24

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/README.md +61 -4
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -6,6 +6,10 @@
6
6
  **Install: @travetto/registry**
7
7
  ```bash
8
8
  npm install @travetto/registry
9
+
10
+ # or
11
+
12
+ yarn add @travetto/registry
9
13
  ```
10
14
 
11
15
  This module is the backbone for all "discovered" and "registered" behaviors within the framework. This is primarily used for building modules within the framework and not directly useful for application development.
@@ -39,12 +43,21 @@ interface Child {
39
43
  method: Function;
40
44
  }
41
45
 
46
+ function isComplete(o: Partial<Group>): o is Group {
47
+ return !!o;
48
+ }
49
+
42
50
  export class SampleRegistry extends MetadataRegistry<Group, Child> {
43
51
  /**
44
52
  * Finalize class after all metadata is collected
45
53
  */
46
54
  onInstallFinalize<T>(cls: Class<T>): Group {
47
- return this.getOrCreatePending(cls) as Group;
55
+ const pending: Partial<Group> = this.getOrCreatePending(cls);
56
+ if (isComplete(pending)) {
57
+ return pending;
58
+ } else {
59
+ throw new Error('Invalid Group');
60
+ }
48
61
  }
49
62
 
50
63
  /**
@@ -68,7 +81,51 @@ As the [DynamicFileLoader](https://github.com/travetto/travetto/tree/main/module
68
81
 
69
82
  ## Supporting Metadata
70
83
 
71
- For the registries to work properly, metadata needs to be collected about files and classes to uniquely identify them, especially across file reloads for the live flow. To achieve this, every `class` is decorated with additional fields. The data that is added is:
84
+ As mentioned in [Manifest](https://github.com/travetto/travetto/tree/main/module/manifest#readme "Support for project indexing, manifesting, along with file watching")'s readme, the framework produces hashes of methods, classes, and functions, to allow for detecting changes to individual parts of the codebase. During the live flow, various registries will inspect this information to determine if action should be taken.
72
85
 
73
-
74
- * `Ⲑid` represents a computed id that is tied to the file/class combination;
86
+ **Code: Sample Class Diffing**
87
+ ```typescript
88
+ #handleFileChanges(file: string, classes: Class[] = []): void {
89
+ const next = new Map<string, Class>(classes.map(cls => [cls.Ⲑid, cls] as const));
90
+
91
+ let prev = new Map<string, Class>();
92
+ if (this.#classes.has(file)) {
93
+ prev = new Map(this.#classes.get(file)!.entries());
94
+ }
95
+
96
+ const keys = new Set([...Array.from(prev.keys()), ...Array.from(next.keys())]);
97
+
98
+ if (!this.#classes.has(file)) {
99
+ this.#classes.set(file, new Map());
100
+ }
101
+
102
+ let changes = 0;
103
+
104
+ /**
105
+ * Determine delta based on the various classes (if being added, removed or updated)
106
+ */
107
+ for (const k of keys) {
108
+ if (!next.has(k)) {
109
+ changes += 1;
110
+ this.emit({ type: 'removing', prev: prev.get(k)! });
111
+ this.#classes.get(file)!.delete(k);
112
+ } else {
113
+ this.#classes.get(file)!.set(k, next.get(k)!);
114
+ if (!prev.has(k)) {
115
+ changes += 1;
116
+ this.emit({ type: 'added', curr: next.get(k)! });
117
+ } else {
118
+ const prevMeta = RootIndex.getFunctionMetadataFromClass(prev.get(k));
119
+ const nextMeta = RootIndex.getFunctionMetadataFromClass(next.get(k));
120
+ if (prevMeta?.hash !== nextMeta?.hash) {
121
+ changes += 1;
122
+ this.emit({ type: 'changed', curr: next.get(k)!, prev: prev.get(k) });
123
+ }
124
+ }
125
+ }
126
+ }
127
+ if (!changes) {
128
+ this.#emitter.emit('unchanged-file', file);
129
+ }
130
+ }
131
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/registry",
3
- "version": "3.0.0-rc.22",
3
+ "version": "3.0.0-rc.24",
4
4
  "description": "Patterns and utilities for handling registration of metadata and functionality for run-time use",
5
5
  "keywords": [
6
6
  "ast-transformations",
@@ -27,10 +27,10 @@
27
27
  "directory": "module/registry"
28
28
  },
29
29
  "dependencies": {
30
- "@travetto/base": "^3.0.0-rc.19"
30
+ "@travetto/base": "^3.0.0-rc.21"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^3.0.0-rc.21"
33
+ "@travetto/transformer": "^3.0.0-rc.22"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {