Skip to content

Commit

Permalink
Refactor all services to be used via decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Jan 3, 2024
1 parent b997dd4 commit 002f141
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
39 changes: 9 additions & 30 deletions packages/js-toolkit/Base/managers/ServicesManager.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import { usePointer, useRaf, useResize, useScroll, useKey, useLoad } from '../../services/index.js';
import { AbstractManager } from './AbstractManager.js';
import { noop, isFunction, isDefined } from '../../utils/index.js';
import type {
PointerServiceInterface,
RafServiceInterface,
ResizeServiceInterface,
ServiceInterface,
ScrollServiceInterface,
ResizeServiceInterface,
RafServiceInterface,
PointerServiceInterface,
KeyServiceInterface,
LoadServiceInterface,
ServiceInterface,
} from '../../services/index.js';
import { AbstractManager } from './AbstractManager.js';
import { noop, isFunction, isDefined, isDev } from '../../utils/index.js';

const SERVICES_MAP = {
scrolled: useScroll,
resized: useResize,
ticked: useRaf,
moved: usePointer,
keyed: useKey,
loaded: useLoad,
};

const SERVICE_NAMES = Object.keys(SERVICES_MAP);

type Services = {
scrolled: ScrollServiceInterface;
Expand Down Expand Up @@ -48,10 +36,7 @@ export class ServicesManager extends AbstractManager {
* Get registered services.
*/
get __services() {
return {
...this.__customServices,
...SERVICES_MAP,
};
return { ...this.__customServices };
}

/**
Expand Down Expand Up @@ -174,14 +159,8 @@ export class ServicesManager extends AbstractManager {
* @param {string} name
* The name of the service hook.
*/
unregister(name: string) {
if (SERVICE_NAMES.includes(name)) {
if (isDev) {
throw new Error(`[ServicesManager] The \`${name}\` core service can not be unregistered.`);
}
return;
}

unregister(name) {
// @ts-ignore
this.__base.__removeEmits(name);
delete this.__customServices[name];
}
Expand Down
1 change: 1 addition & 0 deletions packages/js-toolkit/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './withMountWhenPrefersMotion.js';
export * from './withRelativePointer.js';
export * from './withResponsiveOptions.js';
export * from './withScrolledInView/index.js';
export * from './withServices';
45 changes: 45 additions & 0 deletions packages/js-toolkit/decorators/withServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { ServiceInterface } from '../services/index.js';
import type { Base, BaseConstructor } from '../Base/index.js';

export type WithServicesOptions = Record<string, (instance:Base) => ServiceInterface<any>>

Check failure on line 4 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Replace `Base)·=>·ServiceInterface<any>>` with `·Base)·=>·ServiceInterface<any>>;`

Check warning on line 4 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Unexpected any. Specify a different type.

Check failure on line 4 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Missing semicolon.

/**
* Add dragging capabilities to a component.
*
* @template {BaseConstructor} T
* @param {T} BaseClass
* @param {Array<any>} services
* @returns {T}
*/
export function withServices(BaseClass:BaseConstructor, services:WithServicesOptions = {}) {

Check failure on line 14 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Replace `BaseConstructor,·services:` with `·BaseConstructor,·services:·`
const __services = Object.entries(services);
// @ts-ignore
return class extends BaseClass {
static config = {
name: `${BaseClass.config.name}WithServices`,
emits: __services.map(([name]) => name),
};

/**
* Class constructor.

Check warning on line 24 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Expected 1 lines after block description
* @param {HTMLElement} el
*/
constructor(el) {
super(el);

this.$on('mounted', () => {
__services.forEach(([name, getInstance]) => {

Check warning on line 31 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Use `for…of` instead of `.forEach(…)`.
this.$services.register(name, getInstance(this));

Check failure on line 32 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type 'ServiceInterface<any>' is not assignable to parameter of type '<T>(...args: unknown[]) => ServiceInterface<T>'.
this.$services.enable(name);
});
});

this.$on('destroyed', () => {
__services.forEach(([name]) => {

Check warning on line 38 in packages/js-toolkit/decorators/withServices.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Use `for…of` instead of `.forEach(…)`.
this.$services.disable(name);
this.$services.unregister(name);
});
});
}
};
}

0 comments on commit 002f141

Please sign in to comment.