Skip to content

Commit b4e01f3

Browse files
committed
initiated fixing leaks
1 parent 08a18fc commit b4e01f3

5 files changed

Lines changed: 23 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-openscriptjs",
3-
"version": "1.0.22",
3+
"version": "1.0.30",
44
"description": "OpenScriptJs Framework - A lightweight, reactive JavaScript framework for building modern web applications",
55
"type": "module",
66
"main": "./dist/modular-openscriptjs.umd.js",

src/component/Component.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export default class Component {
105105
this.on(this.EVENTS.bound, (th) => (th.bound = true));
106106
this.on(this.EVENTS.mounted, (th) => (th.mounted = true));
107107
this.on(this.EVENTS.visible, (th) => (th.visible = true));
108-
this.getDeclaredListeners();
109108

110109
this.$$ojs = {
111110
routeChanged: () => {
@@ -360,9 +359,6 @@ export default class Component {
360359
this.emit(this.EVENTS.premount);
361360
await this.bindComponent();
362361
this.emit(this.EVENTS.mounted);
363-
364-
// Mark as registered
365-
this.__ojsRegistered = true;
366362
}
367363

368364
/**

src/core/Container.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ export default class Container {
9090
/**
9191
* Resolve a service by name
9292
* @param {string} name - Service identifier
93+
* @param {any} defaultValue - Default value to return if service is not found
9394
* @returns {any} - The resolved service instance
9495
*/
95-
resolve(name) {
96+
resolve(name, defaultValue = null) {
9697
// Check for circular dependencies
9798
if (this.resolvingStack.has(name)) {
9899
const stack = Array.from(this.resolvingStack).join(" -> ");
@@ -101,7 +102,7 @@ export default class Container {
101102

102103
const service = this.services.get(name);
103104
if (!service) {
104-
throw new Error(`Service "${name}" not registered in container`);
105+
return defaultValue;
105106
}
106107

107108
// Return cached singleton instance

src/core/Runner.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,50 @@ export default class Runner {
2323
}
2424

2525
async run(...cls) {
26+
container.value(
27+
"__ojs_registrations",
28+
container.resolve("__ojs_registrations") ?? {}
29+
);
30+
31+
const registrations = container.resolve("__ojs_registrations");
32+
2633
for (let i = 0; i < cls.length; i++) {
2734
let c = cls[i];
2835
let instance;
36+
const classKey = this.getClassKey(c);
2937

3038
if (!this.isClass(c)) {
3139
// Functional component - always create new instance (not a singleton)
3240
instance = new Component(c.name);
3341
instance.render = c.bind(instance);
3442
} else {
35-
// For classes, check if singleton exists in container
36-
const classKey = this.getClassKey(c);
43+
if (registrations[classKey] === "ongoing") {
44+
continue;
45+
}
3746

3847
if (container.has(classKey)) {
39-
// Retrieve existing singleton from container
4048
instance = container.resolve(classKey);
41-
42-
// Skip if already registered (has __ojsRegistered flag)
4349
if (instance.__ojsRegistered) {
4450
continue;
4551
}
4652
} else {
47-
// Create new instance and register as singleton in container
4853
instance = new c();
4954
container.singleton(classKey, () => instance, []);
55+
registrations[classKey] = "ongoing";
5056
}
5157
}
5258

5359
if (instance instanceof Component) {
5460
instance.getDeclaredListeners();
5561
await instance.mount();
62+
instance.__ojsRegistered = true;
63+
registrations[classKey] = "completed";
5664
} else if (instance instanceof Mediator || instance instanceof Listener) {
5765
await instance.register();
66+
registrations[classKey] = "completed";
5867
} else if (instance instanceof Context) {
5968
// Context instances don't need registration
69+
registrations[classKey] = "completed";
6070
} else {
6171
throw Error(
6272
`You can only pass declarations which extend Component, Mediator or Listener`

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ const dom = DOM;
127127
/**
128128
* Access a service from the IoC container or get the container itself
129129
* @param {string|undefined} [instance] - Service name or undefined to get container
130+
* @param {any} [defaultValue] - Default value to return if service is not found
130131
* @returns {any}
131132
*/
132-
const app = (instance = null) => {
133+
const app = (instance = null, defaultValue = null) => {
133134
if (instance === null) return container;
134135

135-
return container.resolve(instance);
136+
return container.resolve(instance, defaultValue);
136137
};
137138

138139
// Export everything

0 commit comments

Comments
 (0)