Skip to content

Commit

Permalink
♻️ 修复decorators导出的类型
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Oct 21, 2024
1 parent 61a296d commit fc9510e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 168 deletions.
2 changes: 1 addition & 1 deletion util/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gaubee/util",
"version": "0.14.1",
"version": "0.14.2",
"exports": {
"./bigint": "./src/bigint.ts",
"./collections": "./src/collections.ts",
Expand Down
161 changes: 81 additions & 80 deletions util/src/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,91 @@
import { accessor, Class, field, getter, setter,method } from "./decorators.ts";
import { accessor, Class, field, getter, method, setter } from "./decorators.ts";
import { curryThisFn, func_remember, uncurryThisFn } from "./func.ts";
@Class((A, c) => {
c.metadata;
c.addInitializer(function(){
console.log("initializer", "Class",)
})
return class B extends A {
constructor() {
super();
console.log("BBBBBBBB");
}
};
c.metadata;
c.addInitializer(function () {
console.log("initializer", "Class");
});
return class B extends A {
constructor() {
super();
console.log("BBBBBBBB");
}
};
})
class A {
constructor(){
console.log("constructor",this.constructor.name)
constructor() {
console.log("constructor", this.constructor.name);
}
@accessor(() => {
return {};
})
accessor acc = 1;
@field(() => {
return function (v) {
console.log("field~zzz", v);
return v + 1;
};
})
fie = 2;
@accessor(() => {
return {};
})
accessor acc = 1;
@field(() => {
return function (v) {
console.log("field~zzz", v);
return v + 1;
};
})
fie = 2;

// @getter((_t, c, r) => {
// return curryThisFn(func_remember(c.access.get));
// })
@getter((get) => curryThisFn(func_remember(uncurryThisFn(get))))
get qaq() {
return crypto.randomUUID();
}
@getter((get) => func_remember(get))
get zzz() {
return crypto.randomUUID();
}
@getter.before<A, number>(console.log.bind(console, "getter.before"))
@getter.after(console.log.bind(console, "getter.after"))
get uuu() {
return 1;
}
@setter.before(console.log.bind(console, "setter.before"))
@setter.after(console.log.bind(console, "setter.after"))
@(function(z,c){})
set uuu(v) {}

@(function (t, c) {
c.addInitializer(function () {
console.log("initializer mmm",this.fie);
});
return function (this: A) {
// t()
// console.log("access.get", c.access.get(this));
return 2;
};
})
get #mmm() {
console.log("get mmm");
return 1;
}
set #mmm(v) {
console.log("set mmm", v);
}
get mmm() {
return this.#mmm;
}
ccccA(a:number){
// @getter((_t, c, r) => {
// return curryThisFn(func_remember(c.access.get));
// })
@getter((get) => curryThisFn(func_remember(uncurryThisFn(get))))
get qaq() {
return crypto.randomUUID();
}
@getter((get) => func_remember(get))
get zzz() {
return crypto.randomUUID();
}
@getter.before<A, number>(console.log.bind(console, "getter.before"))
@getter.after(console.log.bind(console, "getter.after"))
get uuu() {
return 1;
}
@setter.before(console.log.bind(console, "setter.before"))
@setter.after(console.log.bind(console, "setter.after"))
@setter(function (_z, _c) {})
set uuu(v) {}

}
@getter(function (_t, c) {
c.addInitializer(function () {
console.log("initializer mmm", this.fie);
});
return function (this: A) {
// t()
// console.log("access.get", c.access.get(this));
return 2;
};
})
get #mmm() {
console.log("get mmm");
return 1;
}
set #mmm(v) {
console.log("set mmm", v);
}
get mmm() {
return this.#mmm;
}
@method(() => {
})
ccccA(_a: number) {
}
}
if (import.meta.main) {
console.log(A.name);
const a = new A();
// console.log("fie~xxx", Object.getOwnPropertyDescriptor(A.prototype, "fie"));
// const b = new A();
// console.log(a.qaq === a.qaq);
// console.log(a.zzz === a.zzz);
// a.uuu = 123;
// a.uuu = 456;
// a.uuu;
// console.log("fie", a.fie, Object.getOwnPropertyDescriptor(a, "fie"));
// a.fie = 100;
// console.log("fie", a.fie, Object.getOwnPropertyDescriptor(a, "fie"));
console.log(a.mmm);
console.log(A.name);
const a = new A();
// console.log("fie~xxx", Object.getOwnPropertyDescriptor(A.prototype, "fie"));
// const b = new A();
// console.log(a.qaq === a.qaq);
// console.log(a.zzz === a.zzz);
// a.uuu = 123;
// a.uuu = 456;
// a.uuu;
// console.log("fie", a.fie, Object.getOwnPropertyDescriptor(a, "fie"));
// a.fie = 100;
// console.log("fie", a.fie, Object.getOwnPropertyDescriptor(a, "fie"));
console.log(a.mmm);
}
172 changes: 85 additions & 87 deletions util/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,46 @@ export type ClassGetterDecorator<T extends object, V> = (
/**
* 用于对 getter 的修饰
*/
export const getter = Object.assign(<T extends object, V>(
export const getter = <T extends object, V>(
builder: ClassGetterDecorator<T, V>,
): ClassGetterDecorator<T, V> => {
return builder;
}, {
/**
* 用于在 getter 之前做一些动作
*/
before: <T extends object, V>(before: Func<T, [], void>): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function () {
before.call(this);
return get.call(this);
};
});
},
/**
* 用于在 getter 之后做一些动作
*/
after: <T extends object, V>(after: Func<T, [V], void>): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function (this: T) {
const result = get.call(this);
after.call(this, result);
return result;
};
});
},
/**
* 用于对 getter 做一个拦截
*/
wrap: <T extends object, V>(
wrap: (this: T, get: () => V) => V,
): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function (this: T) {
return wrap.call(this, () => get.call(this));
};
});
},
});
};
/**
* 用于在 getter 之前做一些动作
*/
export const beforeGetter = <T extends object, V>(before: Func<T, [], void>): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function () {
before.call(this);
return get.call(this);
};
});
};
/**
* 用于在 getter 之后做一些动作
*/
export const afterGetter = <T extends object, V>(after: Func<T, [V], void>): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function (this: T) {
const result = get.call(this);
after.call(this, result);
return result;
};
});
};
/**
* 用于对 getter 做一个拦截
*/
export const wrapGetter = <T extends object, V>(
wrap: (this: T, get: () => V) => V,
): ClassGetterDecorator<T, V> => {
return getter<T, V>((get) => {
return function (this: T) {
return wrap.call(this, () => get.call(this));
};
});
};

export type ClassSetterDecorator<T, V> = (
target: (this: T, value: V) => void,
Expand All @@ -69,46 +68,45 @@ export type ClassSetterDecorator<T, V> = (
/**
* 用于对 setter 的修饰
*/
export const setter = Object.assign(<T = unknown, V = unknown>(
export const setter = <T = unknown, V = unknown>(
builder: ClassSetterDecorator<T, V>,
): ClassSetterDecorator<T, V> => {
return builder;
}, {
/**
* 用于在 setter 之前做一些动作
*/
before: <T extends object, V>(before: Func<T, [V], void>): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
before.call(this, v);
set.call(this, v);
}
);
},
/**
* 用于在 setter 之后做一些动作
*/
after: <T extends object, V>(after: Func<T, [V], void>): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
set.call(this, v);
after.call(this, v);
}
);
},
/**
* 用于对 setter 做一个拦截
*/
wrap: <T extends object, V>(
wrap: (this: T, value: V, set: Func<T, [V], void>) => void,
): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
wrap.call(this, v, set);
}
);
},
});
};
/**
* 用于在 setter 之前做一些动作
*/
export const beforeSetter = <T extends object, V>(before: Func<T, [V], void>): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
before.call(this, v);
set.call(this, v);
}
);
};
/**
* 用于在 setter 之后做一些动作
*/
export const afterSetter = <T extends object, V>(after: Func<T, [V], void>): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
set.call(this, v);
after.call(this, v);
}
);
};
/**
* 用于对 setter 做一个拦截
*/
export const wrapSetter = <T extends object, V>(
wrap: (this: T, value: V, set: Func<T, [V], void>) => void,
): ClassSetterDecorator<T, V> => {
return setter<T, V>((set) =>
function (v) {
wrap.call(this, v, set);
}
);
};

export type ClassFieldDecorator<T extends object, V> = (
target: undefined,
Expand All @@ -132,21 +130,21 @@ export type ClassMethodDecorator<T, M extends Method<T>> = (
* @param builder
* @returns
*/
export const method = Object.assign(<T extends object, M extends Method<T>>(
export const method = <T extends object, M extends Method<T>>(
builder: ClassMethodDecorator<T, M>,
): ClassMethodDecorator<T, M> => {
return builder;
}, {
bindThis: <T extends object, M extends Method<T>>(): ClassMethodDecorator<T, M> =>
method<T, M>((target, context) => {
if (context.private) {
throw new Error("private method no suport to bind this.");
}
context.addInitializer(function () {
Reflect.set(this, context.name, target.bind(this));
});
}),
});
};

export const bindThis = <T extends object, M extends Method<T>>(): ClassMethodDecorator<T, M> =>
method<T, M>((target, context) => {
if (context.private) {
throw new Error("private method no suport to bind this.");
}
context.addInitializer(function () {
Reflect.set(this, context.name, target.bind(this));
});
});

type Class = abstract new (...args: any) => any;
export type ClassDecorator<C extends Class> = (
Expand Down

0 comments on commit fc9510e

Please sign in to comment.