forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogoDependencies.js
More file actions
271 lines (253 loc) · 10.6 KB
/
LogoDependencies.js
File metadata and controls
271 lines (253 loc) · 10.6 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2026 Music Blocks contributors
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/**
* @file LogoDependencies.js
* @description Explicit dependency container for the Logo execution engine.
*
* This class manages Logo's explicit dependencies.
*/
/**
* @class
* @classdesc Container for Logo's explicit dependencies.
*/
class LogoDependencies {
/**
* @constructor
* @param {Object} deps - Dependency configuration object
* @param {Object} deps.blocks - Blocks subsystem for program structure
* @param {Array} deps.blocks.blockList - List of all blocks
* @param {Function} deps.blocks.unhighlightAll - Unhighlight all blocks
* @param {Function} deps.blocks.bringToTop - Bring blocks to top z-index
* @param {Function} deps.blocks.showBlocks - Show blocks UI
* @param {Function} deps.blocks.hideBlocks - Hide blocks UI
* @param {Function} deps.blocks.sameGeneration - Check block relationships
* @param {boolean} deps.blocks.visible - Whether blocks are visible
*
* @param {Object} deps.turtles - Turtles subsystem for command execution
* @param {Array} deps.turtles.turtleList - List of all turtles
* @param {Function} deps.turtles.ithTurtle - Get turtle by index
* @param {Function} deps.turtles.getTurtle - Get turtle by ID
* @param {Function} deps.turtles.getTurtleCount - Get total turtle count
* @param {Function} deps.turtles.add - Add a new turtle
* @param {Function} deps.turtles.markAllAsStopped - Mark all turtles as stopped
*
* @param {Object} deps.stage - Stage/canvas for event handling
* @param {Function} deps.stage.addEventListener - Add event listener
* @param {Function} deps.stage.removeEventListener - Remove event listener
*
* @param {Function} deps.errorHandler - Function to display error messages
* @param {string} deps.errorHandler.msg - Error message to display
*
* @param {Object} deps.messageHandler - Message display handler
* @param {Function} deps.messageHandler.hide - Hide all messages
*
* @param {Object} deps.storage - Persistence handler
* @param {Function} deps.storage.saveLocally - Save state locally
*
* @param {Object} deps.config - Configuration object
* @param {boolean} deps.config.showBlocksAfterRun - Whether to show blocks after run
*
* @param {Object} deps.callbacks - Lifecycle callbacks
* @param {Function} deps.callbacks.onStopTurtle - Called when turtle stops
* @param {Function} deps.callbacks.onRunTurtle - Called when turtle runs
*
*/
constructor({
blocks,
turtles,
stage,
errorHandler,
messageHandler,
storage,
config,
callbacks,
instruments = null,
instrumentsFilters = null,
instrumentsEffects = null,
widgetWindows = null,
utils = null,
Singer = null,
Tone = null,
classes = null
}) {
// Validate required dependencies
if (!blocks) {
throw new Error("LogoDependencies: 'blocks' is required");
}
if (!turtles) {
throw new Error("LogoDependencies: 'turtles' is required");
}
if (!stage) {
throw new Error("LogoDependencies: 'stage' is required");
}
if (!errorHandler) {
throw new Error("LogoDependencies: 'errorHandler' is required");
}
/**
* Blocks subsystem - manages program structure and visual feedback
* @type {Object}
*/
this.blocks = blocks;
/**
* Turtles subsystem - manages turtle state and execution
* @type {Object}
*/
this.turtles = turtles;
/**
* Stage - canvas/event bus for interaction
* @type {Object}
*/
this.stage = stage;
/**
* Error handler - displays error messages to user
* @type {Function}
*/
this.errorHandler = errorHandler;
/**
* Message handler - manages UI messages
* @type {Object}
*/
this.messageHandler = messageHandler || { hide: () => {} };
/**
* Storage - handles persistence
* @type {Object}
*/
this.storage = storage || { saveLocally: () => {} };
/**
* Configuration - runtime settings
* @type {Object}
*/
this.config = config || { showBlocksAfterRun: false };
/**
* Callbacks - lifecycle event handlers
* @type {Object}
*/
this.callbacks = callbacks || { onStopTurtle: null, onRunTurtle: null };
// Audio and utility dependencies
this.instruments =
instruments || (typeof window !== "undefined" ? window.instruments : null);
this.instrumentsFilters =
instrumentsFilters ||
(typeof window !== "undefined" ? window.instrumentsFilters : null);
this.instrumentsEffects =
instrumentsEffects ||
(typeof window !== "undefined" ? window.instrumentsEffects : null);
this.widgetWindows =
widgetWindows || (typeof window !== "undefined" ? window.widgetWindows : null);
this.Singer = Singer || (typeof window !== "undefined" ? window.Singer : null);
this.Tone = Tone || (typeof window !== "undefined" ? window.Tone : null);
this.utils = utils || {
doUseCamera: typeof doUseCamera !== "undefined" ? doUseCamera : null,
doStopVideoCam: typeof doStopVideoCam !== "undefined" ? doStopVideoCam : null,
getIntervalDirection:
typeof getIntervalDirection !== "undefined" ? getIntervalDirection : null,
getIntervalNumber: typeof getIntervalNumber !== "undefined" ? getIntervalNumber : null,
mixedNumber: typeof mixedNumber !== "undefined" ? mixedNumber : null,
rationalToFraction:
typeof rationalToFraction !== "undefined" ? rationalToFraction : null,
getStatsFromNotation:
typeof getStatsFromNotation !== "undefined" ? getStatsFromNotation : null,
delayExecution: typeof delayExecution !== "undefined" ? delayExecution : null,
last: typeof last !== "undefined" ? last : null
};
this.classes = classes || {
Notation: typeof Notation !== "undefined" ? Notation : null,
Synth: typeof Synth !== "undefined" ? Synth : null,
StatusMatrix: typeof StatusMatrix !== "undefined" ? StatusMatrix : null
};
}
/**
* Factory method to create LogoDependencies from an Activity object.
* This provides backward compatibility with the existing architecture.
*
* @param {Object} activity - Activity object (old pattern)
* @returns {LogoDependencies} Dependency container
*
* @example
* const deps = LogoDependencies.fromActivity(activity);
* const logo = new Logo(deps);
*/
static fromActivity(activity) {
return new LogoDependencies({
blocks: activity.blocks,
turtles: activity.turtles,
stage: activity.stage,
errorHandler: msg => activity.errorMsg(msg),
messageHandler: {
hide: () => activity.hideMsgs()
},
storage: {
saveLocally: () => activity.saveLocally()
},
config: {
get showBlocksAfterRun() {
return activity.showBlocksAfterRun;
},
set showBlocksAfterRun(value) {
activity.showBlocksAfterRun = value;
}
},
callbacks: {
onStopTurtle: activity.onStopTurtle,
onRunTurtle: activity.onRunTurtle
},
// Pass globals for backward compatibility during migration
instruments: typeof instruments !== "undefined" ? instruments : null,
instrumentsFilters:
typeof instrumentsFilters !== "undefined" ? instrumentsFilters : null,
instrumentsEffects:
typeof instrumentsEffects !== "undefined" ? instrumentsEffects : null,
widgetWindows: typeof window !== "undefined" ? window.widgetWindows : null,
Singer: typeof Singer !== "undefined" ? Singer : null,
Tone: typeof Tone !== "undefined" ? Tone : null,
utils: {
doUseCamera: typeof doUseCamera !== "undefined" ? doUseCamera : null,
doStopVideoCam: typeof doStopVideoCam !== "undefined" ? doStopVideoCam : null,
getIntervalDirection:
typeof getIntervalDirection !== "undefined" ? getIntervalDirection : null,
getIntervalNumber:
typeof getIntervalNumber !== "undefined" ? getIntervalNumber : null,
mixedNumber: typeof mixedNumber !== "undefined" ? mixedNumber : null,
rationalToFraction:
typeof rationalToFraction !== "undefined" ? rationalToFraction : null,
getStatsFromNotation:
typeof getStatsFromNotation !== "undefined" ? getStatsFromNotation : null,
delayExecution: typeof delayExecution !== "undefined" ? delayExecution : null,
last: typeof last !== "undefined" ? last : null
},
classes: {
Notation: typeof Notation !== "undefined" ? Notation : null,
Synth: typeof Synth !== "undefined" ? Synth : null,
StatusMatrix: typeof StatusMatrix !== "undefined" ? StatusMatrix : null
}
});
}
/**
* Check if this is a valid LogoDependencies instance.
* Used to distinguish between old Activity pattern and new dependency pattern.
*
* @param {*} obj - Object to check
* @returns {boolean} True if obj is a LogoDependencies instance
*/
static isLogoDependencies(obj) {
return obj instanceof LogoDependencies;
}
}
// Export for RequireJS/AMD
if (typeof define === "function" && define.amd) {
define([], function () {
return LogoDependencies;
});
}
// Export for Node.js/CommonJS (for testing)
if (typeof module !== "undefined" && module.exports) {
module.exports = LogoDependencies;
}