Skip to content

Commit 8fc35db

Browse files
authored
refactor(pointeract): remove JSR integration and refactor module initialization (#47)
- Remove JSR badges from README and get-started documentation - Delete jsr.json file - Refactor module initialization process in Pointeract class - Update BaseModule constructor to receive individual functions instead of moduleUtils object - Convert class methods to arrow functions for better flexibility
1 parent 81418d1 commit 8fc35db

7 files changed

Lines changed: 66 additions & 104 deletions

File tree

.github/workflows/publish.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,3 @@ jobs:
3636

3737
- name: Publish to npm
3838
run: pnpm publish --no-git-checks
39-
40-
- name: Publish to JSR
41-
run: pnpx jsr publish

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
<a href="https://www.npmjs.com/package/pointeract">
2424
<img src="https://img.shields.io/npm/v/pointeract?logo=npm&labelColor=red&logoColor=white&color=333333" alt="npm package" />
2525
</a>
26-
<a href="https://jsr.io/@hesprs/pointeract">
27-
<img src="https://img.shields.io/jsr/v/@hesprs/pointeract?logo=jsr&labelColor=f7df1e&logoColor=white&color=333333" alt="JSR package" />
28-
</a>
2926
<a href="https://snyk.io/test/npm/pointeract">
3027
<img src="https://img.shields.io/badge/Snyk%20Security-Monitored-333333?logo=snyk&style=flat&labelColor=8A2BE2&logoColor=white" alt="library security" />
3128
</a>
@@ -78,9 +75,6 @@ yarn add pointeract
7875

7976
# bun
8077
bun add pointeract
81-
82-
# deno
83-
deno add jsr:@hesprs/pointeract
8478
```
8579

8680
Or include the following lines directly in your HTML file:

docs/en/get-started.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ yarn add pointeract
2828
bun add pointeract
2929
```
3030

31-
```sh [deno]
32-
deno add jsr:@hesprs/pointeract
33-
```
34-
3531
:::
3632

3733
Or include the following lines directly in your HTML file:

jsr.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/BaseModule.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { Pointeract, PointeractInterface } from '@/Pointeract';
21
import type {
32
BaseOptions,
4-
GeneralObject,
53
Pointer,
64
Pointers,
75
StdEvents,
86
ModuleInput as MI,
97
Orchestratable,
108
General,
9+
Coordinates,
10+
GeneralFunction,
1111
} from '@/types';
1212

1313
export type HookKeys =
@@ -36,25 +36,21 @@ export default class BaseModule<
3636
declare private static readonly _BaseModuleBrand: unique symbol; // Nominal marker
3737
declare readonly _Events: E;
3838
declare readonly _Augmentation: A;
39-
protected getNthPointer: Pointeract<[]>['moduleUtils']['getNthPointer'];
40-
protected toTargetCoords: Pointeract<[]>['moduleUtils']['toTargetCoords'];
41-
protected augment: (augmentation: A) => void;
4239
protected dispatch: <K extends keyof E>(
4340
...arg: undefined extends E[K] ? [K] : [K, E[K]]
4441
) => void;
45-
options: O;
42+
4643
constructor(
47-
utils: PointeractInterface['moduleUtils'],
44+
protected augment: (augmentation: A) => void,
45+
dispatch: GeneralFunction,
46+
protected getNthPointer: (n: number) => Pointer,
47+
protected toTargetCoords: (raw: Coordinates) => Coordinates,
4848
protected window: Window,
4949
protected pointers: Pointers,
5050
protected element: HTMLElement,
51-
options: GeneralObject,
51+
public options: O,
5252
) {
53-
this.getNthPointer = utils.getNthPointer;
54-
this.toTargetCoords = utils.toTargetCoords;
55-
this.augment = utils.augment;
56-
this.dispatch = utils.dispatch as typeof this.dispatch;
57-
this.options = options as O;
53+
this.dispatch = dispatch;
5854
}
5955

6056
onPointerDown?: (...args: [event: PointerEvent, pointer: Pointer, pointers: Pointers]) => void;

src/Pointeract.ts

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export class Pointeract<T extends ModuleInputCtor = []> {
3535
this.options = options;
3636
modules.forEach((module) => {
3737
const instance = new module(
38-
this.moduleUtils as PointeractInterface['moduleUtils'],
38+
this.#augment,
39+
this.dispatch,
40+
this.#getNthPointer,
41+
this.#toTargetCoords,
3942
this.#window,
4043
this.#pointers,
4144
this.#element,
@@ -51,73 +54,69 @@ export class Pointeract<T extends ModuleInputCtor = []> {
5154
this.#subscribers[type]?.add(listener);
5255
return this;
5356
};
54-
off<K extends keyof Events<T>>(type: K, listener: (event: Events<T>[K]) => void) {
57+
58+
off = <K extends keyof Events<T>>(type: K, listener: (event: Events<T>[K]) => void) => {
5559
this.#subscribers[type]?.delete(listener);
5660
return this;
57-
}
61+
};
62+
63+
#getNthPointer = (n: number) => {
64+
const error = new Error('[Pointeract] Invalid pointer index.');
65+
if (n < 0 || n >= this.#pointers.size) throw error;
66+
let i = 0;
67+
for (const value of this.#pointers.values()) {
68+
if (i === n) return value;
69+
i++;
70+
}
71+
throw error;
72+
};
5873

59-
private moduleUtils = {
60-
getNthPointer: (n: number) => {
61-
const error = new Error('[Pointeract] Invalid pointer index.');
62-
if (n < 0 || n >= this.#pointers.size) throw error;
63-
let i = 0;
64-
for (const value of this.#pointers.values()) {
65-
if (i === n) return value;
66-
i++;
67-
}
68-
throw error;
69-
},
70-
71-
// Screen to Container
72-
toTargetCoords: (raw: Coordinates) => {
73-
if (this.options.coordinateOutput === 'absolute') return raw;
74-
const rect = this.#element.getBoundingClientRect();
75-
raw.x -= rect.left;
76-
raw.y -= rect.top;
77-
if (this.options.coordinateOutput === 'relative') return raw;
78-
raw.x /= rect.width;
79-
raw.y /= rect.height;
80-
return raw;
81-
},
82-
83-
dispatch: <N extends keyof Events<T>>(
84-
...args: undefined extends Events<T>[N] ? [N] : [N, Events<T>[N]]
85-
) => {
86-
const name = args[0];
87-
const e = args[1];
88-
let lastResult: boolean | Events<T>[N] = true;
89-
for (const value of Object.values(this.#modules)) {
90-
if (!value.modifiers || !(name in value.modifiers)) continue;
91-
lastResult =
92-
e === undefined
93-
? (value.modifiers[name] as () => boolean)()
94-
: (
95-
value.modifiers[name] as (
96-
detail?: Events<T>[N],
97-
) => boolean | Events<T>[N]
98-
)(e);
99-
if (lastResult === false) return;
100-
}
101-
let event: Events<T>[N];
102-
if (lastResult === true) event = e as Events<T>[N];
103-
else event = lastResult;
104-
this.#subscribers[name]?.forEach((listener) => listener(event));
105-
},
106-
107-
augment: (aug: GeneralObject) => {
108-
Object.entries(aug).forEach(([key, value]) => (this[key as '_augmentSlot'] = value));
109-
},
74+
// Screen to Container
75+
#toTargetCoords = (raw: Coordinates) => {
76+
if (this.options.coordinateOutput === 'absolute') return raw;
77+
const rect = this.#element.getBoundingClientRect();
78+
raw.x -= rect.left;
79+
raw.y -= rect.top;
80+
if (this.options.coordinateOutput === 'relative') return raw;
81+
raw.x /= rect.width;
82+
raw.y /= rect.height;
83+
return raw;
11084
};
11185

112-
dispatch = this.moduleUtils.dispatch;
86+
dispatch = <N extends keyof Events<T>>(
87+
...args: undefined extends Events<T>[N] ? [N] : [N, Events<T>[N]]
88+
) => {
89+
const name = args[0];
90+
const e = args[1];
91+
let lastResult: boolean | Events<T>[N] = true;
92+
for (const value of Object.values(this.#modules)) {
93+
if (!value.modifiers || !(name in value.modifiers)) continue;
94+
lastResult =
95+
e === undefined
96+
? (value.modifiers[name] as () => boolean)()
97+
: (value.modifiers[name] as (detail?: Events<T>[N]) => boolean | Events<T>[N])(
98+
e,
99+
);
100+
if (lastResult === false) return;
101+
}
102+
let event: Events<T>[N];
103+
if (lastResult === true) event = e as Events<T>[N];
104+
else event = lastResult;
105+
this.#subscribers[name]?.forEach((listener) => listener(event));
106+
};
107+
108+
#augment = (aug: GeneralObject) => {
109+
const descriptors = Object.getOwnPropertyDescriptors(aug);
110+
Object.defineProperties(this, descriptors);
111+
};
113112

114-
#runHooks<K extends HookKeys>(field: K, ...args: Parameters<Required<BaseModule>[K]>) {
113+
#runHooks = <K extends HookKeys>(field: K, ...args: Parameters<Required<BaseModule>[K]>) => {
115114
Object.values(this.#modules).forEach((module) => {
116115
const hook = module[field];
117116
// oxlint-disable-next-line typescript/no-explicit-any
118117
if (hook) hook(...(args as any));
119118
});
120-
}
119+
};
121120

122121
#onPointerDown = (e: PointerEvent) => {
123122
if (this.#pointers.size >= 2) return;

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type General = any;
44
export type GeneralArray = ReadonlyArray<General>;
55
export type GeneralObject = object;
66
export type GeneralDictionary = Record<Indexable, General>;
7+
export type GeneralFunction = (...args: General[]) => General;
78
export type GeneralConstructor = new (...args: General[]) => General;
89
type Indexable = string | number | symbol;
910

0 commit comments

Comments
 (0)