mirror of
https://github.com/HeyPuter/puter.git
synced 2025-01-24 06:50:22 +08:00
dev: move service interface to trait
This commit is contained in:
parent
273a51fc53
commit
9b1f1817f5
@ -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,15 +26,16 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static IMPLEMENTS = {
|
||||||
|
[TService]: {
|
||||||
init (...a) {
|
init (...a) {
|
||||||
if ( ! this._init ) return;
|
if ( ! this._init ) return;
|
||||||
return this._init(...a);
|
return this._init(...a);
|
||||||
}
|
},
|
||||||
|
|
||||||
construct (o) {
|
construct (o) {
|
||||||
this.$parameters = {};
|
this.$parameters = {};
|
||||||
for ( const k in o ) this.$parameters[k] = o[k];
|
for ( const k in o ) this.$parameters[k] = o[k];
|
||||||
@ -39,7 +43,10 @@ class Service extends AdvancedBase {
|
|||||||
return this._construct(o);
|
return this._construct(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
TService,
|
||||||
Service,
|
Service,
|
||||||
};
|
};
|
||||||
|
@ -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],
|
||||||
});
|
});
|
||||||
|
@ -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];
|
||||||
|
@ -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(),
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user