Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds AutoBind decorator #586

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-doors-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/draggable': patch
---

Adds AutoBind decorator
4 changes: 1 addition & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @type {import('@babel/core').TransformOptions}
*/
module.exports = function (api) {
api.cache(true);

Expand All @@ -12,6 +9,7 @@ module.exports = function (api) {
],
['@babel/preset-typescript'],
],
plugins: [['@babel/plugin-proposal-decorators', {version: '2023-05'}]],
assumptions: {
setPublicClassFields: true,
privateFieldsAsProperties: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
],
"devDependencies": {
"@babel/core": "^7.22.20",
"@babel/plugin-proposal-decorators": "^7.23.0",
"@babel/preset-env": "^7.22.20",
"@babel/preset-typescript": "^7.23.0",
"@changesets/changelog-github": "^0.4.8",
Expand Down
9 changes: 4 additions & 5 deletions src/Plugins/ResizeMirror/ResizeMirror.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AbstractPlugin from 'shared/AbstractPlugin';
import {requestNextAnimationFrame} from 'shared/utils';
import {requestNextAnimationFrame, AutoBind} from 'shared/utils';
import {FixMeAny} from 'shared/types';

import {MirrorCreatedEvent} from '../../Draggable/Plugins/Mirror/MirrorEvent';
Expand Down Expand Up @@ -53,10 +53,6 @@ export default class ResizeMirror extends AbstractPlugin {
* @property {HTMLElement} mirror
*/
this.mirror = null;

this.onMirrorCreated = this.onMirrorCreated.bind(this);
this.onMirrorDestroy = this.onMirrorDestroy.bind(this);
this.onDragOver = this.onDragOver.bind(this);
}

/**
Expand Down Expand Up @@ -93,6 +89,7 @@ export default class ResizeMirror extends AbstractPlugin {
* @param {MirrorCreatedEvent} mirrorEvent
* @private
*/
@AutoBind
private onMirrorCreated({mirror}: MirrorCreatedEvent) {
this.mirror = mirror;
}
Expand All @@ -102,6 +99,7 @@ export default class ResizeMirror extends AbstractPlugin {
* @param {MirrorDestroyEvent} mirrorEvent
* @private
*/
@AutoBind
private onMirrorDestroy() {
this.mirror = null;
}
Expand All @@ -111,6 +109,7 @@ export default class ResizeMirror extends AbstractPlugin {
* @param {DragOverEvent | DragOverContainer} dragEvent
* @private
*/
@AutoBind
private onDragOver(dragEvent: DragOverEvent | DragOverContainerEvent) {
this.resize(dragEvent);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/SwapAnimation/SwapAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AbstractPlugin from 'shared/AbstractPlugin';
import {FixMeAny} from 'shared/types';
import {AutoBind} from 'shared/utils';

interface Options {
duration: number;
Expand Down Expand Up @@ -56,8 +57,6 @@ export default class SwapAnimation extends AbstractPlugin {
* @type {Number}
*/
this.lastAnimationFrame = null;

this.onSortableSorted = this.onSortableSorted.bind(this);
}

/**
Expand Down Expand Up @@ -87,6 +86,7 @@ export default class SwapAnimation extends AbstractPlugin {
* @param {SortableSortedEvent} sortableEvent
* @private
*/
@AutoBind
onSortableSorted({oldIndex, newIndex, dragEvent}: FixMeAny) {
const {source, over} = dragEvent;

Expand Down
11 changes: 11 additions & 0 deletions src/shared/utils/decorators/AutoBind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function AutoBind<T extends (...args: any[]) => any>(
originalMethod: T,
{name, addInitializer}: ClassMethodDecoratorContext<ThisParameterType<T>, T>,
) {
addInitializer(function (this: ThisParameterType<T>) {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
this[name as PropertyKey] = originalMethod.bind(this);
/* eslint-enable @typescript-eslint/ban-ts-comment */
});
}
1 change: 1 addition & 0 deletions src/shared/utils/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {AutoBind} from './AutoBind';
1 change: 1 addition & 0 deletions src/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {default as closest} from './closest';
export {AutoBind} from './decorators';
export {default as requestNextAnimationFrame} from './requestNextAnimationFrame';
export {default as distance} from './distance';
export {default as touchCoords} from './touchCoords';
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"outDir": "build/ts",
"rootDir": "./",
"strictFunctionTypes": false,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"paths": {
"shared/*": ["./src/shared/*"],
"helper": ["./test/helper.ts"]
Expand Down
20 changes: 19 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-wrap-function" "^7.22.20"

"@babel/helper-replace-supers@^7.22.5", "@babel/helper-replace-supers@^7.22.9":
"@babel/helper-replace-supers@^7.22.20", "@babel/helper-replace-supers@^7.22.5", "@babel/helper-replace-supers@^7.22.9":
version "7.22.20"
resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.22.20.tgz#e37d367123ca98fe455a9887734ed2e16eb7a793"
integrity sha512-qsW0In3dbwQUbK8kejJ4R7IHVGwHJlV6lpG6UA7a9hSa2YEiAib+N1T2kr6PEeUT+Fl7najmSOS6SmAwCHK6Tw==
Expand Down Expand Up @@ -294,6 +294,17 @@
"@babel/helper-skip-transparent-expression-wrappers" "^7.22.5"
"@babel/plugin-transform-optional-chaining" "^7.22.15"

"@babel/plugin-proposal-decorators@^7.23.0":
version "7.23.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.23.0.tgz#66d9014173b3267a9ced3e69935138bc64ffb5c8"
integrity sha512-kYsT+f5ARWF6AdFmqoEEp+hpqxEB8vGmRWfw2aj78M2vTwS2uHW91EF58iFm1Z9U8Y/RrLu2XKJn46P9ca1b0w==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.22.15"
"@babel/helper-plugin-utils" "^7.22.5"
"@babel/helper-replace-supers" "^7.22.20"
"@babel/helper-split-export-declaration" "^7.22.6"
"@babel/plugin-syntax-decorators" "^7.22.10"

"@babel/plugin-proposal-private-property-in-object@7.21.0-placeholder-for-preset-env.2":
version "7.21.0-placeholder-for-preset-env.2"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz#7844f9289546efa9febac2de4cfe358a050bd703"
Expand Down Expand Up @@ -327,6 +338,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"

"@babel/plugin-syntax-decorators@^7.22.10":
version "7.22.10"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.22.10.tgz#7d83ea04d893c442b78ebf4c3cbac59a7211deff"
integrity sha512-z1KTVemBjnz+kSEilAsI4lbkPOl5TvJH7YDSY1CTIzvLWJ+KHXp+mRe8VPmfnyvqOPqar1V2gid2PleKzRUstQ==
dependencies:
"@babel/helper-plugin-utils" "^7.22.5"

"@babel/plugin-syntax-dynamic-import@^7.8.3":
version "7.8.3"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.8.3.tgz#62bf98b2da3cd21d626154fc96ee5b3cb68eacb3"
Expand Down