@knotx/decorators 0.2.4 → 0.2.5

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,43 +312,89 @@ function createInjectProxy(...presets) {
312
312
  }
313
313
  const inject = createInjectProxy();
314
314
 
315
+ function observable() {
316
+ return function(_, context) {
317
+ if (context.static || typeof context.name !== "string") {
318
+ return;
319
+ }
320
+ context.addInitializer(function() {
321
+ const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
322
+ if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
323
+ subscribeEngine.call(this, (engine) => {
324
+ Reflect.defineProperty(this, context.name, {
325
+ get() {
326
+ return core.Runtime.getInstance().getValue(engine, {
327
+ paths: [],
328
+ matcher: () => this[behaviorKey]
329
+ });
330
+ },
331
+ set(value) {
332
+ this[behaviorKey].next(value);
333
+ }
334
+ });
335
+ });
336
+ }
337
+ });
338
+ };
339
+ }
340
+
341
+ function config() {
342
+ return function(_, context) {
343
+ if (context.static) {
344
+ return;
345
+ }
346
+ observable()(_, context);
347
+ context.addInitializer(function() {
348
+ const updateConfig = (config2) => {
349
+ this[context.name] = config2;
350
+ };
351
+ mergePluginHookFunction.call(this, "onInit", updateConfig);
352
+ mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
353
+ });
354
+ };
355
+ }
356
+
357
+ function OnInit(_, context) {
358
+ context.addInitializer(function() {
359
+ mergePluginHookFunction.call(this, "onInit", this[context.name]);
360
+ });
361
+ }
362
+ function OnDestroy(_, context) {
363
+ context.addInitializer(function() {
364
+ mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
365
+ });
366
+ }
367
+ function OnConfigChange(_, context) {
368
+ context.addInitializer(function() {
369
+ mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
370
+ });
371
+ }
372
+
315
373
  function register(property) {
316
374
  return function(_, context) {
317
- if (context.kind !== "field" && context.kind !== "getter") {
375
+ if (context.static) {
318
376
  return;
319
377
  }
378
+ observable()(_, context);
320
379
  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
- }
380
+ subscribeEngine.call(this, (engine) => {
381
+ engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
337
382
  });
338
383
  });
339
384
  };
340
385
  }
341
386
 
342
387
  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
- });
388
+ return function(_, context) {
389
+ if (context.static) {
390
+ return;
351
391
  }
392
+ context.addInitializer(function() {
393
+ var _a;
394
+ const layers = (_a = Reflect.get(this, core.getSymbol("layer"))) != null ? _a : {};
395
+ layers[context.name] = { layer: layer2, offset };
396
+ Reflect.set(this, core.getSymbol("layer"), layers);
397
+ });
352
398
  };
353
399
  }
354
400
 
@@ -437,160 +483,80 @@ function ContainerListener({
437
483
  return container.width > 0 && container.height > 0 ? /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { container, children }) : null;
438
484
  }
439
485
  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
- });
486
+ return function(_, context) {
487
+ if (context.static) {
488
+ return;
465
489
  }
490
+ layer(core.Layer.Foreground)(_, context);
491
+ context.addInitializer(function() {
492
+ const originalRender = this[context.name];
493
+ this[context.name] = () => {
494
+ const result = originalRender == null ? void 0 : originalRender.apply(this);
495
+ const engine = getEngine.call(this);
496
+ if (!result || !engine) {
497
+ return result;
498
+ }
499
+ return /* @__PURE__ */ jsxRuntime.jsx(
500
+ ContainerListener,
501
+ {
502
+ getContainer: () => (
503
+ // TODO (boen): fixhack
504
+ core.Runtime.getInstance().getValue(engine, { paths: ["container$"] })
505
+ ),
506
+ position,
507
+ offset,
508
+ children: result
509
+ }
510
+ );
511
+ };
512
+ });
466
513
  };
467
514
  }
468
515
 
469
516
  function nodeOperator() {
470
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
471
- if (isStatic || isPrivate) {
517
+ return function(_, context) {
518
+ if (context.static) {
472
519
  return;
473
520
  }
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)) {
521
+ context.addInitializer(function() {
522
+ const original = this[context.name];
523
+ subscribeEngine.call(this, (engine) => {
524
+ this[context.name] = (...args) => {
525
+ const operations = original.apply(this, args);
526
+ if (Array.isArray(operations) && operations.length > 0) {
486
527
  engine.dispatchNodeOperation({
487
528
  type: "batch",
488
529
  operations
489
530
  });
490
531
  }
491
532
  return operations;
492
- });
533
+ };
493
534
  });
494
535
  });
495
536
  };
496
537
  }
497
538
 
498
- const nodePipeSymbol = Symbol("nodePipe");
499
539
  function nodePipe() {
500
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
501
- if (isStatic || isPrivate) {
540
+ return function(_, context) {
541
+ if (context.static) {
502
542
  return;
503
543
  }
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;
544
+ context.addInitializer(function() {
545
+ subscribeEngine.call(this, (engine) => {
546
+ pushSubscription.call(this, engine.addNodePipe(this[context.name]()));
518
547
  });
519
548
  });
520
549
  };
521
550
  }
522
551
 
523
552
  function nodeType(type) {
524
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
525
- if (isStatic || isPrivate) {
526
- return;
527
- }
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");
532
- }
533
- requireEngine(this).subscribe((engine) => engine == null ? void 0 : engine.registerNodeRenderer(type, renderer.bind(this)));
534
- });
535
- };
536
- }
537
-
538
- function OnInit(originalMethod, context) {
539
- if (context.kind !== "method") {
540
- console.warn("OnInit decorator can only be applied to methods");
541
- return;
542
- }
543
- context.addInitializer(function() {
544
- this.onInit = (config) => {
545
- originalMethod.call(this, config);
546
- };
547
- });
548
- }
549
- function OnDestroy(originalMethod, context) {
550
- if (context.kind !== "method") {
551
- console.warn("OnDestroy decorator can only be applied to methods");
552
- return;
553
- }
554
- context.addInitializer(function() {
555
- this.onDestroy = () => {
556
- originalMethod.call(this);
557
- };
558
- });
559
- }
560
- function OnConfigChange(originalMethod, context) {
561
- if (context.kind !== "method") {
562
- console.warn("OnConfigChange decorator can only be applied to methods");
563
- return;
564
- }
565
- context.addInitializer(function() {
566
- this.onConfigChange = (config) => {
567
- originalMethod.call(this, config);
568
- };
569
- });
570
- }
571
-
572
- function observable() {
573
553
  return function(_, context) {
574
- if (context.kind !== "field") {
554
+ if (context.static) {
575
555
  return;
576
556
  }
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
+ pushSubscription.call(this, engine.registerNodeRenderer(type, this[context.name].bind(this)));
594
560
  });
595
561
  });
596
562
  };
@@ -599,6 +565,7 @@ function observable() {
599
565
  exports.OnConfigChange = OnConfigChange;
600
566
  exports.OnDestroy = OnDestroy;
601
567
  exports.OnInit = OnInit;
568
+ exports.config = config;
602
569
  exports.createPanelWrapper = createPanelWrapper;
603
570
  exports.edgeOperator = edgeOperator;
604
571
  exports.edgePipe = edgePipe;