-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all services to be used via decorators
- Loading branch information
1 parent
b997dd4
commit 002f141
Showing
3 changed files
with
55 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Code-Quality
Check warning on line 4 in packages/js-toolkit/decorators/withServices.ts GitHub Actions / Code-Quality
|
||
|
||
/** | ||
* 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 = {}) { | ||
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. | ||
* @param {HTMLElement} el | ||
*/ | ||
constructor(el) { | ||
super(el); | ||
|
||
this.$on('mounted', () => { | ||
__services.forEach(([name, getInstance]) => { | ||
this.$services.register(name, getInstance(this)); | ||
this.$services.enable(name); | ||
}); | ||
}); | ||
|
||
this.$on('destroyed', () => { | ||
__services.forEach(([name]) => { | ||
this.$services.disable(name); | ||
this.$services.unregister(name); | ||
}); | ||
}); | ||
} | ||
}; | ||
} |