Visualize Your Ideas With Code
refactor: simplify patching
aarthificiald709091fc462
diff --git a/src/decorators/getset.ts b/src/decorators/getset.tsindex b4797637..599fd2f1 100644--- a/src/decorators/getset.ts+++ b/src/decorators/getset.ts@@ -7,12 +7,9 @@ export function getset(tween?: TweenProvider<any>,): PropertyDecorator {return function (target, propertyKey) {- Factory.addGetterSetter(- target.constructor,- propertyKey,- defaultValue,- tween,- after,- );+ Factory.addGetter(target.constructor, propertyKey, defaultValue);+ Factory.addSetter(target.constructor, propertyKey, undefined, after);+ // @ts-ignore+ Factory.addOverloadedGetterSetter(target.constructor, propertyKey, tween);};}diff --git a/src/patches/Factory.ts b/src/patches/Factory.tsindex 4db66222..37a02aee 100644--- a/src/patches/Factory.ts+++ b/src/patches/Factory.ts@@ -1,8 +1,9 @@+import type {Node} from 'konva/lib/Node';import {Factory} from 'konva/lib/Factory';import {ANIMATE} from '../symbols';-import type {Node} from 'konva/lib/Node';import {Animator, TweenProvider} from '../tweening';import {ThreadGenerator} from '../threading';+import {Vector2d} from 'konva/lib/types';declare module 'konva/lib/types' {export interface GetSet<Type, This extends Node> {@@ -13,40 +14,25 @@ declare module 'konva/lib/types' {}}-Factory.overWriteSetter = function overWriteSetter(- constructor: Function,- attr: string,- validator?: TweenProvider<any>,- after?: () => void,-) {- const name = attr.charAt(0).toUpperCase() + attr.slice(1);- const setter = `set${name}`;- constructor.prototype[setter] = function (val: any, time?: any) {- if (val === ANIMATE) {- return new Animator(this, attr, validator);- }-- if (time !== undefined) {- return new Animator(this, attr, validator).key(val, time).run();- }-- const old = this.attrs[attr];- this._setAttr(attr, val);- if (old !== val) after?.call(this);-- return this;- };-};-Factory.addOverloadedGetterSetter = function addOverloadedGetterSetter(constructor: Function,attr: string,+ tween?: TweenProvider<any>,) {const capitalizedAttr = attr.charAt(0).toUpperCase() + attr.slice(1);const setter = 'set' + capitalizedAttr;const getter = 'get' + capitalizedAttr;- constructor.prototype[attr] = function (...args: any[]) {- return args.length ? this[setter](...args) : this[getter]();+ constructor.prototype[attr] = function (+ value?: Vector2d | typeof ANIMATE,+ time?: number,+ ) {+ if (value === ANIMATE) {+ return new Animator<any, Node>(this, attr, tween);+ }+ if (time !== undefined) {+ return new Animator<any, Node>(this, attr, tween).key(value, time).run();+ }+ return value === undefined ? this[getter]() : this[setter](value);};};diff --git a/src/patches/Node.ts b/src/patches/Node.tsdeleted file mode 100644index 9ab17bdc..00000000--- a/src/patches/Node.ts+++ /dev/null@@ -1,28 +0,0 @@-import {Node} from 'konva/lib/Node';-import {Vector2d} from 'konva/lib/types';-import {Animator} from '../tweening/Animator';-import {ANIMATE} from '../symbols';-import {ThreadGenerator} from '../threading';--const oldPosition = Node.prototype.position;-function patchedPosition(): Vector2d;-function patchedPosition(value: Vector2d): Node;-function patchedPosition(value: typeof ANIMATE): Animator<Vector2d, Node>;-function patchedPosition(value: Vector2d, time: number): ThreadGenerator;-function patchedPosition(- value?: Vector2d | typeof ANIMATE,- time?: number,-): Node | Vector2d | Animator<Vector2d, Node> | ThreadGenerator {- if (value === ANIMATE) {- return new Animator<Vector2d, Node>(this, 'position');- }- if (time !== undefined) {- return new Animator<Vector2d, Node>(this, 'position')- .key(value, time)- .run();- }- return value === undefined- ? oldPosition.call(this)- : oldPosition.call(this, value);-}-Node.prototype.position = patchedPosition;diff --git a/src/patches/index.ts b/src/patches/index.tsindex 83e15cb3..7e2fe710 100644--- a/src/patches/index.ts+++ b/src/patches/index.ts@@ -1,2 +1 @@import './Factory';-import './Node';\ No newline at end of file