dev: add config.__set_config_object__()

This commit is contained in:
KernelDeimos
2025-08-28 18:27:45 -04:00
parent 4c8d170202
commit 6d8889e434
8 changed files with 69 additions and 1 deletions

View File

@@ -42,6 +42,7 @@ const { EntityStoreModule } = require("./src/modules/entitystore/EntityStoreModu
const { KVStoreModule } = require("./src/modules/kvstore/KVStoreModule.js");
const { DomainModule } = require("./src/modules/domain/DomainModule.js");
const { DNSModule } = require("./src/modules/dns/DNSModule.js");
const { TestConfigModule } = require("./src/modules/test-config/TestConfigModule.js");
module.exports = {
helloworld: () => {
@@ -77,6 +78,7 @@ module.exports = {
MemoryStorageModule,
SelfHostedModule,
TestDriversModule,
TestConfigModule,
PuterAIModule,
BroadcastModule,
InternetModule,

View File

@@ -219,8 +219,26 @@ const config_pointer = {};
const config_runtime_values = {
$: 'runtime-values'
};
let initialPrototype = config_to_export;
Object.setPrototypeOf(config_runtime_values, config_to_export);
config_to_export = config_runtime_values
config_to_export.__set_config_object__ = (object, options = {}) => {
// options for this method
const replacePrototype = options.replacePrototype ?? true;
const useInitialPrototype = options.useInitialPrototype ?? true;
// maybe replace prototype
if ( replacePrototype ) {
const newProto = useInitialPrototype
? initialPrototype
: Object.getPrototypeOf(config_runtime_values);
Object.setPrototypeOf(object, newProto);
}
// use this object as the prototype
Object.setPrototypeOf(config_runtime_values, object);
};
// These can be difficult to find and cause painful
// confusing issues, so we log any time this happens

View File

@@ -0,0 +1,15 @@
const { AdvancedBase } = require("@heyputer/putility");
class TestConfigModule extends AdvancedBase {
async install (context) {
const services = context.get('services');
const TestConfigUpdateService = require('./TestConfigUpdateService');
services.registerService('__test-config-update', TestConfigUpdateService);
const TestConfigReadService = require('./TestConfigReadService');
services.registerService('__test-config-read', TestConfigReadService);
}
}
module.exports = {
TestConfigModule,
};

View File

@@ -0,0 +1,11 @@
const BaseService = require("../../services/BaseService");
class TestConfigReadService extends BaseService {
async _init () {
this.log.noticeme('test config value (should be abcdefg) is: ' +
this.global_config.testConfigValue,
);
}
}
module.exports = TestConfigReadService;

View File

@@ -0,0 +1,12 @@
const BaseService = require("../../services/BaseService");
class TestConfigUpdateService extends BaseService {
async _run_as_early_as_possible () {
const config = this.global_config;
config.__set_config_object__({
testConfigValue: 'abcdefg'
});
}
}
module.exports = TestConfigUpdateService;

View File

@@ -54,7 +54,10 @@ class BaseService extends concepts.Service {
this.global_config.server_id = 'local';
}
}
async run_as_early_as_possible () {
await (this._run_as_early_as_possible || NOOP).call(this, this.args);
}
/**
* Creates the service's data structures and initial values.

View File

@@ -173,6 +173,11 @@ class Container {
* initialized or rejects if any service initialization fails.
*/
async init () {
for ( const k in this.instances_ ) {
if ( ! this.instances_[k]._run_as_early_as_possible ) continue;
this.logger.info(`very early hooks for ${k}`);
await this.instances_[k].run_as_early_as_possible();
}
for ( const k in this.instances_ ) {
this.logger.info(`constructing ${k}`);
await this.instances_[k].construct();

View File

@@ -87,6 +87,7 @@ const main = async () => {
SelfHostedModule,
BroadcastModule,
TestDriversModule,
TestConfigModule,
PuterAIModule,
InternetModule,
DevelopmentModule,
@@ -105,6 +106,7 @@ const main = async () => {
k.add_module(new SelfHostedModule());
k.add_module(new BroadcastModule());
k.add_module(new TestDriversModule());
k.add_module(new TestConfigModule());
k.add_module(new PuterAIModule());
k.add_module(new InternetModule());
k.add_module(new DNSModule());