-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
81 lines (71 loc) · 2.24 KB
/
registry.ts
File metadata and controls
81 lines (71 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { RegistrableComponent } from "@/types/state/plugin";
/**
* A registry for Chartlets components.
*/
export interface Registry {
/**
* Register a React component that renders a Chartlets component.
*
* `component` can be any React component. However, if you want to register
* a custom, reactive component, then `component` must be of type
* `ComponentType<ComponentProps>` where a `ComponentProps` has at
* least the following two properties:
*
* - `type: string`: your component's type name.
* This will be the same as the `type` used for registration.
* - `onChange: ComponentChangeHandler`: an event handler
* that your component may call to signal change events.
*
* Both props are always be present plus. The component may also
* be passed any other props provided by the contribution.
*
* @param type The Chartlets component's unique type name.
* @param component A functional React component.
*/
register(type: string, component: RegistrableComponent): () => void;
/**
* Lookup the component of the provided type.
*
* @param type The Chartlets component's type name.
*/
lookup(type: string): RegistrableComponent | undefined;
/**
* Clears the registry.
* For testing only.
*/
clear(): void;
/**
* Get the type names of all registered components.
*/
types: string[];
}
// export for testing only
export class RegistryImpl implements Registry {
private components = new Map<string, RegistrableComponent>();
register(type: string, component: RegistrableComponent): () => void {
const oldComponent = this.components.get(type);
this.components.set(type, component);
return () => {
if (typeof oldComponent === "function") {
this.components.set(type, oldComponent);
} else {
this.components.delete(type);
}
};
}
lookup(type: string): RegistrableComponent | undefined {
return this.components.get(type);
}
clear() {
this.components.clear();
}
get types(): string[] {
return Array.from(this.components.keys());
}
}
/**
* The Chartlets component registry.
*
* Use `registry.register("C", C)` to register your own component `C`.
*/
export const registry = new RegistryImpl();