@knotx/decorators 0.0.1

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.mjs ADDED
@@ -0,0 +1,586 @@
1
+ import { getSymbol, Runtime, Engine, Layer, use } from '@knotx/core';
2
+ import { BehaviorSubject, combineLatest } from 'rxjs';
3
+ import { get } from 'lodash-es';
4
+ import { jsx } from '@knotx/jsx/jsx-runtime';
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);
12
+ }
13
+
14
+ function edgeOperator() {
15
+ return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
16
+ if (isStatic || isPrivate) {
17
+ return;
18
+ }
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)) {
31
+ engine.dispatchEdgeOperation({
32
+ type: "batch",
33
+ operations
34
+ });
35
+ }
36
+ return operations;
37
+ });
38
+ });
39
+ });
40
+ };
41
+ }
42
+
43
+ const edgePipeSymbol = Symbol("edgePipe");
44
+ function edgePipe() {
45
+ return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
46
+ if (isStatic || isPrivate) {
47
+ return;
48
+ }
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;
63
+ });
64
+ });
65
+ };
66
+ }
67
+
68
+ function edgeType(type) {
69
+ return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
70
+ if (isStatic || isPrivate) {
71
+ return;
72
+ }
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
+ });
80
+ };
81
+ }
82
+
83
+ function injectData(context, engineKey, selector) {
84
+ context.addInitializer(function() {
85
+ const engine$ = requireEngine(this);
86
+ Reflect.defineProperty(this, context.name, {
87
+ get() {
88
+ const engine = engine$.value;
89
+ if (!engine) {
90
+ return void 0;
91
+ }
92
+ const engineKey$ = `${String(engineKey)}$`;
93
+ const paths = get(engine, engineKey$) ? [engineKey$] : [engineKey];
94
+ return Runtime.getInstance().getValue(engine, { paths, selector });
95
+ }
96
+ });
97
+ });
98
+ }
99
+
100
+ function injectMethod(context, methodKey) {
101
+ context.addInitializer(function() {
102
+ Reflect.set(this, context.name, (...methodArgs) => {
103
+ const engine = requireEngine(this).value;
104
+ const engineMethod = engine == null ? void 0 : engine[methodKey];
105
+ if (!engineMethod || typeof engineMethod !== "function") {
106
+ throw new Error(`Engine method ${String(methodKey)} not found`);
107
+ }
108
+ return Reflect.apply(engineMethod, engine, methodArgs);
109
+ });
110
+ });
111
+ }
112
+
113
+ function injectPluginData(context, pluginName, property, selector) {
114
+ context.addInitializer(function() {
115
+ const engine$ = requireEngine(this);
116
+ Reflect.defineProperty(this, context.name, {
117
+ get() {
118
+ const engine = engine$.value;
119
+ if (!engine) {
120
+ return void 0;
121
+ }
122
+ const paths = ["_pluginDataContainer", pluginName, property];
123
+ return Runtime.getInstance().getValue(engine, { paths, selector });
124
+ }
125
+ });
126
+ });
127
+ }
128
+
129
+ function createInjectableProxy(engine, accessRecord) {
130
+ return new Proxy({}, {
131
+ get(_, prop) {
132
+ if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") {
133
+ return () => "[object Injectable]";
134
+ }
135
+ if (typeof prop !== "string") {
136
+ return void 0;
137
+ }
138
+ if (prop in engine) {
139
+ const engineProp = engine[prop];
140
+ if (typeof engineProp === "function") {
141
+ throw new TypeError(`Engine method ${String(prop)} cannot be accessed via injectable`);
142
+ }
143
+ if (isBehaviorSubject(engineProp)) {
144
+ accessRecord.subjects.push(engineProp);
145
+ accessRecord.paths.push([prop]);
146
+ return engineProp;
147
+ }
148
+ if (engineProp && typeof engineProp === "object") {
149
+ return createEnginePropertyProxy(engine, prop, engineProp, accessRecord);
150
+ }
151
+ return engineProp;
152
+ }
153
+ return createPluginPropertyProxy(engine, prop, accessRecord);
154
+ }
155
+ });
156
+ }
157
+ function createEnginePropertyProxy(engine, propName, propValue, accessRecord) {
158
+ return new Proxy({}, {
159
+ get(_, subProp) {
160
+ if (subProp === Symbol.toPrimitive || subProp === "toString" || subProp === "valueOf") {
161
+ return () => `[object Injectable.${String(propName)}]`;
162
+ }
163
+ if (typeof subProp !== "string") {
164
+ return void 0;
165
+ }
166
+ const nestedProp = propValue[subProp];
167
+ if (isBehaviorSubject(nestedProp)) {
168
+ accessRecord.subjects.push(nestedProp);
169
+ accessRecord.paths.push([propName, subProp]);
170
+ return nestedProp;
171
+ }
172
+ if (nestedProp && typeof nestedProp === "object") {
173
+ return createNestedPropertyProxy(engine, [propName, subProp], nestedProp, accessRecord);
174
+ }
175
+ return nestedProp;
176
+ }
177
+ });
178
+ }
179
+ function createNestedPropertyProxy(engine, path, propValue, accessRecord) {
180
+ return new Proxy({}, {
181
+ get(_, subProp) {
182
+ if (subProp === Symbol.toPrimitive || subProp === "toString" || subProp === "valueOf") {
183
+ return () => `[object Injectable.${path.join(".")}]`;
184
+ }
185
+ if (typeof subProp !== "string") {
186
+ return void 0;
187
+ }
188
+ const nestedProp = propValue[subProp];
189
+ if (isBehaviorSubject(nestedProp)) {
190
+ accessRecord.subjects.push(nestedProp);
191
+ accessRecord.paths.push([...path, subProp]);
192
+ return nestedProp;
193
+ }
194
+ if (nestedProp && typeof nestedProp === "object") {
195
+ return createNestedPropertyProxy(engine, [...path, subProp], nestedProp, accessRecord);
196
+ }
197
+ return nestedProp;
198
+ }
199
+ });
200
+ }
201
+ function createPluginPropertyProxy(engine, pluginName, accessRecord) {
202
+ return new Proxy({}, {
203
+ get(_, prop) {
204
+ if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") {
205
+ return () => `[object Injectable.${String(pluginName)}]`;
206
+ }
207
+ if (typeof prop !== "string") {
208
+ return void 0;
209
+ }
210
+ try {
211
+ const paths = ["_pluginDataContainer", pluginName, prop];
212
+ const pluginData = get(engine, paths);
213
+ if (isBehaviorSubject(pluginData)) {
214
+ accessRecord.subjects.push(pluginData);
215
+ accessRecord.paths.push(paths);
216
+ return pluginData.value;
217
+ }
218
+ return pluginData;
219
+ } catch (e) {
220
+ return void 0;
221
+ }
222
+ }
223
+ });
224
+ }
225
+ function isBehaviorSubject(value) {
226
+ return value instanceof BehaviorSubject;
227
+ }
228
+
229
+ function injectable(context, injectableSelector) {
230
+ const injectableStateSymbol = Symbol(`injectable:${String(context.name)}`);
231
+ context.addInitializer(function() {
232
+ const engine$ = requireEngine(this);
233
+ Reflect.set(this, injectableStateSymbol, {
234
+ initialized: false,
235
+ accessRecord: null,
236
+ wrappedSubject$: null,
237
+ subscription: null,
238
+ selector: null,
239
+ matcher: null,
240
+ paths: []
241
+ });
242
+ Reflect.defineProperty(this, context.name, {
243
+ get() {
244
+ const engine = engine$.value;
245
+ if (!engine) {
246
+ return void 0;
247
+ }
248
+ const state = this[injectableStateSymbol];
249
+ if (!state.initialized) {
250
+ state.initialized = true;
251
+ const accessRecord = {
252
+ subjects: [],
253
+ paths: []
254
+ };
255
+ const injectable2 = createInjectableProxy(engine, accessRecord);
256
+ injectableSelector(injectable2, {});
257
+ if (accessRecord.subjects.length > 0) {
258
+ const combinedObservable$ = combineLatest(accessRecord.subjects);
259
+ const wrappedSubject$ = new BehaviorSubject(
260
+ accessRecord.subjects.map((subject) => subject.value)
261
+ );
262
+ const subscription = combinedObservable$.subscribe(wrappedSubject$);
263
+ state.accessRecord = accessRecord;
264
+ state.wrappedSubject$ = wrappedSubject$;
265
+ state.subscription = subscription;
266
+ state.matcher = () => state.wrappedSubject$;
267
+ }
268
+ state.selector = (_, context2) => {
269
+ const injectable3 = createInjectableProxy(engine, { subjects: [], paths: [] });
270
+ return injectableSelector(injectable3, context2 || {});
271
+ };
272
+ }
273
+ return Runtime.getInstance().getValue(engine, state);
274
+ }
275
+ });
276
+ });
277
+ }
278
+
279
+ function injectDecorator(arg0, arg1, arg2) {
280
+ return function(_, context) {
281
+ var _a;
282
+ if (typeof arg0 === "function") {
283
+ return injectable(context, arg0);
284
+ }
285
+ if (typeof arg1 === "string") {
286
+ return injectPluginData(context, arg0, arg1, arg2);
287
+ }
288
+ const engineKey = arg0;
289
+ const isMethod = typeof ((_a = Reflect.getOwnPropertyDescriptor(Engine.prototype, engineKey)) == null ? void 0 : _a.value) === "function";
290
+ if (isMethod) {
291
+ return injectMethod(context, engineKey);
292
+ }
293
+ return injectData(context, engineKey, arg1);
294
+ };
295
+ }
296
+
297
+ function createInjectProxy(...presets) {
298
+ return new Proxy(
299
+ new Object(() => {
300
+ }),
301
+ {
302
+ get(_, prop) {
303
+ return createInjectProxy(...presets, prop);
304
+ },
305
+ apply(_, thisArg, args) {
306
+ return Reflect.apply(injectDecorator, thisArg, [...presets, ...args]);
307
+ }
308
+ }
309
+ );
310
+ }
311
+ const inject = createInjectProxy();
312
+
313
+ function register(property) {
314
+ return function(_, context) {
315
+ if (context.kind !== "field" && context.kind !== "getter") {
316
+ return;
317
+ }
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
+ });
336
+ });
337
+ };
338
+ }
339
+
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
+ var __defProp = Object.defineProperty;
354
+ var __defProps = Object.defineProperties;
355
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
356
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
357
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
358
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
359
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
360
+ var __spreadValues = (a, b) => {
361
+ for (var prop in b || (b = {}))
362
+ if (__hasOwnProp.call(b, prop))
363
+ __defNormalProp(a, prop, b[prop]);
364
+ if (__getOwnPropSymbols)
365
+ for (var prop of __getOwnPropSymbols(b)) {
366
+ if (__propIsEnum.call(b, prop))
367
+ __defNormalProp(a, prop, b[prop]);
368
+ }
369
+ return a;
370
+ };
371
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
372
+ function getPositionStyle(position, container, offset = {}) {
373
+ const { x = 0, y = 0 } = offset;
374
+ const { width, height } = container;
375
+ const baseStyle = {
376
+ position: "absolute",
377
+ top: 0,
378
+ left: 0
379
+ };
380
+ switch (position) {
381
+ case "top":
382
+ return __spreadProps(__spreadValues({}, baseStyle), {
383
+ transform: `translate(calc(${width / 2}px - 50%), ${y}px)`
384
+ });
385
+ case "bottom":
386
+ return __spreadProps(__spreadValues({}, baseStyle), {
387
+ transform: `translate(calc(${width / 2}px - 50%), calc(${height}px - 100% + ${y}px))`
388
+ });
389
+ case "left":
390
+ return __spreadProps(__spreadValues({}, baseStyle), {
391
+ transform: `translate(${x}px, calc(${height / 2}px - 50%))`
392
+ });
393
+ case "right":
394
+ return __spreadProps(__spreadValues({}, baseStyle), {
395
+ transform: `translate(calc(${width}px - 100% + ${x}px), calc(${height / 2}px - 50%))`
396
+ });
397
+ case "center":
398
+ return __spreadProps(__spreadValues({}, baseStyle), {
399
+ transform: `translate(calc(${width / 2}px - 50% + ${x}px), calc(${height / 2}px - 50% + ${y}px))`
400
+ });
401
+ case "left-top":
402
+ return __spreadProps(__spreadValues({}, baseStyle), {
403
+ transform: `translate(${x}px, ${y}px)`
404
+ });
405
+ case "right-top":
406
+ return __spreadProps(__spreadValues({}, baseStyle), {
407
+ transform: `translate(calc(${width}px - 100% + ${x}px), ${y}px)`
408
+ });
409
+ case "left-bottom":
410
+ return __spreadProps(__spreadValues({}, baseStyle), {
411
+ transform: `translate(${x}px, calc(${height}px - 100% + ${y}px))`
412
+ });
413
+ case "right-bottom":
414
+ return __spreadProps(__spreadValues({}, baseStyle), {
415
+ transform: `translate(calc(${width}px - 100% + ${x}px), calc(${height}px - 100% + ${y}px))`
416
+ });
417
+ default:
418
+ return baseStyle;
419
+ }
420
+ }
421
+ function createPanelWrapper(position, offset = {}) {
422
+ return function PanelWrapper({ children, container }) {
423
+ const style = getPositionStyle(position, container, offset);
424
+ return /* @__PURE__ */ jsx("div", { style, children });
425
+ };
426
+ }
427
+ function ContainerListener({
428
+ getContainer,
429
+ children,
430
+ position,
431
+ offset
432
+ }) {
433
+ const container = use(getContainer);
434
+ const Wrapper = createPanelWrapper(position, offset);
435
+ return container.width > 0 && container.height > 0 ? /* @__PURE__ */ jsx(Wrapper, { container, children }) : null;
436
+ }
437
+ 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
+ });
463
+ }
464
+ };
465
+ }
466
+
467
+ function nodeOperator() {
468
+ return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
469
+ if (isStatic || isPrivate) {
470
+ return;
471
+ }
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)) {
484
+ engine.dispatchNodeOperation({
485
+ type: "batch",
486
+ operations
487
+ });
488
+ }
489
+ return operations;
490
+ });
491
+ });
492
+ });
493
+ };
494
+ }
495
+
496
+ const nodePipeSymbol = Symbol("nodePipe");
497
+ function nodePipe() {
498
+ return function(_target, { name, static: isStatic, private: isPrivate, addInitializer }) {
499
+ if (isStatic || isPrivate) {
500
+ return;
501
+ }
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;
516
+ });
517
+ });
518
+ };
519
+ }
520
+
521
+ 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 = () => {
543
+ originalMethod.call(this);
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
+
559
+ function observable() {
560
+ return function(_, context) {
561
+ if (context.kind !== "field") {
562
+ return;
563
+ }
564
+ context.addInitializer(function() {
565
+ const subject = new BehaviorSubject(Reflect.get(this, context.name));
566
+ const engine$ = requireEngine(this);
567
+ Reflect.defineProperty(this, context.name, {
568
+ get() {
569
+ const engine = engine$.value;
570
+ if (!engine) {
571
+ return subject.value;
572
+ }
573
+ return Runtime.getInstance().getValue(engine, {
574
+ paths: [],
575
+ matcher: () => subject
576
+ });
577
+ },
578
+ set(value) {
579
+ subject.next(value);
580
+ }
581
+ });
582
+ });
583
+ };
584
+ }
585
+
586
+ export { OnDestroy, OnInit, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@knotx/decorators",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "Decorators for Knotx",
6
+ "author": "boenfu",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/boenfu/knotx#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/boenfu/knotx.git",
12
+ "directory": "packages/decorators"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "dependencies": {
32
+ "lodash-es": "^4.17.21",
33
+ "rxjs": "^7.8.1",
34
+ "@knotx/core": "0.0.1"
35
+ },
36
+ "devDependencies": {
37
+ "@types/lodash-es": "^4.14.12",
38
+ "@knotx/build-config": "0.0.1",
39
+ "@knotx/jsx": "0.0.1",
40
+ "@knotx/eslint-config": "0.0.1",
41
+ "@knotx/typescript-config": "0.0.1"
42
+ },
43
+ "scripts": {
44
+ "build": "unbuild --failOnWarn=false",
45
+ "dev": "unbuild --stub",
46
+ "lint": "eslint .",
47
+ "typecheck": "tsc --noEmit"
48
+ }
49
+ }