lombok-typescript 0.5.0 → 0.7.0

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.
@@ -104,6 +104,9 @@ var MetadataKeys = {
104
104
  SINGLETON: `${PREFIX}singleton`,
105
105
  FACTORY: `${PREFIX}factory`,
106
106
  PROTOTYPE: `${PREFIX}prototype`,
107
+ COMPOSITE: `${PREFIX}composite`,
108
+ FLYWEIGHT: `${PREFIX}flyweight`,
109
+ PROXY: `${PREFIX}proxy`,
107
110
  CHAIN_OF_RESPONSIBILITY: `${PREFIX}chainOfResponsibility`,
108
111
  COMMAND: `${PREFIX}command`,
109
112
  ITERABLE: `${PREFIX}iterable`,
@@ -876,6 +879,128 @@ function iterateOverFieldStage3(backend, context) {
876
879
  registerStage3IterateField(context.metadata, context.name);
877
880
  }
878
881
 
882
+ // src/decorators/shared/flyweight.ts
883
+ var pools = /* @__PURE__ */ new WeakMap();
884
+ function getPool(ctor) {
885
+ let pool = pools.get(ctor);
886
+ if (!pool) {
887
+ pool = /* @__PURE__ */ new Map();
888
+ pools.set(ctor, pool);
889
+ }
890
+ return pool;
891
+ }
892
+ function wrapFlyweightClass(target, options) {
893
+ const pool = getPool(target);
894
+ const FlyweightClass = class extends target {
895
+ constructor(...args) {
896
+ const key = options.key(...args);
897
+ const existing = pool.get(key);
898
+ if (existing) {
899
+ return existing;
900
+ }
901
+ super(...args);
902
+ pool.set(key, this);
903
+ }
904
+ };
905
+ return FlyweightClass;
906
+ }
907
+ function flyweightClassStage3(backend, value, context, options) {
908
+ if (typeof options?.key !== "function") {
909
+ throw new Error("@Flyweight requires a `key` function in options");
910
+ }
911
+ backend.metadata.set(MetadataKeys.FLYWEIGHT, context.metadata, void 0, options);
912
+ return wrapFlyweightClass(value, options);
913
+ }
914
+
915
+ // src/decorators/shared/composite.ts
916
+ var CHILDREN = /* @__PURE__ */ Symbol("lombok-ts:composite-children");
917
+ function getChildren(instance) {
918
+ const bag = instance;
919
+ if (!bag[CHILDREN]) {
920
+ bag[CHILDREN] = [];
921
+ }
922
+ return bag[CHILDREN];
923
+ }
924
+ function wrapCompositeClass(target) {
925
+ const CompositeClass = class extends target {
926
+ add(child) {
927
+ getChildren(this).push(child);
928
+ }
929
+ remove(child) {
930
+ const children = getChildren(this);
931
+ const index = children.indexOf(child);
932
+ if (index >= 0) {
933
+ children.splice(index, 1);
934
+ }
935
+ }
936
+ getChild(index) {
937
+ const children = getChildren(this);
938
+ const child = children[index];
939
+ if (child === void 0) {
940
+ throw new RangeError(`@Composite: no child at index ${index}`);
941
+ }
942
+ return child;
943
+ }
944
+ getChildren() {
945
+ return [...getChildren(this)];
946
+ }
947
+ traverse(callback) {
948
+ const visit = (node) => {
949
+ callback(node);
950
+ for (const child of getChildren(node)) {
951
+ visit(child);
952
+ }
953
+ };
954
+ visit(this);
955
+ }
956
+ *[Symbol.iterator]() {
957
+ yield* getChildren(this);
958
+ }
959
+ };
960
+ return CompositeClass;
961
+ }
962
+ function compositeClassStage3(backend, value, context) {
963
+ backend.metadata.set(MetadataKeys.COMPOSITE, context.metadata, void 0, true);
964
+ return wrapCompositeClass(value);
965
+ }
966
+
967
+ // src/decorators/shared/proxy.ts
968
+ function isMethod(key, value) {
969
+ return typeof key === "string" && key !== "constructor" && typeof value === "function";
970
+ }
971
+ function wrapProxyInstance(instance, hooks) {
972
+ const proto = Object.getPrototypeOf(instance);
973
+ return new Proxy(instance, {
974
+ get(target, prop, receiver) {
975
+ const value = Reflect.get(target, prop, receiver);
976
+ if (!isMethod(prop, value)) {
977
+ return value;
978
+ }
979
+ const original = Object.getOwnPropertyDescriptor(proto, prop)?.value ?? value;
980
+ return (...args) => {
981
+ const methodName = String(prop);
982
+ hooks.before?.(methodName, args);
983
+ const result = original.apply(target, args);
984
+ hooks.after?.(methodName, result);
985
+ return result;
986
+ };
987
+ }
988
+ });
989
+ }
990
+ function wrapProxyClass(target, hooks) {
991
+ const ProxyClass = class extends target {
992
+ constructor(...args) {
993
+ super(...args);
994
+ return wrapProxyInstance(this, hooks);
995
+ }
996
+ };
997
+ return ProxyClass;
998
+ }
999
+ function proxyClassStage3(backend, value, context, hooks = {}) {
1000
+ backend.metadata.set(MetadataKeys.PROXY, context.metadata, void 0, hooks);
1001
+ return wrapProxyClass(value, hooks);
1002
+ }
1003
+
879
1004
  // src/decorators/stage3/index.ts
880
1005
  var NonNull = defineFieldDecorator(nonNullFieldStage3);
881
1006
  var Singleton = defineClassDecorator(singletonClassStage3);
@@ -987,6 +1112,17 @@ function Handler(options) {
987
1112
  }
988
1113
  var Iterable = defineClassDecorator(iterableClassStage3);
989
1114
  var IterateOver = defineFieldDecorator(iterateOverFieldStage3);
1115
+ function Flyweight(options) {
1116
+ return defineClassDecorator(
1117
+ (backend, value, context) => flyweightClassStage3(backend, value, context, options)
1118
+ );
1119
+ }
1120
+ var Composite = defineClassDecorator(compositeClassStage3);
1121
+ function Proxy2(hooks = {}) {
1122
+ return defineClassDecorator(
1123
+ (backend, value, context) => proxyClassStage3(backend, value, context, hooks)
1124
+ );
1125
+ }
990
1126
 
991
1127
  // src/stage3/index.ts
992
1128
  function getClassMetadata(cls) {
@@ -998,12 +1134,14 @@ exports.Builder = Builder;
998
1134
  exports.ChainOfResponsibility = ChainOfResponsibility;
999
1135
  exports.Command = Command;
1000
1136
  exports.CommandHistory = CommandHistory;
1137
+ exports.Composite = Composite;
1001
1138
  exports.Data = Data;
1002
1139
  exports.Delegate = Delegate;
1003
1140
  exports.Equals = Equals;
1004
1141
  exports.EqualsExclude = EqualsExclude;
1005
1142
  exports.Factory = Factory;
1006
1143
  exports.FieldDefaults = FieldDefaults;
1144
+ exports.Flyweight = Flyweight;
1007
1145
  exports.Getter = Getter;
1008
1146
  exports.Handler = Handler;
1009
1147
  exports.Iterable = Iterable;
@@ -1015,6 +1153,7 @@ exports.NonNull = NonNull;
1015
1153
  exports.Observable = Observable;
1016
1154
  exports.Observer = Observer;
1017
1155
  exports.Prototype = Prototype;
1156
+ exports.Proxy = Proxy2;
1018
1157
  exports.Setter = Setter;
1019
1158
  exports.Singleton = Singleton;
1020
1159
  exports.Stage3Backend = Stage3Backend;