Skip to content

Commit 91e41f1

Browse files
authored
fix: Unwrap Vue Proxies when possible (#2132)
1 parent 7166db8 commit 91e41f1

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

src/Environment.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,4 +802,32 @@ export class Environment {
802802
print(`Screen Size: ${window.screen.width}x${window.screen.height}`);
803803
}
804804
}
805+
806+
/**
807+
* Prepares the given object to be sent to workers. Web Frameworks like Vue might
808+
* create proxy objects for all objects used. This code handles the necessary unwrapping.
809+
* @internal
810+
* @target web
811+
*/
812+
public static prepareForPostMessage<T>(object: T): T {
813+
if (!object) {
814+
return object;
815+
}
816+
817+
// Vue toRaw:
818+
// https://github.com/vuejs/core/blob/e7381761cc7971c0d40ae0a0a72687a500fd8db3/packages/reactivity/src/reactive.ts#L378-L381
819+
820+
if (typeof object === 'object') {
821+
const unwrapped = (object as any).__v_raw;
822+
if (unwrapped) {
823+
return Environment.prepareForPostMessage(unwrapped);
824+
}
825+
}
826+
827+
// Solidjs unwrap: the symbol required to access the raw object is unfortunately hidden and we cannot unwrap it without importing
828+
// import { unwrap } from "solid-js/store"
829+
// alternative for users is to replace this method during runtime.
830+
831+
return object;
832+
}
805833
}

src/platform/javascript/AlphaSynthAudioWorkletOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
240240
public addSamples(f: Float32Array): void {
241241
this._worklet?.port.postMessage({
242242
cmd: AlphaSynthWorkerSynthOutput.CmdOutputAddSamples,
243-
samples: f
243+
samples: Environment.prepareForPostMessage(f)
244244
});
245245
}
246246

src/platform/javascript/AlphaSynthWebWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class AlphaSynthWebWorker {
178178
public onSoundFontLoadFailed(e: any): void {
179179
this._main.postMessage({
180180
cmd: 'alphaSynth.soundFontLoadFailed',
181-
error: this.serializeException(e)
181+
error: this.serializeException(Environment.prepareForPostMessage(e))
182182
});
183183
}
184184

@@ -212,7 +212,7 @@ export class AlphaSynthWebWorker {
212212
public onMidiLoadFailed(e: any): void {
213213
this._main.postMessage({
214214
cmd: 'alphaSynth.midiLoaded',
215-
error: this.serializeException(e)
215+
error: this.serializeException(Environment.prepareForPostMessage(e))
216216
});
217217
}
218218

src/platform/javascript/AlphaSynthWebWorkerApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class AlphaSynthWebWorkerApi implements IAlphaSynth {
113113
this._midiEventsPlayedFilter = value;
114114
this._synth.postMessage({
115115
cmd: 'alphaSynth.setMidiEventsPlayedFilter',
116-
value: value
116+
value: Environment.prepareForPostMessage(value)
117117
});
118118
}
119119

@@ -188,7 +188,7 @@ export class AlphaSynthWebWorkerApi implements IAlphaSynth {
188188
this._playbackRange = value;
189189
this._synth.postMessage({
190190
cmd: 'alphaSynth.setPlaybackRange',
191-
value: value
191+
value: Environment.prepareForPostMessage(value)
192192
});
193193
}
194194

@@ -264,14 +264,14 @@ export class AlphaSynthWebWorkerApi implements IAlphaSynth {
264264
public playOneTimeMidiFile(midi: MidiFile): void {
265265
this._synth.postMessage({
266266
cmd: 'alphaSynth.playOneTimeMidiFile',
267-
midi: JsonConverter.midiFileToJsObject(midi)
267+
midi: JsonConverter.midiFileToJsObject(Environment.prepareForPostMessage(midi))
268268
});
269269
}
270270

271271
public loadSoundFont(data: Uint8Array, append: boolean): void {
272272
this._synth.postMessage({
273273
cmd: 'alphaSynth.loadSoundFontBytes',
274-
data: data,
274+
data: Environment.prepareForPostMessage(data),
275275
append: append
276276
});
277277
}
@@ -285,14 +285,14 @@ export class AlphaSynthWebWorkerApi implements IAlphaSynth {
285285
public loadMidiFile(midi: MidiFile): void {
286286
this._synth.postMessage({
287287
cmd: 'alphaSynth.loadMidi',
288-
midi: JsonConverter.midiFileToJsObject(midi)
288+
midi: JsonConverter.midiFileToJsObject(Environment.prepareForPostMessage(midi))
289289
});
290290
}
291291

292292
public applyTranspositionPitches(transpositionPitches: Map<number, number>): void {
293293
this._synth.postMessage({
294294
cmd: 'alphaSynth.applyTranspositionPitches',
295-
transpositionPitches: JSON.stringify(Array.from(transpositionPitches.entries()))
295+
transpositionPitches: JSON.stringify(Array.from(Environment.prepareForPostMessage(transpositionPitches).entries()))
296296
});
297297
}
298298

src/platform/javascript/AlphaSynthWorkerSynthOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class AlphaSynthWorkerSynthOutput implements ISynthOutput {
6262
public addSamples(samples: Float32Array): void {
6363
this._worker.postMessage({
6464
cmd: 'alphaSynth.output.addSamples',
65-
samples: samples
65+
samples: Environment.prepareForPostMessage(samples)
6666
});
6767
}
6868

src/platform/javascript/AlphaTabWorkerScoreRenderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class AlphaTabWorkerScoreRenderer<T> implements IScoreRenderer {
4848
}
4949

5050
private serializeSettingsForWorker(settings: Settings): unknown {
51-
const jsObject = JsonConverter.settingsToJsObject(settings)!;
51+
const jsObject = JsonConverter.settingsToJsObject(Environment.prepareForPostMessage(settings))!;
5252
// cut out player settings, they are only needed on UI thread side
5353
jsObject.delete('player');
5454
return jsObject;
@@ -113,11 +113,11 @@ export class AlphaTabWorkerScoreRenderer<T> implements IScoreRenderer {
113113
}
114114

115115
public renderScore(score: Score | null, trackIndexes: number[] | null): void {
116-
const jsObject: unknown = score == null ? null : JsonConverter.scoreToJsObject(score);
116+
const jsObject: unknown = score == null ? null : JsonConverter.scoreToJsObject(Environment.prepareForPostMessage(score));
117117
this._worker.postMessage({
118118
cmd: 'alphaTab.renderScore',
119119
score: jsObject,
120-
trackIndexes: trackIndexes,
120+
trackIndexes: Environment.prepareForPostMessage(trackIndexes),
121121
fontSizes: FontSizes.FontSizeLookupTables
122122
});
123123
}

0 commit comments

Comments
 (0)