@knotx/decorators 0.2.3 → 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.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,43 +310,89 @@ function createInjectProxy(...presets) {
310
310
  }
311
311
  const inject = createInjectProxy();
312
312
 
313
+ function observable() {
314
+ return function(_, context) {
315
+ if (context.static || typeof context.name !== "string") {
316
+ return;
317
+ }
318
+ context.addInitializer(function() {
319
+ const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
320
+ if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
321
+ subscribeEngine.call(this, (engine) => {
322
+ Reflect.defineProperty(this, context.name, {
323
+ get() {
324
+ return Runtime.getInstance().getValue(engine, {
325
+ paths: [],
326
+ matcher: () => this[behaviorKey]
327
+ });
328
+ },
329
+ set(value) {
330
+ this[behaviorKey].next(value);
331
+ }
332
+ });
333
+ });
334
+ }
335
+ });
336
+ };
337
+ }
338
+
339
+ function config() {
340
+ return function(_, context) {
341
+ if (context.static) {
342
+ return;
343
+ }
344
+ observable()(_, context);
345
+ context.addInitializer(function() {
346
+ const updateConfig = (config2) => {
347
+ this[context.name] = config2;
348
+ };
349
+ mergePluginHookFunction.call(this, "onInit", updateConfig);
350
+ mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
351
+ });
352
+ };
353
+ }
354
+
355
+ function OnInit(_, context) {
356
+ context.addInitializer(function() {
357
+ mergePluginHookFunction.call(this, "onInit", this[context.name]);
358
+ });
359
+ }
360
+ function OnDestroy(_, context) {
361
+ context.addInitializer(function() {
362
+ mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
363
+ });
364
+ }
365
+ function OnConfigChange(_, context) {
366
+ context.addInitializer(function() {
367
+ mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
368
+ });
369
+ }
370
+
313
371
  function register(property) {
314
372
  return function(_, context) {
315
- if (context.kind !== "field" && context.kind !== "getter") {
373
+ if (context.static) {
316
374
  return;
317
375
  }
376
+ observable()(_, context);
318
377
  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
- }
378
+ subscribeEngine.call(this, (engine) => {
379
+ engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
335
380
  });
336
381
  });
337
382
  };
338
383
  }
339
384
 
340
385
  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
- });
386
+ return function(_, context) {
387
+ if (context.static) {
388
+ return;
349
389
  }
390
+ context.addInitializer(function() {
391
+ var _a;
392
+ const layers = (_a = Reflect.get(this, getSymbol("layer"))) != null ? _a : {};
393
+ layers[context.name] = { layer: layer2, offset };
394
+ Reflect.set(this, getSymbol("layer"), layers);
395
+ });
350
396
  };
351
397
  }
352
398
 
@@ -435,163 +481,83 @@ function ContainerListener({
435
481
  return container.width > 0 && container.height > 0 ? /* @__PURE__ */ jsx(Wrapper, { container, children }) : null;
436
482
  }
437
483
  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
- });
484
+ return function(_, context) {
485
+ if (context.static) {
486
+ return;
463
487
  }
488
+ layer(Layer.Foreground)(_, context);
489
+ context.addInitializer(function() {
490
+ const originalRender = this[context.name];
491
+ this[context.name] = () => {
492
+ const result = originalRender == null ? void 0 : originalRender.apply(this);
493
+ const engine = getEngine.call(this);
494
+ if (!result || !engine) {
495
+ return result;
496
+ }
497
+ return /* @__PURE__ */ jsx(
498
+ ContainerListener,
499
+ {
500
+ getContainer: () => (
501
+ // TODO (boen): fixhack
502
+ Runtime.getInstance().getValue(engine, { paths: ["container$"] })
503
+ ),
504
+ position,
505
+ offset,
506
+ children: result
507
+ }
508
+ );
509
+ };
510
+ });
464
511
  };
465
512
  }
466
513
 
467
514
  function nodeOperator() {
468
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
469
- if (isStatic || isPrivate) {
515
+ return function(_, context) {
516
+ if (context.static) {
470
517
  return;
471
518
  }
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)) {
519
+ context.addInitializer(function() {
520
+ const original = this[context.name];
521
+ subscribeEngine.call(this, (engine) => {
522
+ this[context.name] = (...args) => {
523
+ const operations = original.apply(this, args);
524
+ if (Array.isArray(operations) && operations.length > 0) {
484
525
  engine.dispatchNodeOperation({
485
526
  type: "batch",
486
527
  operations
487
528
  });
488
529
  }
489
530
  return operations;
490
- });
531
+ };
491
532
  });
492
533
  });
493
534
  };
494
535
  }
495
536
 
496
- const nodePipeSymbol = Symbol("nodePipe");
497
537
  function nodePipe() {
498
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
499
- if (isStatic || isPrivate) {
538
+ return function(_, context) {
539
+ if (context.static) {
500
540
  return;
501
541
  }
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;
542
+ context.addInitializer(function() {
543
+ subscribeEngine.call(this, (engine) => {
544
+ pushSubscription.call(this, engine.addNodePipe(this[context.name]()));
516
545
  });
517
546
  });
518
547
  };
519
548
  }
520
549
 
521
550
  function nodeType(type) {
522
- return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
523
- if (isStatic || isPrivate) {
524
- return;
525
- }
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");
530
- }
531
- requireEngine(this).subscribe((engine) => engine == null ? void 0 : engine.registerNodeRenderer(type, renderer.bind(this)));
532
- });
533
- };
534
- }
535
-
536
- function OnInit(originalMethod, context) {
537
- if (context.kind !== "method") {
538
- console.warn("OnInit decorator can only be applied to methods");
539
- return;
540
- }
541
- context.addInitializer(function() {
542
- this.onInit = (config) => {
543
- originalMethod.call(this, config);
544
- };
545
- });
546
- }
547
- function OnDestroy(originalMethod, context) {
548
- if (context.kind !== "method") {
549
- console.warn("OnDestroy decorator can only be applied to methods");
550
- return;
551
- }
552
- context.addInitializer(function() {
553
- this.onDestroy = () => {
554
- originalMethod.call(this);
555
- };
556
- });
557
- }
558
- function OnConfigChange(originalMethod, context) {
559
- if (context.kind !== "method") {
560
- console.warn("OnConfigChange decorator can only be applied to methods");
561
- return;
562
- }
563
- context.addInitializer(function() {
564
- this.onConfigChange = (config) => {
565
- originalMethod.call(this, config);
566
- };
567
- });
568
- }
569
-
570
- function observable() {
571
551
  return function(_, context) {
572
- if (context.kind !== "field") {
552
+ if (context.static) {
573
553
  return;
574
554
  }
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
+ pushSubscription.call(this, engine.registerNodeRenderer(type, this[context.name].bind(this)));
592
558
  });
593
559
  });
594
560
  };
595
561
  }
596
562
 
597
- export { OnConfigChange, OnDestroy, OnInit, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
563
+ export { OnConfigChange, OnDestroy, OnInit, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/decorators",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Decorators for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -28,19 +28,19 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@knotx/jsx": "0.2.3"
31
+ "@knotx/jsx": "0.2.5"
32
32
  },
33
33
  "dependencies": {
34
34
  "lodash-es": "^4.17.21",
35
35
  "rxjs": "^7.8.1",
36
- "@knotx/core": "0.2.3"
36
+ "@knotx/core": "0.2.5"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/lodash-es": "^4.14.12",
40
- "@knotx/build-config": "0.2.3",
41
- "@knotx/eslint-config": "0.2.3",
42
- "@knotx/jsx": "0.2.3",
43
- "@knotx/typescript-config": "0.2.3"
40
+ "@knotx/build-config": "0.2.5",
41
+ "@knotx/eslint-config": "0.2.5",
42
+ "@knotx/jsx": "0.2.5",
43
+ "@knotx/typescript-config": "0.2.5"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "unbuild",