@knotx/decorators 0.2.4 → 0.2.6

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/dist/index.js CHANGED
@@ -3,92 +3,94 @@ import { BehaviorSubject, combineLatest } from 'rxjs';
3
3
  import { get } from 'lodash-es';
4
4
  import { jsx } from '@knotx/jsx/jsx-runtime';
5
5
 
6
- function requireEngine(plugin) {
7
- const engineSymbol = getSymbol("engine");
8
- if (!Reflect.has(plugin, engineSymbol)) {
9
- Reflect.set(plugin, engineSymbol, new BehaviorSubject(null));
10
- }
11
- return Reflect.get(plugin, engineSymbol);
6
+ function getEngine() {
7
+ return Reflect.get(this, getSymbol("engine")).value;
8
+ }
9
+ function subscribeEngine(callback) {
10
+ this.subscriptions.push(Reflect.get(this, getSymbol("engine")).subscribe((engine) => {
11
+ if (!engine)
12
+ return;
13
+ callback(engine);
14
+ }));
15
+ }
16
+ function pushSubscription(callback) {
17
+ this.subscriptions.push(callback);
18
+ }
19
+ function createBehaviorSubject(plugin, key, value) {
20
+ Reflect.set(plugin, key, new BehaviorSubject(value));
21
+ pushSubscription.call(plugin, () => {
22
+ var _a;
23
+ return (_a = Reflect.get(plugin, key)) == null ? void 0 : _a.complete();
24
+ });
25
+ return true;
26
+ }
27
+ function getPropertyBehaviorSubjectKey(key) {
28
+ return `__${key}$__`;
29
+ }
30
+ function mergePluginHookFunction(...[key, newFunction]) {
31
+ const original = this[key];
32
+ this[key] = (...args) => {
33
+ original == null ? void 0 : original.apply(this, args);
34
+ newFunction.apply(this, args);
35
+ };
12
36
  }
13
37
 
14
38
  function edgeOperator() {
15
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
16
- if (isStatic || isPrivate) {
39
+ return function(_, context) {
40
+ if (context.static) {
17
41
  return;
18
42
  }
19
- addInitializer(function() {
20
- const operator = Reflect.get(this, name);
21
- if (typeof operator !== "function") {
22
- throw new TypeError("Edge operator must be a function");
23
- }
24
- requireEngine(this).subscribe((engine) => {
25
- if (!engine)
26
- return;
27
- const boundOperator = operator.bind(this);
28
- Reflect.set(this, name, (...args) => {
29
- const operations = boundOperator(...args);
30
- if (Array.isArray(operations)) {
43
+ context.addInitializer(function() {
44
+ const original = this[context.name];
45
+ subscribeEngine.call(this, (engine) => {
46
+ this[context.name] = (...args) => {
47
+ const operations = original.apply(this, args);
48
+ if (Array.isArray(operations) && operations.length > 0) {
31
49
  engine.dispatchEdgeOperation({
32
50
  type: "batch",
33
51
  operations
34
52
  });
35
53
  }
36
54
  return operations;
37
- });
55
+ };
38
56
  });
39
57
  });
40
58
  };
41
59
  }
42
60
 
43
- const edgePipeSymbol = Symbol("edgePipe");
44
61
  function edgePipe() {
45
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
46
- if (isStatic || isPrivate) {
62
+ return function(_, context) {
63
+ if (context.static) {
47
64
  return;
48
65
  }
49
- addInitializer(function() {
50
- const pipeFactory = Reflect.get(this, name);
51
- if (typeof pipeFactory !== "function") {
52
- throw new TypeError("Edge pipe must be a function");
53
- }
54
- requireEngine(this).subscribe((engine) => {
55
- if (!engine)
56
- return;
57
- const pipe = pipeFactory.call(this);
58
- engine.addEdgePipe(pipe);
59
- if (!this[edgePipeSymbol]) {
60
- this[edgePipeSymbol] = {};
61
- }
62
- this[edgePipeSymbol][name] = pipe;
66
+ context.addInitializer(function() {
67
+ subscribeEngine.call(this, (engine) => {
68
+ pushSubscription.call(this, engine.addEdgePipe(this[context.name]()));
63
69
  });
64
70
  });
65
71
  };
66
72
  }
67
73
 
68
74
  function edgeType(type) {
69
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
70
- if (isStatic || isPrivate) {
75
+ return function(_, context) {
76
+ if (context.static) {
71
77
  return;
72
78
  }
73
- addInitializer(function() {
74
- const renderer = Reflect.get(this, name);
75
- if (typeof renderer !== "function") {
76
- throw new TypeError("Edge renderer must be a function");
77
- }
78
- requireEngine(this).subscribe((engine) => engine == null ? void 0 : engine.registerEdgeRenderer(type, renderer.bind(this)));
79
+ context.addInitializer(function() {
80
+ subscribeEngine.call(this, (engine) => {
81
+ pushSubscription.call(this, engine.registerEdgeRenderer(type, this[context.name].bind(this)));
82
+ });
79
83
  });
80
84
  };
81
85
  }
82
86
 
83
87
  function injectData(context, engineKey, selector) {
84
88
  context.addInitializer(function() {
85
- const engine$ = requireEngine(this);
86
89
  Reflect.defineProperty(this, context.name, {
87
90
  get() {
88
- const engine = engine$.value;
89
- if (!engine) {
91
+ const engine = getEngine.call(this);
92
+ if (!engine)
90
93
  return void 0;
91
- }
92
94
  const engineKey$ = `${String(engineKey)}$`;
93
95
  const paths = get(engine, engineKey$) ? [engineKey$] : [engineKey];
94
96
  return Runtime.getInstance().getValue(engine, { paths, selector });
@@ -100,7 +102,7 @@ function injectData(context, engineKey, selector) {
100
102
  function injectMethod(context, methodKey) {
101
103
  context.addInitializer(function() {
102
104
  Reflect.set(this, context.name, (...methodArgs) => {
103
- const engine = requireEngine(this).value;
105
+ const engine = getEngine.call(this);
104
106
  const engineMethod = engine == null ? void 0 : engine[methodKey];
105
107
  if (!engineMethod || typeof engineMethod !== "function") {
106
108
  throw new Error(`Engine method ${String(methodKey)} not found`);
@@ -112,10 +114,9 @@ function injectMethod(context, methodKey) {
112
114
 
113
115
  function injectPluginData(context, pluginName, property, selector) {
114
116
  context.addInitializer(function() {
115
- const engine$ = requireEngine(this);
116
117
  Reflect.defineProperty(this, context.name, {
117
118
  get() {
118
- const engine = engine$.value;
119
+ const engine = getEngine.call(this);
119
120
  if (!engine) {
120
121
  return void 0;
121
122
  }
@@ -229,7 +230,6 @@ function isBehaviorSubject(value) {
229
230
  function injectable(context, injectableSelector) {
230
231
  const injectableStateSymbol = Symbol(`injectable:${String(context.name)}`);
231
232
  context.addInitializer(function() {
232
- const engine$ = requireEngine(this);
233
233
  Reflect.set(this, injectableStateSymbol, {
234
234
  initialized: false,
235
235
  accessRecord: null,
@@ -241,7 +241,7 @@ function injectable(context, injectableSelector) {
241
241
  });
242
242
  Reflect.defineProperty(this, context.name, {
243
243
  get() {
244
- const engine = engine$.value;
244
+ const engine = getEngine.call(this);
245
245
  if (!engine) {
246
246
  return void 0;
247
247
  }
@@ -310,46 +310,20 @@ function createInjectProxy(...presets) {
310
310
  }
311
311
  const inject = createInjectProxy();
312
312
 
313
- function register(property) {
313
+ function layer(layer2, offset) {
314
314
  return function(_, context) {
315
- if (context.kind !== "field" && context.kind !== "getter") {
315
+ if (context.static) {
316
316
  return;
317
317
  }
318
318
  context.addInitializer(function() {
319
- const subject = new BehaviorSubject(Reflect.get(this, context.name));
320
- const engine$ = requireEngine(this);
321
- engine$.subscribe((engine) => engine == null ? void 0 : engine.registerPluginData(this.name, property, subject));
322
- Reflect.defineProperty(this, context.name, {
323
- get() {
324
- const engine = engine$.value;
325
- if (!engine) {
326
- return void 0;
327
- }
328
- return Runtime.getInstance().getValue(engine, { paths: ["_pluginDataContainer", this.name, property] });
329
- },
330
- set(value) {
331
- if (context.kind === "field") {
332
- subject.next(value);
333
- }
334
- }
335
- });
319
+ var _a;
320
+ const layers = (_a = Reflect.get(this, getSymbol("layer"))) != null ? _a : {};
321
+ layers[context.name] = { layer: layer2, offset };
322
+ Reflect.set(this, getSymbol("layer"), layers);
336
323
  });
337
324
  };
338
325
  }
339
326
 
340
- function layer(layer2, offset) {
341
- return function(_target, { name, kind, static: isStatic, private: isPrivate, addInitializer }) {
342
- if (kind === "method" && typeof name === "string" && !isStatic && !isPrivate) {
343
- addInitializer(function() {
344
- var _a;
345
- const layers = (_a = Reflect.get(this, getSymbol("layer"))) != null ? _a : {};
346
- layers[name] = { layer: layer2, offset };
347
- Reflect.set(this, getSymbol("layer"), layers);
348
- });
349
- }
350
- };
351
- }
352
-
353
327
  var __defProp = Object.defineProperty;
354
328
  var __defProps = Object.defineProperties;
355
329
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -435,163 +409,172 @@ function ContainerListener({
435
409
  return container.width > 0 && container.height > 0 ? /* @__PURE__ */ jsx(Wrapper, { container, children }) : null;
436
410
  }
437
411
  function panel(position, offset) {
438
- return function(_target, { name, kind, static: isStatic, private: isPrivate, addInitializer }) {
439
- if (kind === "method" && typeof name === "string" && !isStatic && !isPrivate) {
440
- addInitializer(function() {
441
- var _a;
442
- const layers = (_a = Reflect.get(this, getSymbol("layer"))) != null ? _a : {};
443
- const originalRender = Reflect.get(this, name);
444
- Reflect.set(this, name, function(...args) {
445
- const result = originalRender == null ? void 0 : originalRender.apply(this, args);
446
- const engine = requireEngine(this).value;
447
- if (!engine || !result) {
448
- return result;
449
- }
450
- return /* @__PURE__ */ jsx(
451
- ContainerListener,
452
- {
453
- getContainer: () => Runtime.getInstance().getValue(engine, { paths: ["container$"] }),
454
- position,
455
- offset,
456
- children: result
457
- }
458
- );
459
- });
460
- layers[name] = { layer: Layer.Foreground };
461
- Reflect.set(this, getSymbol("layer"), layers);
462
- });
412
+ return function(_, context) {
413
+ if (context.static) {
414
+ return;
463
415
  }
416
+ layer(Layer.Foreground)(_, context);
417
+ context.addInitializer(function() {
418
+ const originalRender = this[context.name];
419
+ this[context.name] = () => {
420
+ const result = originalRender == null ? void 0 : originalRender.apply(this);
421
+ const engine = getEngine.call(this);
422
+ if (!result || !engine) {
423
+ return result;
424
+ }
425
+ return /* @__PURE__ */ jsx(
426
+ ContainerListener,
427
+ {
428
+ getContainer: () => (
429
+ // TODO (boen): fixhack
430
+ Runtime.getInstance().getValue(engine, { paths: ["container$"] })
431
+ ),
432
+ position,
433
+ offset,
434
+ children: result
435
+ }
436
+ );
437
+ };
438
+ });
464
439
  };
465
440
  }
466
441
 
467
442
  function nodeOperator() {
468
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
469
- if (isStatic || isPrivate) {
443
+ return function(_, context) {
444
+ if (context.static) {
470
445
  return;
471
446
  }
472
- addInitializer(function() {
473
- const operator = Reflect.get(this, name);
474
- if (typeof operator !== "function") {
475
- throw new TypeError("Node operator must be a function");
476
- }
477
- requireEngine(this).subscribe((engine) => {
478
- if (!engine)
479
- return;
480
- const boundOperator = operator.bind(this);
481
- Reflect.set(this, name, (...args) => {
482
- const operations = boundOperator(...args);
483
- if (Array.isArray(operations)) {
447
+ context.addInitializer(function() {
448
+ const original = this[context.name];
449
+ subscribeEngine.call(this, (engine) => {
450
+ this[context.name] = (...args) => {
451
+ const operations = original.apply(this, args);
452
+ if (Array.isArray(operations) && operations.length > 0) {
484
453
  engine.dispatchNodeOperation({
485
454
  type: "batch",
486
455
  operations
487
456
  });
488
457
  }
489
458
  return operations;
490
- });
459
+ };
491
460
  });
492
461
  });
493
462
  };
494
463
  }
495
464
 
496
- const nodePipeSymbol = Symbol("nodePipe");
497
465
  function nodePipe() {
498
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
499
- if (isStatic || isPrivate) {
466
+ return function(_, context) {
467
+ if (context.static) {
500
468
  return;
501
469
  }
502
- addInitializer(function() {
503
- const pipeFactory = Reflect.get(this, name);
504
- if (typeof pipeFactory !== "function") {
505
- throw new TypeError("Node pipe must be a function");
506
- }
507
- requireEngine(this).subscribe((engine) => {
508
- if (!engine)
509
- return;
510
- const pipe = pipeFactory.call(this);
511
- engine.addNodePipe(pipe);
512
- if (!this[nodePipeSymbol]) {
513
- this[nodePipeSymbol] = {};
514
- }
515
- this[nodePipeSymbol][name] = pipe;
470
+ context.addInitializer(function() {
471
+ subscribeEngine.call(this, (engine) => {
472
+ pushSubscription.call(this, engine.addNodePipe(this[context.name]()));
516
473
  });
517
474
  });
518
475
  };
519
476
  }
520
477
 
521
478
  function nodeType(type) {
522
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
523
- if (isStatic || isPrivate) {
479
+ return function(_, context) {
480
+ if (context.static) {
524
481
  return;
525
482
  }
526
- addInitializer(function() {
527
- const renderer = Reflect.get(this, name);
528
- if (typeof renderer !== "function") {
529
- throw new TypeError("Node renderer must be a function");
483
+ context.addInitializer(function() {
484
+ subscribeEngine.call(this, (engine) => {
485
+ pushSubscription.call(this, engine.registerNodeRenderer(type, this[context.name].bind(this)));
486
+ });
487
+ });
488
+ };
489
+ }
490
+
491
+ function observable() {
492
+ return function(_, context) {
493
+ if (context.static || typeof context.name !== "string") {
494
+ return;
495
+ }
496
+ context.addInitializer(function() {
497
+ const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
498
+ if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
499
+ subscribeEngine.call(this, (engine) => {
500
+ Reflect.defineProperty(this, context.name, {
501
+ get() {
502
+ return Runtime.getInstance().getValue(engine, {
503
+ paths: [],
504
+ matcher: () => this[behaviorKey]
505
+ });
506
+ },
507
+ set(value) {
508
+ this[behaviorKey].next(value);
509
+ }
510
+ });
511
+ });
530
512
  }
531
- requireEngine(this).subscribe((engine) => engine == null ? void 0 : engine.registerNodeRenderer(type, renderer.bind(this)));
532
513
  });
533
514
  };
534
515
  }
535
516
 
536
- function OnInit(originalMethod, context) {
537
- if (context.kind !== "method") {
538
- console.warn("OnInit decorator can only be applied to methods");
539
- return;
540
- }
517
+ function config() {
518
+ return function(_, context) {
519
+ if (context.static) {
520
+ return;
521
+ }
522
+ observable()(_, context);
523
+ context.addInitializer(function() {
524
+ const updateConfig = (config2) => {
525
+ this[context.name] = config2;
526
+ };
527
+ mergePluginHookFunction.call(this, "onInit", updateConfig);
528
+ mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
529
+ });
530
+ };
531
+ }
532
+
533
+ function OnInit(_, context) {
541
534
  context.addInitializer(function() {
542
- this.onInit = (config) => {
543
- originalMethod.call(this, config);
544
- };
535
+ mergePluginHookFunction.call(this, "onInit", this[context.name]);
545
536
  });
546
537
  }
547
- function OnDestroy(originalMethod, context) {
548
- if (context.kind !== "method") {
549
- console.warn("OnDestroy decorator can only be applied to methods");
550
- return;
551
- }
538
+ function OnDestroy(_, context) {
552
539
  context.addInitializer(function() {
553
- this.onDestroy = () => {
554
- originalMethod.call(this);
555
- };
540
+ mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
556
541
  });
557
542
  }
558
- function OnConfigChange(originalMethod, context) {
559
- if (context.kind !== "method") {
560
- console.warn("OnConfigChange decorator can only be applied to methods");
561
- return;
562
- }
543
+ function OnConfigChange(_, context) {
563
544
  context.addInitializer(function() {
564
- this.onConfigChange = (config) => {
565
- originalMethod.call(this, config);
566
- };
545
+ mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
567
546
  });
568
547
  }
569
548
 
570
- function observable() {
549
+ function register(property) {
571
550
  return function(_, context) {
572
- if (context.kind !== "field") {
551
+ if (context.static) {
573
552
  return;
574
553
  }
554
+ observable()(_, context);
575
555
  context.addInitializer(function() {
576
- const subject = new BehaviorSubject(Reflect.get(this, context.name));
577
- const engine$ = requireEngine(this);
578
- Reflect.defineProperty(this, context.name, {
579
- get() {
580
- const engine = engine$.value;
581
- if (!engine) {
582
- return subject.value;
583
- }
584
- return Runtime.getInstance().getValue(engine, {
585
- paths: [],
586
- matcher: () => subject
587
- });
588
- },
589
- set(value) {
590
- subject.next(value);
591
- }
556
+ subscribeEngine.call(this, (engine) => {
557
+ engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
558
+ });
559
+ });
560
+ };
561
+ }
562
+
563
+ function tool(description, params) {
564
+ return function(_, context) {
565
+ if (context.static) {
566
+ return;
567
+ }
568
+ context.addInitializer(function() {
569
+ subscribeEngine.call(this, (engine) => {
570
+ engine.registerPluginTool(this.name, context.name, {
571
+ description,
572
+ params,
573
+ func: this[context.name].bind(this)
574
+ });
592
575
  });
593
576
  });
594
577
  };
595
578
  }
596
579
 
597
- export { OnConfigChange, OnDestroy, OnInit, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
580
+ export { OnConfigChange, OnDestroy, OnInit, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register, tool };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/decorators",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Decorators for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -28,19 +28,20 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@knotx/jsx": "0.2.4"
31
+ "@knotx/jsx": "0.2.6"
32
32
  },
33
33
  "dependencies": {
34
+ "jsonschema": "^1.5.0",
34
35
  "lodash-es": "^4.17.21",
35
36
  "rxjs": "^7.8.1",
36
- "@knotx/core": "0.2.4"
37
+ "@knotx/core": "0.2.6"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@types/lodash-es": "^4.14.12",
40
- "@knotx/build-config": "0.2.4",
41
- "@knotx/eslint-config": "0.2.4",
42
- "@knotx/jsx": "0.2.4",
43
- "@knotx/typescript-config": "0.2.4"
41
+ "@knotx/build-config": "0.2.6",
42
+ "@knotx/eslint-config": "0.2.6",
43
+ "@knotx/jsx": "0.2.6",
44
+ "@knotx/typescript-config": "0.2.6"
44
45
  },
45
46
  "scripts": {
46
47
  "build": "unbuild",