@khapu2906/treasure-chest 1.0.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,110 +0,0 @@
1
- "use strict";
2
- /**
3
- * Treasure Chest Service Container in TypeScript
4
- * Supports:
5
- * - bind / singleton
6
- * - conditional binding
7
- * - contextual binding
8
- * - alias
9
- * - resolve
10
- */
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Container = void 0;
13
- class Container {
14
- constructor() {
15
- this.bindings = [];
16
- this.instances = {};
17
- this.aliases = {};
18
- }
19
- /**
20
- * Bind a dependency (default: transient)
21
- */
22
- bind(key, factory, condition) {
23
- this.bindings.push({ key, factory, singleton: false, condition });
24
- }
25
- /**
26
- * Bind a dependency as singleton (only one instance created)
27
- */
28
- singleton(key, factory, condition) {
29
- this.bindings.push({ key, factory, singleton: true, condition });
30
- }
31
- /**
32
- * Add an alias for an existing key
33
- */
34
- alias(aliasKey, originalKey) {
35
- this.aliases[aliasKey] = originalKey;
36
- }
37
- /**
38
- * Internal: find correct binding based on key + condition + context
39
- */
40
- findBinding(key, context) {
41
- const realKey = this.aliases[key] || key;
42
- const bindings = this.bindings.filter((b) => b.key === realKey);
43
- if (bindings.length === 0) {
44
- throw new Error(`No binding found for key: ${realKey}`);
45
- }
46
- // Priority:
47
- // 1. Binding with context and condition true
48
- // 2. Binding without context but condition true
49
- // 3. Binding default first
50
- const match = bindings.find((b) => b.context === context && (!b.condition || b.condition(this))) ||
51
- bindings.find((b) => !b.context && (!b.condition || b.condition(this))) ||
52
- bindings[0];
53
- return match;
54
- }
55
- /**
56
- * Resolve an instance from the container
57
- */
58
- resolve(key, context) {
59
- const prevContext = this.currentContext;
60
- this.currentContext = context || key;
61
- // Get real key (resolve alias)
62
- const realKey = this.aliases[key] || key;
63
- const binding = this.findBinding(key, this.currentContext);
64
- // Create unique cache key for contextual singletons
65
- const cacheKey = binding.context
66
- ? `${realKey}::${binding.context}`
67
- : realKey;
68
- // Return existing singleton instance
69
- if (binding.singleton && this.instances[cacheKey]) {
70
- this.currentContext = prevContext;
71
- return this.instances[cacheKey];
72
- }
73
- const instance = binding.factory(this);
74
- if (binding.singleton) {
75
- this.instances[cacheKey] = instance;
76
- }
77
- this.currentContext = prevContext;
78
- return instance;
79
- }
80
- /**
81
- * Contextual binding:
82
- * Example:
83
- * container.when('UserService').needs('UserRepo').give(...)
84
- */
85
- when(context) {
86
- return {
87
- needs: (depKey) => ({
88
- give: (factory) => {
89
- this.bindings.push({
90
- key: depKey,
91
- factory,
92
- singleton: false,
93
- context,
94
- });
95
- },
96
- }),
97
- };
98
- }
99
- /**
100
- * Clear all bindings and instances (useful for testing)
101
- */
102
- reset() {
103
- this.bindings = [];
104
- this.instances = {};
105
- this.aliases = {};
106
- this.currentContext = undefined;
107
- }
108
- }
109
- exports.Container = Container;
110
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AAaH,MAAa,SAAS;IAAtB;QACU,aAAQ,GAAc,EAAE,CAAC;QACzB,cAAS,GAAwB,EAAE,CAAC;QACpC,YAAO,GAA2B,EAAE,CAAC;IA+G/C,CAAC;IA5GC;;OAEG;IACH,IAAI,CAAI,GAAW,EAAE,OAAqB,EAAE,SAAuB;QACjE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IACpE,CAAC;IAED;;OAEG;IACH,SAAS,CAAI,GAAW,EAAE,OAAqB,EAAE,SAAuB;QACtE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;IACnE,CAAC;IAED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