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