Skip to content

Instantly share code, notes, and snippets.

@pofulu
Last active March 28, 2022 04:46
Show Gist options
  • Save pofulu/f4bd136199e77332b6e265cf4b7cd7bb to your computer and use it in GitHub Desktop.
Save pofulu/f4bd136199e77332b6e265cf4b7cd7bb to your computer and use it in GitHub Desktop.
用於在 Spark AR 中等待任意 Signal 的數值變化
function pinInitValues<T extends readonly string[]>(signal: ISignal, values: T): Promise<{ [K in T[number]]: number }> {
const signals = values.reduce((pre, cur) => {
pre[cur] = signal[cur];
return pre;
}, {})
return new Promise(resolve =>
Reactive.monitorMany(signals, { fireOnInitialValue: true }).select('newValues').take(1).subscribe(resolve)
);
}
import Scene from 'Scene';
import Diagnostics from 'Diagnostics';
void async function main() {
const plane0 = await Scene.root.findFirst('plane0');
plane0.transform.scale = Reactive.vector(3, 3, 3);
const scaleBeforeUpdated = plane0.transform.scale.pinLastValue();
Diagnostics.log(scaleBeforeUpdated.x.pinLastValue()); // 1
Diagnostics.log(scaleBeforeUpdated.y.pinLastValue()); // 1
Diagnostics.log(scaleBeforeUpdated.z.pinLastValue()); // 1
const scaleAfterUpdated = await pinInitValues(plane0.transform.scale, ['x', 'y', 'z'] as const)
// The `as const` make code hinting work
Diagnostics.log(scaleAfterUpdated); // {x:3, y:3, z:3}
Diagnostics.log(scaleAfterUpdated.x); // 3
Diagnostics.log(scaleAfterUpdated.y); // 3
Diagnostics.log(scaleAfterUpdated.z); // 3
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment