mesh-decorators 1.0.0

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.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Mesh Decorators
2
+
3
+ Small framework for creating custom decorators for use with [Mesh IoC](https://github.com/MeshIoC/mesh-ioc).
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { createMemberDecorator, findMembers, invokeMethods } from 'mesh-decorators';
9
+
10
+ // 1. Create a decorator to track "init" methods
11
+ const init = createMemberDecorator('init');
12
+
13
+ class FooService {
14
+
15
+ // 2. Decorate members of the classes (can apply to methods and/or properties)
16
+ @init()
17
+ async setup() { /* ... */}
18
+
19
+ }
20
+
21
+ // 3. Bind the service(s) to a mesh
22
+ const mesh = new Mesh();
23
+ mesh.service(FooService);
24
+
25
+ // 4. Get all references to members decorated with @init
26
+ const initHandlers = findMembers('init', mesh);
27
+ // [{ target: <instance of FooService>, memberName: 'setup' }]
28
+
29
+ // 5. Invoke all @inmit methods
30
+ await Promise.all(invokeMethods('init', mesh));
31
+ ```
32
+
33
+ ## License
34
+
35
+ [ISC](https://en.wikipedia.org/wiki/ISC_license) © Boris Okunskiy
@@ -0,0 +1 @@
1
+ export * from './member.js';
@@ -0,0 +1,2 @@
1
+ export * from './member.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/main/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { Mesh } from 'mesh-ioc';
2
+ export interface MemberRef {
3
+ target: any;
4
+ memberName: string;
5
+ }
6
+ export declare const globalMemberRefMap: Map<string, MemberRef[]>;
7
+ /**
8
+ * Creates a decorator that tracks references to a particular class property or method in a global map.
9
+ * These member references can be subsequently accessed via `findMembers`.
10
+ */
11
+ export declare function createMemberDecorator(decoratorName: string): () => (target: any, memberName: string) => void;
12
+ /**
13
+ * Obtains a list of member references decorated by specified `decoratorName`.
14
+ *
15
+ * For each reference, the `target` is the actual instance of the class created by specified `mesh`.
16
+ */
17
+ export declare function findMembers(decoratorName: string, mesh: Mesh, recursive?: boolean): MemberRef[];
18
+ /**
19
+ * Convenience method to call all methods decorated with specified `decoratorName`.
20
+ *
21
+ * The order of results is generally not defined as decorators are called in the order the modules are imported.
22
+ */
23
+ export declare function invokeMethods(decoratorName: string, mesh: Mesh, recursive?: boolean, ...args: any[]): any[];
@@ -0,0 +1,51 @@
1
+ export const globalMemberRefMap = new Map();
2
+ /**
3
+ * Creates a decorator that tracks references to a particular class property or method in a global map.
4
+ * These member references can be subsequently accessed via `findMembers`.
5
+ */
6
+ export function createMemberDecorator(decoratorName) {
7
+ return function decorator() {
8
+ return (target, memberName) => {
9
+ const handlers = globalMemberRefMap.get(decoratorName) || [];
10
+ globalMemberRefMap.set(decoratorName, handlers);
11
+ handlers.push({
12
+ target: target.constructor,
13
+ memberName,
14
+ });
15
+ };
16
+ };
17
+ }
18
+ /**
19
+ * Obtains a list of member references decorated by specified `decoratorName`.
20
+ *
21
+ * For each reference, the `target` is the actual instance of the class created by specified `mesh`.
22
+ */
23
+ export function findMembers(decoratorName, mesh, recursive = true) {
24
+ const result = [];
25
+ const refs = globalMemberRefMap.get(decoratorName) || [];
26
+ const bindings = recursive ? mesh.allBindings() : mesh.bindings.entries();
27
+ for (const [key, binding] of bindings) {
28
+ if (binding.type === 'service') {
29
+ for (const { target, memberName } of refs) {
30
+ if (target === binding.class || target.isPrototypeOf(binding.class)) {
31
+ result.push({
32
+ target: mesh.resolve(key),
33
+ memberName,
34
+ });
35
+ }
36
+ }
37
+ }
38
+ }
39
+ return result;
40
+ }
41
+ /**
42
+ * Convenience method to call all methods decorated with specified `decoratorName`.
43
+ *
44
+ * The order of results is generally not defined as decorators are called in the order the modules are imported.
45
+ */
46
+ export function invokeMethods(decoratorName, mesh, recursive = true, ...args) {
47
+ const members = findMembers(decoratorName, mesh, recursive);
48
+ const results = members.map(ref => ref.target[ref.memberName](...args));
49
+ return results;
50
+ }
51
+ //# sourceMappingURL=member.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"member.js","sourceRoot":"","sources":["../../src/main/member.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAuB,CAAC;AAEjE;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,aAAqB;IACvD,OAAO,SAAS,SAAS;QACrB,OAAO,CAAC,MAAW,EAAE,UAAkB,EAAE,EAAE;YACvC,MAAM,QAAQ,GAAG,kBAAkB,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;YAC7D,kBAAkB,CAAC,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;YAChD,QAAQ,CAAC,IAAI,CAAC;gBACV,MAAM,EAAE,MAAM,CAAC,WAAW;gBAC1B,UAAU;aACb,CAAC,CAAC;QACP,CAAC,CAAC;IACN,CAAC,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,aAAqB,EAAE,IAAU,EAAE,SAAS,GAAG,IAAI;IAC3E,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,MAAM,IAAI,GAAG,kBAAkB,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IACzD,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IAC1E,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,QAAQ,EAAE,CAAC;QACpC,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC7B,KAAK,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC;gBACxC,IAAI,MAAM,KAAK,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;oBAClE,MAAM,CAAC,IAAI,CAAC;wBACR,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC;wBACzB,UAAU;qBACb,CAAC,CAAC;gBACP,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,aAAqB,EAAE,IAAU,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,IAAW;IAC7F,MAAM,OAAO,GAAG,WAAW,CAAC,aAAa,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;IAC5D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACxE,OAAO,OAAO,CAAC;AACnB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "mesh-decorators",
3
+ "version": "1.0.0",
4
+ "description": "Custom decorators for Mesh IoC",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "exports": {
8
+ ".": "./out/main/index.js"
9
+ },
10
+ "main": "out/main/index.js",
11
+ "types": "out/main/index.d.ts",
12
+ "files": [
13
+ "out/main/**/*"
14
+ ],
15
+ "scripts": {
16
+ "clean": "rm -rf out *.tsbuildinfo",
17
+ "dev": "npm run clean && tsc -b -w",
18
+ "compile": "npm run clean && tsc -b",
19
+ "lint": "eslint --ext=.js,.ts,.vue --cache .",
20
+ "test": "NODE_ENV=test mocha",
21
+ "preversion": "npm run lint",
22
+ "version": "npm run compile",
23
+ "postversion": "npm publish --access=public && git push origin main --tags"
24
+ },
25
+ "pre-commit": [
26
+ "lint"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+ssh://git@github.com/MeshIoc/mesh-decorators.git"
31
+ },
32
+ "keywords": [
33
+ "ioc",
34
+ "di",
35
+ "decorators"
36
+ ],
37
+ "author": "Boris Okunskiy",
38
+ "license": "ISC",
39
+ "devDependencies": {
40
+ "@nodescript/eslint-config": "^1.2.0",
41
+ "@types/mocha": "^8.2.3",
42
+ "@types/node": "^16.18.88",
43
+ "chalk": "^4.1.2",
44
+ "eslint": "^8.57.0",
45
+ "mocha": "^9.2.2",
46
+ "pre-commit": "^1.2.2",
47
+ "reflect-metadata": "^0.1.14",
48
+ "typescript": "^5.4.2"
49
+ },
50
+ "dependencies": {
51
+ "mesh-ioc": "^3.3.0"
52
+ }
53
+ }