mycelia-kernel-plugin 1.2.0 → 1.4.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.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Mycelia Plugin System - Vue Listener Helpers
3
+ *
4
+ * Vue composables for event listener management.
5
+ */
6
+
7
+ import { ref, onUnmounted } from 'vue';
8
+ import { useMycelia } from './index.js';
9
+
10
+ /**
11
+ * useListener - Register an event listener with automatic cleanup
12
+ *
13
+ * @param {string} eventName - Event name/path to listen for
14
+ * @param {Function} handler - Handler function: (message) => void
15
+ *
16
+ * @example
17
+ * ```js
18
+ * import { useListener } from 'mycelia-kernel-plugin/vue';
19
+ *
20
+ * export default {
21
+ * setup() {
22
+ * useListener('user:created', (msg) => {
23
+ * console.log('User created:', msg.body);
24
+ * });
25
+ * }
26
+ * }
27
+ * ```
28
+ */
29
+ export function useListener(eventName, handler) {
30
+ const system = useMycelia();
31
+ const listeners = system?.listeners; // useListeners facet
32
+
33
+ if (!listeners || !listeners.hasListeners?.()) {
34
+ return;
35
+ }
36
+
37
+ listeners.on(eventName, handler);
38
+
39
+ onUnmounted(() => {
40
+ listeners.off?.(eventName, handler);
41
+ });
42
+ }
43
+
44
+ /**
45
+ * useEventStream - Subscribe to events and keep them in reactive state
46
+ *
47
+ * @param {string} eventName - Event name/path to listen for
48
+ * @param {Object} [options={}] - Options
49
+ * @param {boolean} [options.accumulate=false] - If true, accumulate events in array
50
+ * @returns {import('vue').Ref<any|any[]|null>} Reactive ref to latest event value, array of events, or null
51
+ *
52
+ * @example
53
+ * ```js
54
+ * import { useEventStream } from 'mycelia-kernel-plugin/vue';
55
+ *
56
+ * export default {
57
+ * setup() {
58
+ * const events = useEventStream('todo:created', { accumulate: true });
59
+ * return { events };
60
+ * }
61
+ * }
62
+ * ```
63
+ */
64
+ export function useEventStream(eventName, options = {}) {
65
+ const { accumulate = false } = options;
66
+ const value = ref(accumulate ? [] : null);
67
+
68
+ useListener(eventName, (msg) => {
69
+ if (accumulate) {
70
+ value.value = [...value.value, msg.body];
71
+ } else {
72
+ value.value = msg.body;
73
+ }
74
+ });
75
+
76
+ return value;
77
+ }
78
+
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Mycelia Plugin System - Vue Queue Helpers
3
+ *
4
+ * Vue composables for queue management.
5
+ */
6
+
7
+ import { ref, onMounted, onUnmounted } from 'vue';
8
+ import { useFacet } from './index.js';
9
+
10
+ /**
11
+ * useQueueStatus - Get queue status with reactive updates
12
+ *
13
+ * @returns {import('vue').Ref<Object>} Reactive ref to queue status object
14
+ * @returns {number} size - Current queue size
15
+ * @returns {number} capacity - Maximum queue capacity
16
+ * @returns {number} utilization - Utilization ratio (0-1)
17
+ * @returns {boolean} isFull - Whether queue is full
18
+ *
19
+ * @example
20
+ * ```js
21
+ * import { useQueueStatus } from 'mycelia-kernel-plugin/vue';
22
+ *
23
+ * export default {
24
+ * setup() {
25
+ * const status = useQueueStatus();
26
+ * return { status };
27
+ * }
28
+ * }
29
+ * ```
30
+ */
31
+ export function useQueueStatus() {
32
+ const queue = useFacet('queue');
33
+ const status = ref({
34
+ size: 0,
35
+ capacity: 0,
36
+ utilization: 0,
37
+ isFull: false
38
+ });
39
+
40
+ let intervalId = null;
41
+
42
+ const updateStatus = () => {
43
+ if (queue.value?.getQueueStatus) {
44
+ const newStatus = queue.value.getQueueStatus();
45
+ status.value = {
46
+ size: newStatus.size || 0,
47
+ capacity: newStatus.maxSize || 0,
48
+ utilization: newStatus.utilization || 0,
49
+ isFull: newStatus.isFull || false
50
+ };
51
+ }
52
+ };
53
+
54
+ onMounted(() => {
55
+ updateStatus();
56
+ intervalId = setInterval(updateStatus, 100); // Poll every 100ms
57
+ });
58
+
59
+ onUnmounted(() => {
60
+ if (intervalId) {
61
+ clearInterval(intervalId);
62
+ }
63
+ });
64
+
65
+ return status;
66
+ }
67
+
68
+ /**
69
+ * useQueueDrain - Automatically drain queue on mount
70
+ *
71
+ * @param {Object} [options={}] - Options
72
+ * @param {number} [options.interval=100] - Polling interval in ms
73
+ * @param {Function} [options.onMessage] - Callback for each message: (msg, options) => void
74
+ *
75
+ * @example
76
+ * ```js
77
+ * import { useQueueDrain } from 'mycelia-kernel-plugin/vue';
78
+ *
79
+ * export default {
80
+ * setup() {
81
+ * useQueueDrain({
82
+ * interval: 50,
83
+ * onMessage: (msg) => console.log('Processed:', msg)
84
+ * });
85
+ * }
86
+ * }
87
+ * ```
88
+ */
89
+ export function useQueueDrain(options = {}) {
90
+ const { interval = 100, onMessage } = options;
91
+ const queue = useFacet('queue');
92
+ let processInterval = null;
93
+
94
+ onMounted(() => {
95
+ if (!queue.value || !queue.value.hasMessagesToProcess) return;
96
+
97
+ processInterval = setInterval(() => {
98
+ if (queue.value?.hasMessagesToProcess()) {
99
+ const next = queue.value.selectNextMessage();
100
+ if (next && onMessage) {
101
+ onMessage(next.msg, next.options);
102
+ }
103
+ }
104
+ }, interval);
105
+ });
106
+
107
+ onUnmounted(() => {
108
+ if (processInterval) {
109
+ clearInterval(processInterval);
110
+ }
111
+ });
112
+ }
113
+