dev: move service interface to trait

This commit is contained in:
KernelDeimos 2024-10-17 21:52:17 -04:00 committed by Eric Dubé
parent 273a51fc53
commit 9b1f1817f5
4 changed files with 32 additions and 17 deletions

View File

@ -2,6 +2,9 @@ const { AdvancedBase } = require("../AdvancedBase");
const NOOP = async () => {}; const NOOP = async () => {};
/** Service trait */
const TService = Symbol('TService');
/** /**
* Service will be incrementally updated to consolidate * Service will be incrementally updated to consolidate
* BaseService in Puter's backend with Service in Puter's frontend, * BaseService in Puter's backend with Service in Puter's frontend,
@ -23,23 +26,27 @@ class Service extends AdvancedBase {
static create ({ parameters, context }) { static create ({ parameters, context }) {
const ins = new this(); const ins = new this();
ins._.context = context; ins._.context = context;
ins.construct(parameters); ins.as(TService).construct(parameters);
return ins; return ins;
} }
init (...a) { static IMPLEMENTS = {
if ( ! this._init ) return; [TService]: {
return this._init(...a); init (...a) {
} if ( ! this._init ) return;
return this._init(...a);
construct (o) { },
this.$parameters = {}; construct (o) {
for ( const k in o ) this.$parameters[k] = o[k]; this.$parameters = {};
if ( ! this._construct ) return; for ( const k in o ) this.$parameters[k] = o[k];
return this._construct(o); if ( ! this._construct ) return;
return this._construct(o);
}
}
} }
} }
module.exports = { module.exports = {
TService,
Service, Service,
}; };

View File

@ -18,14 +18,15 @@
*/ */
module.exports = { module.exports = {
name: 'Properties', name: 'Properties',
depends: ['Listeners'],
install_in_instance: (instance) => { install_in_instance: (instance) => {
const properties = instance._get_merged_static_object('PROPERTIES'); const properties = instance._get_merged_static_object('PROPERTIES');
instance.onchange = (name, callback) => { instance.onchange = (name, callback) => {
instance.__properties[name].listeners.push(callback); instance._.properties[name].listeners.push(callback);
}; };
instance.__properties = {}; instance._.properties = {};
for ( const k in properties ) { for ( const k in properties ) {
const state = { const state = {
@ -33,7 +34,7 @@ module.exports = {
listeners: [], listeners: [],
value: undefined, value: undefined,
}; };
instance.__properties[k] = state; instance._.properties[k] = state;
if ( typeof properties[k] === 'object' ) { if ( typeof properties[k] === 'object' ) {
// This will be supported in the future. // This will be supported in the future.
@ -54,7 +55,7 @@ module.exports = {
return state.value; return state.value;
}, },
set: (value) => { set: (value) => {
for ( const listener of instance.__properties[k].listeners ) { for ( const listener of instance._.properties[k].listeners ) {
listener(value, { listener(value, {
old_value: instance[k], old_value: instance[k],
}); });

View File

@ -30,7 +30,11 @@ module.exports = {
for ( const cls of chain ) { for ( const cls of chain ) {
const cls_traits = cls.IMPLEMENTS; const cls_traits = cls.IMPLEMENTS;
if ( ! cls_traits ) continue; if ( ! cls_traits ) continue;
for ( const trait_name in cls_traits ) { const trait_keys = [
...Object.getOwnPropertySymbols(cls_traits),
...Object.keys(cls_traits),
];
for ( const trait_name of trait_keys ) {
const impl = instance._.impls[trait_name] ?? const impl = instance._.impls[trait_name] ??
(instance._.impls[trait_name] = {}); (instance._.impls[trait_name] = {});
const cls_impl = cls_traits[trait_name]; const cls_impl = cls_traits[trait_name];

View File

@ -1,4 +1,5 @@
const { AdvancedBase } = require("../AdvancedBase"); const { AdvancedBase } = require("../AdvancedBase");
const { TService } = require("../concepts/Service");
const mkstatus = name => { const mkstatus = name => {
const c = class { const c = class {
@ -99,7 +100,9 @@ class ServiceManager extends AdvancedBase {
async init_service_ (name) { async init_service_ (name) {
const entry = this.services_m_[name]; const entry = this.services_m_[name];
entry.status = new this.constructor.StatusInitializing(); entry.status = new this.constructor.StatusInitializing();
await entry.instance.init();
const service_impl = entry.instance.as(TService);
await service_impl.init();
entry.status = new this.constructor.StatusRunning({ entry.status = new this.constructor.StatusRunning({
start_ts: new Date(), start_ts: new Date(),
}); });