-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosc-command-queue.js
More file actions
143 lines (120 loc) · 3.71 KB
/
osc-command-queue.js
File metadata and controls
143 lines (120 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* OSC Command Queue System - Plan 2: Scheduling & Automation
*/
class OSCCommandQueue {
constructor() {
this.queue = [];
this.scheduledCommands = new Map(); // timestamp -> commands
this.patterns = new Map(); // patternId -> pattern definition
this.isRunning = false;
this.tickInterval = 10; // 10ms precision
}
// Add command to queue
queueCommand(command) {
const queueItem = {
id: this.generateId(),
...command,
queuedAt: Date.now(),
status: 'queued'
};
this.queue.push(queueItem);
return queueItem.id;
}
// Schedule command for future execution
scheduleCommand(command, executeAt) {
const timestamp = typeof executeAt === 'number' ? executeAt : Date.now() + executeAt;
if (!this.scheduledCommands.has(timestamp)) {
this.scheduledCommands.set(timestamp, []);
}
this.scheduledCommands.get(timestamp).push({
id: this.generateId(),
...command,
scheduledFor: timestamp
});
}
// Create repeating pattern
createPattern(patternId, commands, options = {}) {
const pattern = {
id: patternId,
commands,
interval: options.interval || 1000,
repeat: options.repeat || 'infinite',
currentLoop: 0,
isActive: false,
startTime: null
};
this.patterns.set(patternId, pattern);
return pattern;
}
// Start pattern execution
startPattern(patternId) {
const pattern = this.patterns.get(patternId);
if (!pattern) throw new Error(`Pattern ${patternId} not found`);
pattern.isActive = true;
pattern.startTime = Date.now();
pattern.currentLoop = 0;
this.schedulePatternCommands(pattern);
}
schedulePatternCommands(pattern) {
pattern.commands.forEach((command, index) => {
const executeAt = pattern.startTime + (index * (pattern.interval / pattern.commands.length));
this.scheduleCommand(command, executeAt);
});
// Schedule next loop if repeating
if (pattern.repeat === 'infinite' || pattern.currentLoop < pattern.repeat) {
const nextLoopTime = pattern.startTime + ((pattern.currentLoop + 1) * pattern.interval);
setTimeout(() => {
if (pattern.isActive) {
pattern.currentLoop++;
pattern.startTime = nextLoopTime;
this.schedulePatternCommands(pattern);
}
}, pattern.interval);
}
}
// Process queue and scheduled commands
async tick() {
const now = Date.now();
// Process immediate queue
const readyCommands = this.queue.filter(cmd =>
cmd.status === 'queued' && (cmd.executeAt || 0) <= now
);
for (const command of readyCommands) {
await this.executeCommand(command);
command.status = 'executed';
command.executedAt = now;
}
// Clean up executed commands
this.queue = this.queue.filter(cmd => cmd.status !== 'executed');
// Process scheduled commands
const scheduledTimes = Array.from(this.scheduledCommands.keys())
.filter(time => time <= now);
for (const time of scheduledTimes) {
const commands = this.scheduledCommands.get(time);
for (const command of commands) {
await this.executeCommand(command);
}
this.scheduledCommands.delete(time);
}
}
// Start processing loop
start() {
if (this.isRunning) return;
this.isRunning = true;
this.processLoop = setInterval(() => {
this.tick();
}, this.tickInterval);
}
// Stop processing
stop() {
if (this.processLoop) {
clearInterval(this.processLoop);
this.processLoop = null;
}
this.isRunning = false;
// Stop all patterns
this.patterns.forEach(pattern => {
pattern.isActive = false;
});
}
}