@runtime-labs/observer-core 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Anton Gor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Readme.md ADDED
@@ -0,0 +1,25 @@
1
+ # @runtime-labs/composable-core
2
+
3
+ Runtime engine for Vue Composable Observer.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @runtime-labs/composable-core
9
+ ```
10
+
11
+ ## Purpose
12
+
13
+ This package contains the runtime tracking engine used by Vue Composable Observer.
14
+
15
+ Most users should install:
16
+
17
+ ```bash
18
+ pnpm add -D @runtime-labs/composable-plugin
19
+ ```
20
+
21
+ instead.
22
+
23
+ ## License
24
+
25
+ MIT
@@ -0,0 +1,68 @@
1
+ interface ComposableInstance {
2
+ id: string;
3
+ name: string;
4
+ file: string;
5
+ createdAt: number;
6
+ state: unknown;
7
+ dependencyIds?: Set<string> | null;
8
+ parentId?: string | null;
9
+ component?: {
10
+ uid: number;
11
+ name: string;
12
+ file?: string;
13
+ };
14
+ }
15
+
16
+ declare function registerInstance(record: ComposableInstance): void;
17
+ declare function unregisterInstance(id: string): void;
18
+ declare function getInstances(): ComposableInstance[];
19
+ declare function clearInstances(): void;
20
+ declare function updateInstanceState(id: string, state: unknown): void;
21
+ declare function getInstanceById(id: ComposableInstance['id']): ComposableInstance | null;
22
+
23
+ type ObserverEvent = {
24
+ type: 'instance:registered';
25
+ instanceId: string;
26
+ } | {
27
+ type: 'instance:unregistered';
28
+ instanceId: string;
29
+ } | {
30
+ type: 'instance:stateUpdated';
31
+ instanceId: string;
32
+ state: unknown;
33
+ } | {
34
+ type: 'instance:dependencyRegistered';
35
+ instanceId: string;
36
+ dependencyId: string;
37
+ } | {
38
+ type: 'instance:cleared';
39
+ };
40
+ type Listener = (event: ObserverEvent) => void;
41
+ declare function subscribe(listener: Listener): () => void;
42
+ declare function notifySubscribers(event: ObserverEvent): void;
43
+
44
+ declare function createObserver(): {
45
+ getInstances: typeof getInstances;
46
+ subscribe: typeof subscribe;
47
+ };
48
+
49
+ declare global {
50
+ interface Window {
51
+ __COMPOSABLE_OBSERVER__?: ReturnType<typeof createObserver>;
52
+ }
53
+ }
54
+ declare function initComposableObserver(): void;
55
+
56
+ declare function createInstanceId(): `${string}-${string}-${string}-${string}-${string}`;
57
+ declare function createInstance(id: string, name: string, file: string, parentId?: string | null): ComposableInstance;
58
+
59
+ declare function trackComposable<TArgs extends unknown[], TResult>(name: string, file: string, fn: (...args: TArgs) => TResult): (...args: TArgs) => TResult;
60
+
61
+ declare function trackStateChanges(state: unknown, callback: () => void): () => void;
62
+
63
+ declare function getCurrentComposable(): string | null;
64
+ declare function runWithComposable<T>(composableId: string | null, fn: () => T): T;
65
+
66
+ declare const version = "0.0.0";
67
+
68
+ export { clearInstances, createInstance, createInstanceId, createObserver, getCurrentComposable, getInstanceById, getInstances, initComposableObserver, notifySubscribers, registerInstance, runWithComposable, subscribe, trackComposable, trackStateChanges, unregisterInstance, updateInstanceState, version };
@@ -0,0 +1,68 @@
1
+ interface ComposableInstance {
2
+ id: string;
3
+ name: string;
4
+ file: string;
5
+ createdAt: number;
6
+ state: unknown;
7
+ dependencyIds?: Set<string> | null;
8
+ parentId?: string | null;
9
+ component?: {
10
+ uid: number;
11
+ name: string;
12
+ file?: string;
13
+ };
14
+ }
15
+
16
+ declare function registerInstance(record: ComposableInstance): void;
17
+ declare function unregisterInstance(id: string): void;
18
+ declare function getInstances(): ComposableInstance[];
19
+ declare function clearInstances(): void;
20
+ declare function updateInstanceState(id: string, state: unknown): void;
21
+ declare function getInstanceById(id: ComposableInstance['id']): ComposableInstance | null;
22
+
23
+ type ObserverEvent = {
24
+ type: 'instance:registered';
25
+ instanceId: string;
26
+ } | {
27
+ type: 'instance:unregistered';
28
+ instanceId: string;
29
+ } | {
30
+ type: 'instance:stateUpdated';
31
+ instanceId: string;
32
+ state: unknown;
33
+ } | {
34
+ type: 'instance:dependencyRegistered';
35
+ instanceId: string;
36
+ dependencyId: string;
37
+ } | {
38
+ type: 'instance:cleared';
39
+ };
40
+ type Listener = (event: ObserverEvent) => void;
41
+ declare function subscribe(listener: Listener): () => void;
42
+ declare function notifySubscribers(event: ObserverEvent): void;
43
+
44
+ declare function createObserver(): {
45
+ getInstances: typeof getInstances;
46
+ subscribe: typeof subscribe;
47
+ };
48
+
49
+ declare global {
50
+ interface Window {
51
+ __COMPOSABLE_OBSERVER__?: ReturnType<typeof createObserver>;
52
+ }
53
+ }
54
+ declare function initComposableObserver(): void;
55
+
56
+ declare function createInstanceId(): `${string}-${string}-${string}-${string}-${string}`;
57
+ declare function createInstance(id: string, name: string, file: string, parentId?: string | null): ComposableInstance;
58
+
59
+ declare function trackComposable<TArgs extends unknown[], TResult>(name: string, file: string, fn: (...args: TArgs) => TResult): (...args: TArgs) => TResult;
60
+
61
+ declare function trackStateChanges(state: unknown, callback: () => void): () => void;
62
+
63
+ declare function getCurrentComposable(): string | null;
64
+ declare function runWithComposable<T>(composableId: string | null, fn: () => T): T;
65
+
66
+ declare const version = "0.0.0";
67
+
68
+ export { clearInstances, createInstance, createInstanceId, createObserver, getCurrentComposable, getInstanceById, getInstances, initComposableObserver, notifySubscribers, registerInstance, runWithComposable, subscribe, trackComposable, trackStateChanges, unregisterInstance, updateInstanceState, version };
package/dist/index.mjs ADDED
@@ -0,0 +1,240 @@
1
+ import { isRef, isReactive, watch, toValue, getCurrentScope, getCurrentInstance, onScopeDispose } from 'vue';
2
+
3
+ const listeners = /* @__PURE__ */ new Set();
4
+ function subscribe(listener) {
5
+ listeners.add(listener);
6
+ return () => {
7
+ unsubscribe(listener);
8
+ };
9
+ }
10
+ function unsubscribe(listener) {
11
+ listeners.delete(listener);
12
+ }
13
+ function notifySubscribers(event) {
14
+ listeners.forEach((listener) => listener(event));
15
+ }
16
+
17
+ const composables = /* @__PURE__ */ new Map();
18
+ function registerInstance(record) {
19
+ composables.set(record.id, record);
20
+ notifySubscribers({
21
+ type: "instance:registered",
22
+ instanceId: record.id
23
+ });
24
+ }
25
+ function unregisterInstance(id) {
26
+ composables.delete(id);
27
+ notifySubscribers({
28
+ type: "instance:unregistered",
29
+ instanceId: id
30
+ });
31
+ }
32
+ function getInstances() {
33
+ return [...composables.values()];
34
+ }
35
+ function clearInstances() {
36
+ composables.clear();
37
+ notifySubscribers({
38
+ type: "instance:cleared"
39
+ });
40
+ }
41
+ function updateInstanceState(id, state) {
42
+ const instance = composables.get(id);
43
+ if (instance) {
44
+ instance.state = state;
45
+ notifySubscribers({
46
+ type: "instance:stateUpdated",
47
+ instanceId: id,
48
+ state
49
+ });
50
+ }
51
+ }
52
+ function updateInstanceComponent(id, component) {
53
+ const instance = composables.get(id);
54
+ if (instance) {
55
+ instance.component = component;
56
+ }
57
+ }
58
+ function registerDependency(instanceId, dependencyId) {
59
+ const instance = composables.get(instanceId);
60
+ if (!instance) return;
61
+ if (!instance.dependencyIds) {
62
+ instance.dependencyIds = /* @__PURE__ */ new Set();
63
+ }
64
+ instance.dependencyIds.add(dependencyId);
65
+ notifySubscribers({
66
+ type: "instance:dependencyRegistered",
67
+ instanceId,
68
+ dependencyId
69
+ });
70
+ }
71
+ function getInstanceById(id) {
72
+ return composables.get(id) ?? null;
73
+ }
74
+
75
+ function createObserver() {
76
+ return {
77
+ getInstances,
78
+ subscribe
79
+ };
80
+ }
81
+
82
+ function initComposableObserver() {
83
+ if (typeof window === "undefined") {
84
+ return;
85
+ }
86
+ if (window.__COMPOSABLE_OBSERVER__) {
87
+ return;
88
+ }
89
+ window.__COMPOSABLE_OBSERVER__ = createObserver();
90
+ }
91
+
92
+ function createInstanceId() {
93
+ return crypto.randomUUID();
94
+ }
95
+ function createInstance(id, name, file, parentId = null) {
96
+ return {
97
+ id: id || createInstanceId(),
98
+ name,
99
+ file,
100
+ createdAt: Date.now(),
101
+ state: null,
102
+ dependencyIds: null,
103
+ parentId
104
+ };
105
+ }
106
+
107
+ let currentComposableId = null;
108
+ function setCurrentComposable(id) {
109
+ currentComposableId = id;
110
+ }
111
+ function getCurrentComposable() {
112
+ return currentComposableId;
113
+ }
114
+ function runWithComposable(composableId, fn) {
115
+ const previousComposable = getCurrentComposable();
116
+ setCurrentComposable(composableId);
117
+ try {
118
+ return fn();
119
+ } finally {
120
+ setCurrentComposable(previousComposable);
121
+ }
122
+ }
123
+
124
+ function createComposableRuntime(name, file, parentId) {
125
+ const id = createInstanceId();
126
+ const instance = createInstance(
127
+ id,
128
+ name,
129
+ file,
130
+ parentId
131
+ );
132
+ registerInstance(instance);
133
+ return {
134
+ id,
135
+ instance,
136
+ updateState(state) {
137
+ return updateInstanceState(id, state);
138
+ },
139
+ dispose() {
140
+ unregisterInstance(id);
141
+ }
142
+ };
143
+ }
144
+
145
+ function trackStateChanges(state, callback) {
146
+ const stops = [];
147
+ function storeStop(stop) {
148
+ stops.push(stop);
149
+ }
150
+ function track(value) {
151
+ if (isRef(value) || isReactive(value)) {
152
+ storeStop(
153
+ watch(
154
+ () => toValue(value),
155
+ () => callback(),
156
+ {
157
+ deep: true,
158
+ flush: "post"
159
+ }
160
+ )
161
+ );
162
+ return;
163
+ }
164
+ if (typeof value === "object" && value !== null) {
165
+ for (const nested of Object.values(value)) {
166
+ track(nested);
167
+ }
168
+ }
169
+ }
170
+ track(state);
171
+ return () => {
172
+ for (const stop of stops) {
173
+ stop();
174
+ }
175
+ };
176
+ }
177
+
178
+ function trackComposable(name, file, fn) {
179
+ return (...args) => {
180
+ const parentComposableId = getCurrentComposable();
181
+ const runtime = createComposableRuntime(
182
+ name,
183
+ file,
184
+ parentComposableId
185
+ );
186
+ if (parentComposableId && parentComposableId !== runtime.id) {
187
+ registerDependency(
188
+ parentComposableId,
189
+ runtime.id
190
+ );
191
+ }
192
+ const state = runWithComposable(
193
+ runtime.id,
194
+ () => fn(...args)
195
+ );
196
+ runtime.updateState(state);
197
+ const stopTracking = trackStateChanges(
198
+ state,
199
+ () => {
200
+ notifySubscribers({
201
+ type: "instance:stateUpdated",
202
+ instanceId: runtime.id,
203
+ state
204
+ });
205
+ }
206
+ );
207
+ const scope = getCurrentScope();
208
+ const vm = getCurrentInstance();
209
+ if (vm && !parentComposableId) {
210
+ updateInstanceComponent(
211
+ runtime.id,
212
+ {
213
+ uid: vm.uid,
214
+ name: vm.type.__name ?? vm.type.name ?? "Anonymous",
215
+ file: vm.type.__file
216
+ }
217
+ );
218
+ }
219
+ if (scope) {
220
+ onScopeDispose(() => {
221
+ {
222
+ console.log(`Unregistering composable instance: ${runtime.instance.name} (ID: ${runtime.instance.id})`);
223
+ }
224
+ stopTracking();
225
+ runtime.dispose();
226
+ });
227
+ } else {
228
+ {
229
+ console.warn(
230
+ `Composable instance "${runtime.instance.name}" (ID: ${runtime.instance.id}) is not registered to any scope. It will not be automatically unregistered and may lead to memory leaks.`
231
+ );
232
+ }
233
+ }
234
+ return state;
235
+ };
236
+ }
237
+
238
+ const version = "0.0.0";
239
+
240
+ export { clearInstances, createInstance, createInstanceId, createObserver, getCurrentComposable, getInstanceById, getInstances, initComposableObserver, notifySubscribers, registerInstance, runWithComposable, subscribe, trackComposable, trackStateChanges, unregisterInstance, updateInstanceState, version };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@runtime-labs/observer-core",
3
+ "version": "1.0.0",
4
+ "description": "Runtime engine for observing Vue composables and tracking their relationships.",
5
+ "type": "module",
6
+ "author": {
7
+ "name": "Anton Gor",
8
+ "email": "anton-gor-dev@hotmail.com"
9
+ },
10
+ "license": "MIT",
11
+ "homepage": "https://github.com/runtime-labs/vue-composable-observer",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/runtime-labs/vue-composable-observer.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/runtime-labs/vue-composable-observer/issues"
18
+ },
19
+ "keywords": [
20
+ "vue",
21
+ "composable",
22
+ "runtime",
23
+ "observability",
24
+ "debugging",
25
+ "devtools",
26
+ "vue3"
27
+ ],
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.mjs"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "peerDependencies": {
41
+ "vue": "^3.5.0"
42
+ },
43
+ "devDependencies": {
44
+ "vue": "^3.5.0"
45
+ },
46
+ "scripts": {
47
+ "build": "unbuild",
48
+ "dev": "unbuild --stub",
49
+ "pack": "pnpm pack"
50
+ }
51
+ }