@yunlefun-home/air-conditioner-core 0.4.2 → 0.4.4
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.
- package/README.md +1 -1
- package/dist/index.cjs +207 -68
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +207 -68
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -168,6 +168,7 @@ function resolveAcAudioAssets(baseUrl = "/assets/audio", ext = "m4a") {
|
|
|
168
168
|
const readyTimeoutMs = 1500;
|
|
169
169
|
const fallbackTweenStepMs = 100;
|
|
170
170
|
const curveLength = 64;
|
|
171
|
+
const defaultAudioCache = /* @__PURE__ */ new Map();
|
|
171
172
|
function sourceUrl(element) {
|
|
172
173
|
return element?.currentSrc || element?.src || "";
|
|
173
174
|
}
|
|
@@ -179,7 +180,12 @@ async function fetchAudioData(url) {
|
|
|
179
180
|
function createDefaultAudioContext() {
|
|
180
181
|
const scope = globalThis;
|
|
181
182
|
const AudioContextConstructor = scope.AudioContext ?? scope.webkitAudioContext;
|
|
182
|
-
|
|
183
|
+
if (!AudioContextConstructor) return null;
|
|
184
|
+
try {
|
|
185
|
+
return new AudioContextConstructor({ latencyHint: "interactive" });
|
|
186
|
+
} catch {
|
|
187
|
+
return new AudioContextConstructor();
|
|
188
|
+
}
|
|
183
189
|
}
|
|
184
190
|
/**
|
|
185
191
|
* 合成一声很轻的「嗒」:音高快速下滑的正弦波,短促起音、指数衰减,像水珠落在窗台上。
|
|
@@ -201,6 +207,32 @@ function playDrip(context, output) {
|
|
|
201
207
|
oscillator.onended = () => gain.disconnect();
|
|
202
208
|
}
|
|
203
209
|
/**
|
|
210
|
+
* 样本还没解码出来时的按键声:很短的一声「嘀」,不依赖文件。
|
|
211
|
+
* 每次调用都是新的振荡器,连按不会互相等待。
|
|
212
|
+
*/
|
|
213
|
+
function playClick(context, output) {
|
|
214
|
+
if (typeof context.createOscillator !== "function") return false;
|
|
215
|
+
try {
|
|
216
|
+
const start = context.currentTime;
|
|
217
|
+
const oscillator = context.createOscillator();
|
|
218
|
+
const gain = context.createGain();
|
|
219
|
+
oscillator.type = "sine";
|
|
220
|
+
oscillator.frequency.setValueAtTime(2400, start);
|
|
221
|
+
oscillator.frequency.exponentialRampToValueAtTime(1400, start + .02);
|
|
222
|
+
gain.gain.setValueAtTime(1e-4, start);
|
|
223
|
+
gain.gain.exponentialRampToValueAtTime(.15, start + .002);
|
|
224
|
+
gain.gain.exponentialRampToValueAtTime(1e-4, start + .045);
|
|
225
|
+
oscillator.connect(gain);
|
|
226
|
+
gain.connect(output);
|
|
227
|
+
oscillator.start(start);
|
|
228
|
+
oscillator.stop(start + .05);
|
|
229
|
+
oscillator.onended = () => gain.disconnect();
|
|
230
|
+
return true;
|
|
231
|
+
} catch {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
204
236
|
* 等功率淡入 / 淡出曲线,噪声类声音交叉淡化时中段不会变小。
|
|
205
237
|
*/
|
|
206
238
|
function fadeCurve(direction) {
|
|
@@ -219,6 +251,20 @@ function playElement(element) {
|
|
|
219
251
|
element.load();
|
|
220
252
|
Promise.resolve(element.play()).catch(() => {});
|
|
221
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* 重播一段已经挂在元素上的短音效。
|
|
256
|
+
* `load()` 会丢掉预加载的数据并重新选资源,按键「嘀」声因此会晚一拍。
|
|
257
|
+
*/
|
|
258
|
+
function replayElement(element) {
|
|
259
|
+
if (!element) return;
|
|
260
|
+
try {
|
|
261
|
+
if (element.currentTime > 0) {
|
|
262
|
+
element.pause?.();
|
|
263
|
+
element.currentTime = 0;
|
|
264
|
+
}
|
|
265
|
+
} catch {}
|
|
266
|
+
Promise.resolve(element.play()).catch(() => {});
|
|
267
|
+
}
|
|
222
268
|
function stopElement(element) {
|
|
223
269
|
if (!element) return;
|
|
224
270
|
if (element.pause) element.pause();
|
|
@@ -231,7 +277,8 @@ function stopElement(element) {
|
|
|
231
277
|
* 风声变小、并不定时随压缩机发力起伏。环境不支持或音频无法获取时退回
|
|
232
278
|
* `<audio>` 元素。
|
|
233
279
|
*
|
|
234
|
-
*
|
|
280
|
+
* 创建控制器时就会开始下载并解码,此时不会出声。浏览器要等用户手势里
|
|
281
|
+
* `resume()` 之后才允许播放,因此第一次 `beep()` / 开关机仍需发生在点击回调中。
|
|
235
282
|
*
|
|
236
283
|
* @param elements 三个音频元素;Web Audio 会从它们的 `src` 获取音频
|
|
237
284
|
* @param options 声音模式与可替换的环境依赖
|
|
@@ -250,6 +297,10 @@ function createAcAudioController(elements, options = {}) {
|
|
|
250
297
|
/** 每次开关机自增,丢弃过期的异步回调 */
|
|
251
298
|
let session = 0;
|
|
252
299
|
const timers = /* @__PURE__ */ new Set();
|
|
300
|
+
/** 同一地址只拉取一次;decodeAudioData 会拆掉传入的 buffer,所以每次给它一份拷贝 */
|
|
301
|
+
const audioCache = options.loadAudioData ? /* @__PURE__ */ new Map() : defaultAudioCache;
|
|
302
|
+
/** 第一次按下时提前 resume,等 click 里真正 beep 时设备已经醒了 */
|
|
303
|
+
let stopUnlock;
|
|
253
304
|
/** `<audio>` 播放时的风声相对音量 */
|
|
254
305
|
let fallbackFanLevel = 1;
|
|
255
306
|
let fallbackTween;
|
|
@@ -294,44 +345,81 @@ function createAcAudioController(elements, options = {}) {
|
|
|
294
345
|
function ensureEngine() {
|
|
295
346
|
if (engine !== void 0) return engine;
|
|
296
347
|
engine = createEngine();
|
|
348
|
+
if (engine) stopUnlock = armGestureUnlock();
|
|
297
349
|
return engine;
|
|
298
350
|
}
|
|
351
|
+
/** 手势里唤醒音频设备。已经在跑时什么都不做。 */
|
|
352
|
+
function unlockPlayback() {
|
|
353
|
+
const web = ensureEngine();
|
|
354
|
+
if (web) resume(web);
|
|
355
|
+
}
|
|
356
|
+
function loadCached(url) {
|
|
357
|
+
let pending = audioCache.get(url);
|
|
358
|
+
if (!pending) {
|
|
359
|
+
const load = options.loadAudioData ?? fetchAudioData;
|
|
360
|
+
pending = new Promise((resolve, reject) => {
|
|
361
|
+
try {
|
|
362
|
+
resolve(load(url));
|
|
363
|
+
} catch (error) {
|
|
364
|
+
reject(error);
|
|
365
|
+
}
|
|
366
|
+
}).catch((error) => {
|
|
367
|
+
audioCache.delete(url);
|
|
368
|
+
throw error;
|
|
369
|
+
});
|
|
370
|
+
audioCache.set(url, pending);
|
|
371
|
+
}
|
|
372
|
+
return pending.then((data) => data.slice(0));
|
|
373
|
+
}
|
|
299
374
|
function createEngine() {
|
|
300
375
|
if (options.webAudio === false) return null;
|
|
301
|
-
let context;
|
|
302
376
|
try {
|
|
303
|
-
context = (options.createAudioContext ?? createDefaultAudioContext)();
|
|
377
|
+
const context = (options.createAudioContext ?? createDefaultAudioContext)();
|
|
378
|
+
if (!context) return null;
|
|
379
|
+
const master = context.createGain();
|
|
380
|
+
master.gain.value = volume();
|
|
381
|
+
master.connect(context.destination);
|
|
382
|
+
const buffers = {};
|
|
383
|
+
const decode = async (name) => {
|
|
384
|
+
const url = sourceUrl(elements[name]);
|
|
385
|
+
if (!url || buffers[name]) return;
|
|
386
|
+
try {
|
|
387
|
+
buffers[name] = await context.decodeAudioData(await loadCached(url));
|
|
388
|
+
} catch {}
|
|
389
|
+
};
|
|
390
|
+
return {
|
|
391
|
+
context,
|
|
392
|
+
master,
|
|
393
|
+
buffers,
|
|
394
|
+
ready: Promise.all([
|
|
395
|
+
"beep",
|
|
396
|
+
"start",
|
|
397
|
+
"work"
|
|
398
|
+
].map(decode)).then(() => Boolean(buffers.start && buffers.work))
|
|
399
|
+
};
|
|
304
400
|
} catch {
|
|
305
401
|
return null;
|
|
306
402
|
}
|
|
307
|
-
if (!context) return null;
|
|
308
|
-
const master = context.createGain();
|
|
309
|
-
master.gain.value = volume();
|
|
310
|
-
master.connect(context.destination);
|
|
311
|
-
const buffers = {};
|
|
312
|
-
const load = options.loadAudioData ?? fetchAudioData;
|
|
313
|
-
const decode = async (name) => {
|
|
314
|
-
const url = sourceUrl(elements[name]);
|
|
315
|
-
if (!url) return;
|
|
316
|
-
try {
|
|
317
|
-
buffers[name] = await context.decodeAudioData(await load(url));
|
|
318
|
-
} catch {}
|
|
319
|
-
};
|
|
320
|
-
const ready = Promise.all([
|
|
321
|
-
"beep",
|
|
322
|
-
"start",
|
|
323
|
-
"work"
|
|
324
|
-
].map(decode)).then(() => Boolean(buffers.start && buffers.work));
|
|
325
|
-
return {
|
|
326
|
-
context,
|
|
327
|
-
master,
|
|
328
|
-
buffers,
|
|
329
|
-
ready
|
|
330
|
-
};
|
|
331
403
|
}
|
|
332
404
|
function resume(web) {
|
|
333
405
|
if (web.context.state === "suspended") Promise.resolve(web.context.resume()).catch(() => {});
|
|
334
406
|
}
|
|
407
|
+
/**
|
|
408
|
+
* pointerdown / keydown 比 click 更早,也比温度键的按下回调更早(捕获阶段)。
|
|
409
|
+
* 监听一直留着:手机切走再回来时上下文会重新被挂起。
|
|
410
|
+
*/
|
|
411
|
+
function armGestureUnlock() {
|
|
412
|
+
if (typeof document === "undefined") return () => {};
|
|
413
|
+
const onGesture = () => {
|
|
414
|
+
unlockPlayback();
|
|
415
|
+
};
|
|
416
|
+
document.addEventListener("pointerdown", onGesture, true);
|
|
417
|
+
document.addEventListener("keydown", onGesture, true);
|
|
418
|
+
return () => {
|
|
419
|
+
document.removeEventListener("pointerdown", onGesture, true);
|
|
420
|
+
document.removeEventListener("keydown", onGesture, true);
|
|
421
|
+
};
|
|
422
|
+
}
|
|
335
423
|
function waitReady(web) {
|
|
336
424
|
return Promise.race([web.ready, new Promise((resolve) => setTimeout(resolve, readyTimeoutMs, false))]);
|
|
337
425
|
}
|
|
@@ -408,7 +496,7 @@ function createAcAudioController(elements, options = {}) {
|
|
|
408
496
|
quiet();
|
|
409
497
|
}, soundTimings.settleAfterMs);
|
|
410
498
|
}
|
|
411
|
-
function startWeb(web) {
|
|
499
|
+
function startWeb(web, withStart = true) {
|
|
412
500
|
const output = web.context.createGain();
|
|
413
501
|
output.connect(web.master);
|
|
414
502
|
const fan = web.context.createGain();
|
|
@@ -421,9 +509,14 @@ function createAcAudioController(elements, options = {}) {
|
|
|
421
509
|
};
|
|
422
510
|
run = target;
|
|
423
511
|
const now = web.context.currentTime;
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
512
|
+
if (withStart) {
|
|
513
|
+
track(target, playBuffer(web, web.buffers.start, output, now));
|
|
514
|
+
loopFan(web, target, now + soundTimings.startDelayMs / 1e3);
|
|
515
|
+
later(startFanCycle, soundTimings.startDelayMs);
|
|
516
|
+
} else {
|
|
517
|
+
loopFan(web, target, now);
|
|
518
|
+
startFanCycle();
|
|
519
|
+
}
|
|
427
520
|
}
|
|
428
521
|
function stopWeb(fadeMs) {
|
|
429
522
|
const target = run;
|
|
@@ -442,12 +535,10 @@ function createAcAudioController(elements, options = {}) {
|
|
|
442
535
|
});
|
|
443
536
|
setTimeout(() => target.output.disconnect(), fadeMs + 100);
|
|
444
537
|
}
|
|
445
|
-
function startFallback() {
|
|
538
|
+
function startFallback(withStart = true) {
|
|
446
539
|
const { start, work } = elements;
|
|
447
540
|
stopElement(work);
|
|
448
|
-
|
|
449
|
-
playElement(start);
|
|
450
|
-
later(() => {
|
|
541
|
+
const beginWork = () => {
|
|
451
542
|
if (!work) return;
|
|
452
543
|
fallbackFanLevel = 0;
|
|
453
544
|
applyElementVolume(work, 0);
|
|
@@ -455,14 +546,21 @@ function createAcAudioController(elements, options = {}) {
|
|
|
455
546
|
playElement(work);
|
|
456
547
|
tweenFallbackFan(initialFanLevel(), soundTimings.loopCrossfadeMs);
|
|
457
548
|
startFanCycle();
|
|
458
|
-
}
|
|
549
|
+
};
|
|
550
|
+
if (!withStart) {
|
|
551
|
+
beginWork();
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
applyElementVolume(start);
|
|
555
|
+
playElement(start);
|
|
556
|
+
later(beginWork, soundTimings.startDelayMs);
|
|
459
557
|
}
|
|
460
558
|
function stopFallback(fadeMs) {
|
|
461
559
|
stopElement(elements.start);
|
|
462
560
|
if (fadeMs > 0) tweenFallbackFan(0, fadeMs, () => stopElement(elements.work));
|
|
463
561
|
else stopElement(elements.work);
|
|
464
562
|
}
|
|
465
|
-
function powerOn() {
|
|
563
|
+
function powerOn(withStart = true) {
|
|
466
564
|
const id = ++session;
|
|
467
565
|
fanRunning = false;
|
|
468
566
|
fanCycle++;
|
|
@@ -472,14 +570,14 @@ function createAcAudioController(elements, options = {}) {
|
|
|
472
570
|
stopFallback(0);
|
|
473
571
|
const web = ensureEngine();
|
|
474
572
|
if (!web) {
|
|
475
|
-
startFallback();
|
|
573
|
+
startFallback(withStart);
|
|
476
574
|
return;
|
|
477
575
|
}
|
|
478
576
|
resume(web);
|
|
479
577
|
waitReady(web).then((ok) => {
|
|
480
578
|
if (id !== session) return;
|
|
481
|
-
if (ok) startWeb(web);
|
|
482
|
-
else startFallback();
|
|
579
|
+
if (ok) startWeb(web, withStart);
|
|
580
|
+
else startFallback(withStart);
|
|
483
581
|
});
|
|
484
582
|
}
|
|
485
583
|
function powerOff() {
|
|
@@ -490,31 +588,43 @@ function createAcAudioController(elements, options = {}) {
|
|
|
490
588
|
stopWeb(soundTimings.stopFadeMs);
|
|
491
589
|
stopFallback(soundTimings.stopFadeMs);
|
|
492
590
|
}
|
|
591
|
+
if (options.webAudio !== false) ensureEngine();
|
|
493
592
|
return {
|
|
593
|
+
unlock() {
|
|
594
|
+
unlockPlayback();
|
|
595
|
+
},
|
|
494
596
|
beep() {
|
|
495
597
|
if (soundMode === "mute") return;
|
|
496
598
|
const web = ensureEngine();
|
|
497
599
|
if (web) {
|
|
498
600
|
resume(web);
|
|
499
601
|
if (web.buffers.beep) {
|
|
500
|
-
playBuffer(web, web.buffers.beep, web.master, web.context.currentTime);
|
|
602
|
+
const source = playBuffer(web, web.buffers.beep, web.master, web.context.currentTime);
|
|
603
|
+
source.onended = () => {
|
|
604
|
+
try {
|
|
605
|
+
source.disconnect();
|
|
606
|
+
} catch {}
|
|
607
|
+
};
|
|
501
608
|
return;
|
|
502
609
|
}
|
|
610
|
+
if (playClick(web.context, web.master)) return;
|
|
503
611
|
}
|
|
504
612
|
applyElementVolume(elements.beep);
|
|
505
|
-
|
|
613
|
+
replayElement(elements.beep);
|
|
506
614
|
},
|
|
507
615
|
drip() {
|
|
508
616
|
if (soundMode !== "normal") return;
|
|
509
617
|
const web = ensureEngine();
|
|
510
|
-
if (!web || typeof web.context.createOscillator !== "function") return;
|
|
511
|
-
resume(web);
|
|
618
|
+
if (!web || web.context.state !== "running" || typeof web.context.createOscillator !== "function") return;
|
|
512
619
|
playDrip(web.context, web.master);
|
|
513
620
|
},
|
|
514
621
|
toggleStatusAudio(previousStatus) {
|
|
515
622
|
if (previousStatus) powerOff();
|
|
516
623
|
else powerOn();
|
|
517
624
|
},
|
|
625
|
+
resumeFan() {
|
|
626
|
+
powerOn(false);
|
|
627
|
+
},
|
|
518
628
|
setSoundMode(mode) {
|
|
519
629
|
soundMode = mode;
|
|
520
630
|
if (engine) engine.master.gain.setTargetAtTime(volume(), engine.context.currentTime, .08);
|
|
@@ -543,6 +653,8 @@ function createAcAudioController(elements, options = {}) {
|
|
|
543
653
|
cancelFallbackTween();
|
|
544
654
|
stopWeb(0);
|
|
545
655
|
stopFallback(0);
|
|
656
|
+
stopUnlock?.();
|
|
657
|
+
stopUnlock = void 0;
|
|
546
658
|
if (engine && !options.createAudioContext) Promise.resolve(engine.context.close()).catch(() => {});
|
|
547
659
|
engine = null;
|
|
548
660
|
}
|
|
@@ -718,7 +830,7 @@ function observeAcScale(target, designWidth, onScale) {
|
|
|
718
830
|
onScale(scale);
|
|
719
831
|
}
|
|
720
832
|
};
|
|
721
|
-
update(target.getBoundingClientRect().width);
|
|
833
|
+
update(target.offsetWidth ?? target.getBoundingClientRect().width);
|
|
722
834
|
const observer = new ResizeObserver((entries) => {
|
|
723
835
|
for (const entry of entries) update(entry.contentRect.width);
|
|
724
836
|
});
|
|
@@ -740,23 +852,38 @@ function observeAcTilt(target, onTilt, maxTilt = 4) {
|
|
|
740
852
|
const matchMedia = globalThis.matchMedia;
|
|
741
853
|
if (typeof matchMedia !== "function" || !matchMedia(finePointerQuery).matches || matchMedia("(prefers-reduced-motion: reduce)").matches) return () => {};
|
|
742
854
|
let frame = 0;
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
855
|
+
let pendingX = 0;
|
|
856
|
+
let pendingY = 0;
|
|
857
|
+
let tilted = false;
|
|
858
|
+
const apply = (rotateX, rotateY) => {
|
|
859
|
+
tilted = rotateX !== 0 || rotateY !== 0;
|
|
860
|
+
onTilt(rotateX, rotateY);
|
|
746
861
|
};
|
|
747
862
|
const onMove = (event) => {
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
863
|
+
pendingX = event.clientX;
|
|
864
|
+
pendingY = event.clientY;
|
|
865
|
+
if (frame) return;
|
|
866
|
+
frame = requestAnimationFrame(() => {
|
|
867
|
+
frame = 0;
|
|
868
|
+
const rect = target.getBoundingClientRect();
|
|
869
|
+
if (!rect.width || !rect.height) return;
|
|
870
|
+
const x = (pendingX - rect.left) / rect.width - .5;
|
|
871
|
+
const y = (pendingY - rect.top) / rect.height - .5;
|
|
872
|
+
const round = (value) => Math.round(value * 100) / 100;
|
|
873
|
+
apply(round(-y * 2 * maxTilt), round(x * 2 * maxTilt));
|
|
874
|
+
});
|
|
754
875
|
};
|
|
755
|
-
const onLeave = () =>
|
|
756
|
-
|
|
757
|
-
|
|
876
|
+
const onLeave = () => {
|
|
877
|
+
if (frame) {
|
|
878
|
+
cancelAnimationFrame(frame);
|
|
879
|
+
frame = 0;
|
|
880
|
+
}
|
|
881
|
+
if (tilted) apply(0, 0);
|
|
882
|
+
};
|
|
883
|
+
target.addEventListener("pointermove", onMove, { passive: true });
|
|
884
|
+
target.addEventListener("pointerleave", onLeave, { passive: true });
|
|
758
885
|
return () => {
|
|
759
|
-
|
|
886
|
+
onLeave();
|
|
760
887
|
target.removeEventListener("pointermove", onMove);
|
|
761
888
|
target.removeEventListener("pointerleave", onLeave);
|
|
762
889
|
};
|
|
@@ -840,27 +967,34 @@ function bindAcPressRepeat(element, fire, options = {}) {
|
|
|
840
967
|
let timer;
|
|
841
968
|
let wait = interval;
|
|
842
969
|
let last;
|
|
970
|
+
let disposed = false;
|
|
971
|
+
let generation = 0;
|
|
843
972
|
const stop = () => {
|
|
973
|
+
generation++;
|
|
844
974
|
clearTimeout(timer);
|
|
845
975
|
timer = void 0;
|
|
846
976
|
};
|
|
847
|
-
const tick = () => {
|
|
977
|
+
const tick = (press) => {
|
|
978
|
+
if (disposed || press !== generation) return;
|
|
848
979
|
if (value && value() === last) return stop();
|
|
849
980
|
last = value?.();
|
|
850
981
|
fire();
|
|
982
|
+
if (disposed || press !== generation) return;
|
|
851
983
|
wait = Math.max(minInterval, wait * .85);
|
|
852
|
-
timer = setTimeout(tick, wait);
|
|
984
|
+
timer = setTimeout(tick, wait, press);
|
|
853
985
|
};
|
|
854
986
|
const onPointerDown = (event) => {
|
|
855
|
-
if (event.button !== 0) return;
|
|
987
|
+
if (disposed || event.button !== 0) return;
|
|
856
988
|
stop();
|
|
989
|
+
const press = generation;
|
|
857
990
|
last = value?.();
|
|
858
991
|
fire();
|
|
992
|
+
if (disposed || press !== generation) return;
|
|
859
993
|
wait = interval;
|
|
860
|
-
timer = setTimeout(tick, delay);
|
|
994
|
+
timer = setTimeout(tick, delay, press);
|
|
861
995
|
};
|
|
862
996
|
const onClick = (event) => {
|
|
863
|
-
if (event.detail === 0) fire();
|
|
997
|
+
if (!disposed && event.detail === 0) fire();
|
|
864
998
|
};
|
|
865
999
|
const onContextMenu = (event) => event.preventDefault();
|
|
866
1000
|
element.addEventListener("pointerdown", onPointerDown);
|
|
@@ -870,6 +1004,7 @@ function bindAcPressRepeat(element, fire, options = {}) {
|
|
|
870
1004
|
element.addEventListener("click", onClick);
|
|
871
1005
|
element.addEventListener("contextmenu", onContextMenu);
|
|
872
1006
|
return () => {
|
|
1007
|
+
disposed = true;
|
|
873
1008
|
stop();
|
|
874
1009
|
element.removeEventListener("pointerdown", onPointerDown);
|
|
875
1010
|
element.removeEventListener("pointerup", stop);
|
|
@@ -1146,7 +1281,7 @@ function createAirConditionerStore(options = {}) {
|
|
|
1146
1281
|
...persistedState,
|
|
1147
1282
|
...options.initialState
|
|
1148
1283
|
}, limits);
|
|
1149
|
-
const listeners = /* @__PURE__ */ new
|
|
1284
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
1150
1285
|
return {
|
|
1151
1286
|
getState() {
|
|
1152
1287
|
return state;
|
|
@@ -1163,14 +1298,18 @@ function createAirConditionerStore(options = {}) {
|
|
|
1163
1298
|
};
|
|
1164
1299
|
state = nextState;
|
|
1165
1300
|
writeAcState(storage, storageKey, nextState);
|
|
1166
|
-
listeners.
|
|
1301
|
+
for (const [listener, subscription] of [...listeners]) if (listeners.get(listener) === subscription) listener(event);
|
|
1167
1302
|
if (notification) options.onNotify?.(notification, event);
|
|
1168
1303
|
return event;
|
|
1169
1304
|
},
|
|
1170
1305
|
subscribe(listener) {
|
|
1171
|
-
listeners.
|
|
1306
|
+
let subscription = listeners.get(listener);
|
|
1307
|
+
if (!subscription) {
|
|
1308
|
+
subscription = Symbol("subscription");
|
|
1309
|
+
listeners.set(listener, subscription);
|
|
1310
|
+
}
|
|
1172
1311
|
return () => {
|
|
1173
|
-
listeners.delete(listener);
|
|
1312
|
+
if (listeners.get(listener) === subscription) listeners.delete(listener);
|
|
1174
1313
|
};
|
|
1175
1314
|
}
|
|
1176
1315
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -154,7 +154,17 @@ export interface AcAudioElements {
|
|
|
154
154
|
}
|
|
155
155
|
export interface AcAudioController {
|
|
156
156
|
beep: () => void;
|
|
157
|
+
/**
|
|
158
|
+
* Resume audio during a user gesture (`pointerdown` / `keydown`), before
|
|
159
|
+
* the click handler plays the beep. Safe to call more than once.
|
|
160
|
+
*/
|
|
161
|
+
unlock: () => void;
|
|
157
162
|
toggleStatusAudio: (previousStatus: boolean) => void;
|
|
163
|
+
/**
|
|
164
|
+
* Resume the running fan loop without replaying the power-on startup chime
|
|
165
|
+
* (e.g. when reconnecting a powered-on widget).
|
|
166
|
+
*/
|
|
167
|
+
resumeFan: () => void;
|
|
158
168
|
/**
|
|
159
169
|
* Change the volume of everything, including sounds already playing.
|
|
160
170
|
*/
|
|
@@ -211,7 +221,8 @@ export interface CreateAcAudioControllerOptions {
|
|
|
211
221
|
* 风声变小、并不定时随压缩机发力起伏。环境不支持或音频无法获取时退回
|
|
212
222
|
* `<audio>` 元素。
|
|
213
223
|
*
|
|
214
|
-
*
|
|
224
|
+
* 创建控制器时就会开始下载并解码,此时不会出声。浏览器要等用户手势里
|
|
225
|
+
* `resume()` 之后才允许播放,因此第一次 `beep()` / 开关机仍需发生在点击回调中。
|
|
215
226
|
*
|
|
216
227
|
* @param elements 三个音频元素;Web Audio 会从它们的 `src` 获取音频
|
|
217
228
|
* @param options 声音模式与可替换的环境依赖
|
package/dist/index.d.ts
CHANGED
|
@@ -154,7 +154,17 @@ export interface AcAudioElements {
|
|
|
154
154
|
}
|
|
155
155
|
export interface AcAudioController {
|
|
156
156
|
beep: () => void;
|
|
157
|
+
/**
|
|
158
|
+
* Resume audio during a user gesture (`pointerdown` / `keydown`), before
|
|
159
|
+
* the click handler plays the beep. Safe to call more than once.
|
|
160
|
+
*/
|
|
161
|
+
unlock: () => void;
|
|
157
162
|
toggleStatusAudio: (previousStatus: boolean) => void;
|
|
163
|
+
/**
|
|
164
|
+
* Resume the running fan loop without replaying the power-on startup chime
|
|
165
|
+
* (e.g. when reconnecting a powered-on widget).
|
|
166
|
+
*/
|
|
167
|
+
resumeFan: () => void;
|
|
158
168
|
/**
|
|
159
169
|
* Change the volume of everything, including sounds already playing.
|
|
160
170
|
*/
|
|
@@ -211,7 +221,8 @@ export interface CreateAcAudioControllerOptions {
|
|
|
211
221
|
* 风声变小、并不定时随压缩机发力起伏。环境不支持或音频无法获取时退回
|
|
212
222
|
* `<audio>` 元素。
|
|
213
223
|
*
|
|
214
|
-
*
|
|
224
|
+
* 创建控制器时就会开始下载并解码,此时不会出声。浏览器要等用户手势里
|
|
225
|
+
* `resume()` 之后才允许播放,因此第一次 `beep()` / 开关机仍需发生在点击回调中。
|
|
215
226
|
*
|
|
216
227
|
* @param elements 三个音频元素;Web Audio 会从它们的 `src` 获取音频
|
|
217
228
|
* @param options 声音模式与可替换的环境依赖
|
package/dist/index.js
CHANGED
|
@@ -167,6 +167,7 @@ function resolveAcAudioAssets(baseUrl = "/assets/audio", ext = "m4a") {
|
|
|
167
167
|
const readyTimeoutMs = 1500;
|
|
168
168
|
const fallbackTweenStepMs = 100;
|
|
169
169
|
const curveLength = 64;
|
|
170
|
+
const defaultAudioCache = /* @__PURE__ */ new Map();
|
|
170
171
|
function sourceUrl(element) {
|
|
171
172
|
return element?.currentSrc || element?.src || "";
|
|
172
173
|
}
|
|
@@ -178,7 +179,12 @@ async function fetchAudioData(url) {
|
|
|
178
179
|
function createDefaultAudioContext() {
|
|
179
180
|
const scope = globalThis;
|
|
180
181
|
const AudioContextConstructor = scope.AudioContext ?? scope.webkitAudioContext;
|
|
181
|
-
|
|
182
|
+
if (!AudioContextConstructor) return null;
|
|
183
|
+
try {
|
|
184
|
+
return new AudioContextConstructor({ latencyHint: "interactive" });
|
|
185
|
+
} catch {
|
|
186
|
+
return new AudioContextConstructor();
|
|
187
|
+
}
|
|
182
188
|
}
|
|
183
189
|
/**
|
|
184
190
|
* 合成一声很轻的「嗒」:音高快速下滑的正弦波,短促起音、指数衰减,像水珠落在窗台上。
|
|
@@ -200,6 +206,32 @@ function playDrip(context, output) {
|
|
|
200
206
|
oscillator.onended = () => gain.disconnect();
|
|
201
207
|
}
|
|
202
208
|
/**
|
|
209
|
+
* 样本还没解码出来时的按键声:很短的一声「嘀」,不依赖文件。
|
|
210
|
+
* 每次调用都是新的振荡器,连按不会互相等待。
|
|
211
|
+
*/
|
|
212
|
+
function playClick(context, output) {
|
|
213
|
+
if (typeof context.createOscillator !== "function") return false;
|
|
214
|
+
try {
|
|
215
|
+
const start = context.currentTime;
|
|
216
|
+
const oscillator = context.createOscillator();
|
|
217
|
+
const gain = context.createGain();
|
|
218
|
+
oscillator.type = "sine";
|
|
219
|
+
oscillator.frequency.setValueAtTime(2400, start);
|
|
220
|
+
oscillator.frequency.exponentialRampToValueAtTime(1400, start + .02);
|
|
221
|
+
gain.gain.setValueAtTime(1e-4, start);
|
|
222
|
+
gain.gain.exponentialRampToValueAtTime(.15, start + .002);
|
|
223
|
+
gain.gain.exponentialRampToValueAtTime(1e-4, start + .045);
|
|
224
|
+
oscillator.connect(gain);
|
|
225
|
+
gain.connect(output);
|
|
226
|
+
oscillator.start(start);
|
|
227
|
+
oscillator.stop(start + .05);
|
|
228
|
+
oscillator.onended = () => gain.disconnect();
|
|
229
|
+
return true;
|
|
230
|
+
} catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
203
235
|
* 等功率淡入 / 淡出曲线,噪声类声音交叉淡化时中段不会变小。
|
|
204
236
|
*/
|
|
205
237
|
function fadeCurve(direction) {
|
|
@@ -218,6 +250,20 @@ function playElement(element) {
|
|
|
218
250
|
element.load();
|
|
219
251
|
Promise.resolve(element.play()).catch(() => {});
|
|
220
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* 重播一段已经挂在元素上的短音效。
|
|
255
|
+
* `load()` 会丢掉预加载的数据并重新选资源,按键「嘀」声因此会晚一拍。
|
|
256
|
+
*/
|
|
257
|
+
function replayElement(element) {
|
|
258
|
+
if (!element) return;
|
|
259
|
+
try {
|
|
260
|
+
if (element.currentTime > 0) {
|
|
261
|
+
element.pause?.();
|
|
262
|
+
element.currentTime = 0;
|
|
263
|
+
}
|
|
264
|
+
} catch {}
|
|
265
|
+
Promise.resolve(element.play()).catch(() => {});
|
|
266
|
+
}
|
|
221
267
|
function stopElement(element) {
|
|
222
268
|
if (!element) return;
|
|
223
269
|
if (element.pause) element.pause();
|
|
@@ -230,7 +276,8 @@ function stopElement(element) {
|
|
|
230
276
|
* 风声变小、并不定时随压缩机发力起伏。环境不支持或音频无法获取时退回
|
|
231
277
|
* `<audio>` 元素。
|
|
232
278
|
*
|
|
233
|
-
*
|
|
279
|
+
* 创建控制器时就会开始下载并解码,此时不会出声。浏览器要等用户手势里
|
|
280
|
+
* `resume()` 之后才允许播放,因此第一次 `beep()` / 开关机仍需发生在点击回调中。
|
|
234
281
|
*
|
|
235
282
|
* @param elements 三个音频元素;Web Audio 会从它们的 `src` 获取音频
|
|
236
283
|
* @param options 声音模式与可替换的环境依赖
|
|
@@ -249,6 +296,10 @@ function createAcAudioController(elements, options = {}) {
|
|
|
249
296
|
/** 每次开关机自增,丢弃过期的异步回调 */
|
|
250
297
|
let session = 0;
|
|
251
298
|
const timers = /* @__PURE__ */ new Set();
|
|
299
|
+
/** 同一地址只拉取一次;decodeAudioData 会拆掉传入的 buffer,所以每次给它一份拷贝 */
|
|
300
|
+
const audioCache = options.loadAudioData ? /* @__PURE__ */ new Map() : defaultAudioCache;
|
|
301
|
+
/** 第一次按下时提前 resume,等 click 里真正 beep 时设备已经醒了 */
|
|
302
|
+
let stopUnlock;
|
|
252
303
|
/** `<audio>` 播放时的风声相对音量 */
|
|
253
304
|
let fallbackFanLevel = 1;
|
|
254
305
|
let fallbackTween;
|
|
@@ -293,44 +344,81 @@ function createAcAudioController(elements, options = {}) {
|
|
|
293
344
|
function ensureEngine() {
|
|
294
345
|
if (engine !== void 0) return engine;
|
|
295
346
|
engine = createEngine();
|
|
347
|
+
if (engine) stopUnlock = armGestureUnlock();
|
|
296
348
|
return engine;
|
|
297
349
|
}
|
|
350
|
+
/** 手势里唤醒音频设备。已经在跑时什么都不做。 */
|
|
351
|
+
function unlockPlayback() {
|
|
352
|
+
const web = ensureEngine();
|
|
353
|
+
if (web) resume(web);
|
|
354
|
+
}
|
|
355
|
+
function loadCached(url) {
|
|
356
|
+
let pending = audioCache.get(url);
|
|
357
|
+
if (!pending) {
|
|
358
|
+
const load = options.loadAudioData ?? fetchAudioData;
|
|
359
|
+
pending = new Promise((resolve, reject) => {
|
|
360
|
+
try {
|
|
361
|
+
resolve(load(url));
|
|
362
|
+
} catch (error) {
|
|
363
|
+
reject(error);
|
|
364
|
+
}
|
|
365
|
+
}).catch((error) => {
|
|
366
|
+
audioCache.delete(url);
|
|
367
|
+
throw error;
|
|
368
|
+
});
|
|
369
|
+
audioCache.set(url, pending);
|
|
370
|
+
}
|
|
371
|
+
return pending.then((data) => data.slice(0));
|
|
372
|
+
}
|
|
298
373
|
function createEngine() {
|
|
299
374
|
if (options.webAudio === false) return null;
|
|
300
|
-
let context;
|
|
301
375
|
try {
|
|
302
|
-
context = (options.createAudioContext ?? createDefaultAudioContext)();
|
|
376
|
+
const context = (options.createAudioContext ?? createDefaultAudioContext)();
|
|
377
|
+
if (!context) return null;
|
|
378
|
+
const master = context.createGain();
|
|
379
|
+
master.gain.value = volume();
|
|
380
|
+
master.connect(context.destination);
|
|
381
|
+
const buffers = {};
|
|
382
|
+
const decode = async (name) => {
|
|
383
|
+
const url = sourceUrl(elements[name]);
|
|
384
|
+
if (!url || buffers[name]) return;
|
|
385
|
+
try {
|
|
386
|
+
buffers[name] = await context.decodeAudioData(await loadCached(url));
|
|
387
|
+
} catch {}
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
context,
|
|
391
|
+
master,
|
|
392
|
+
buffers,
|
|
393
|
+
ready: Promise.all([
|
|
394
|
+
"beep",
|
|
395
|
+
"start",
|
|
396
|
+
"work"
|
|
397
|
+
].map(decode)).then(() => Boolean(buffers.start && buffers.work))
|
|
398
|
+
};
|
|
303
399
|
} catch {
|
|
304
400
|
return null;
|
|
305
401
|
}
|
|
306
|
-
if (!context) return null;
|
|
307
|
-
const master = context.createGain();
|
|
308
|
-
master.gain.value = volume();
|
|
309
|
-
master.connect(context.destination);
|
|
310
|
-
const buffers = {};
|
|
311
|
-
const load = options.loadAudioData ?? fetchAudioData;
|
|
312
|
-
const decode = async (name) => {
|
|
313
|
-
const url = sourceUrl(elements[name]);
|
|
314
|
-
if (!url) return;
|
|
315
|
-
try {
|
|
316
|
-
buffers[name] = await context.decodeAudioData(await load(url));
|
|
317
|
-
} catch {}
|
|
318
|
-
};
|
|
319
|
-
const ready = Promise.all([
|
|
320
|
-
"beep",
|
|
321
|
-
"start",
|
|
322
|
-
"work"
|
|
323
|
-
].map(decode)).then(() => Boolean(buffers.start && buffers.work));
|
|
324
|
-
return {
|
|
325
|
-
context,
|
|
326
|
-
master,
|
|
327
|
-
buffers,
|
|
328
|
-
ready
|
|
329
|
-
};
|
|
330
402
|
}
|
|
331
403
|
function resume(web) {
|
|
332
404
|
if (web.context.state === "suspended") Promise.resolve(web.context.resume()).catch(() => {});
|
|
333
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* pointerdown / keydown 比 click 更早,也比温度键的按下回调更早(捕获阶段)。
|
|
408
|
+
* 监听一直留着:手机切走再回来时上下文会重新被挂起。
|
|
409
|
+
*/
|
|
410
|
+
function armGestureUnlock() {
|
|
411
|
+
if (typeof document === "undefined") return () => {};
|
|
412
|
+
const onGesture = () => {
|
|
413
|
+
unlockPlayback();
|
|
414
|
+
};
|
|
415
|
+
document.addEventListener("pointerdown", onGesture, true);
|
|
416
|
+
document.addEventListener("keydown", onGesture, true);
|
|
417
|
+
return () => {
|
|
418
|
+
document.removeEventListener("pointerdown", onGesture, true);
|
|
419
|
+
document.removeEventListener("keydown", onGesture, true);
|
|
420
|
+
};
|
|
421
|
+
}
|
|
334
422
|
function waitReady(web) {
|
|
335
423
|
return Promise.race([web.ready, new Promise((resolve) => setTimeout(resolve, readyTimeoutMs, false))]);
|
|
336
424
|
}
|
|
@@ -407,7 +495,7 @@ function createAcAudioController(elements, options = {}) {
|
|
|
407
495
|
quiet();
|
|
408
496
|
}, soundTimings.settleAfterMs);
|
|
409
497
|
}
|
|
410
|
-
function startWeb(web) {
|
|
498
|
+
function startWeb(web, withStart = true) {
|
|
411
499
|
const output = web.context.createGain();
|
|
412
500
|
output.connect(web.master);
|
|
413
501
|
const fan = web.context.createGain();
|
|
@@ -420,9 +508,14 @@ function createAcAudioController(elements, options = {}) {
|
|
|
420
508
|
};
|
|
421
509
|
run = target;
|
|
422
510
|
const now = web.context.currentTime;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
511
|
+
if (withStart) {
|
|
512
|
+
track(target, playBuffer(web, web.buffers.start, output, now));
|
|
513
|
+
loopFan(web, target, now + soundTimings.startDelayMs / 1e3);
|
|
514
|
+
later(startFanCycle, soundTimings.startDelayMs);
|
|
515
|
+
} else {
|
|
516
|
+
loopFan(web, target, now);
|
|
517
|
+
startFanCycle();
|
|
518
|
+
}
|
|
426
519
|
}
|
|
427
520
|
function stopWeb(fadeMs) {
|
|
428
521
|
const target = run;
|
|
@@ -441,12 +534,10 @@ function createAcAudioController(elements, options = {}) {
|
|
|
441
534
|
});
|
|
442
535
|
setTimeout(() => target.output.disconnect(), fadeMs + 100);
|
|
443
536
|
}
|
|
444
|
-
function startFallback() {
|
|
537
|
+
function startFallback(withStart = true) {
|
|
445
538
|
const { start, work } = elements;
|
|
446
539
|
stopElement(work);
|
|
447
|
-
|
|
448
|
-
playElement(start);
|
|
449
|
-
later(() => {
|
|
540
|
+
const beginWork = () => {
|
|
450
541
|
if (!work) return;
|
|
451
542
|
fallbackFanLevel = 0;
|
|
452
543
|
applyElementVolume(work, 0);
|
|
@@ -454,14 +545,21 @@ function createAcAudioController(elements, options = {}) {
|
|
|
454
545
|
playElement(work);
|
|
455
546
|
tweenFallbackFan(initialFanLevel(), soundTimings.loopCrossfadeMs);
|
|
456
547
|
startFanCycle();
|
|
457
|
-
}
|
|
548
|
+
};
|
|
549
|
+
if (!withStart) {
|
|
550
|
+
beginWork();
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
applyElementVolume(start);
|
|
554
|
+
playElement(start);
|
|
555
|
+
later(beginWork, soundTimings.startDelayMs);
|
|
458
556
|
}
|
|
459
557
|
function stopFallback(fadeMs) {
|
|
460
558
|
stopElement(elements.start);
|
|
461
559
|
if (fadeMs > 0) tweenFallbackFan(0, fadeMs, () => stopElement(elements.work));
|
|
462
560
|
else stopElement(elements.work);
|
|
463
561
|
}
|
|
464
|
-
function powerOn() {
|
|
562
|
+
function powerOn(withStart = true) {
|
|
465
563
|
const id = ++session;
|
|
466
564
|
fanRunning = false;
|
|
467
565
|
fanCycle++;
|
|
@@ -471,14 +569,14 @@ function createAcAudioController(elements, options = {}) {
|
|
|
471
569
|
stopFallback(0);
|
|
472
570
|
const web = ensureEngine();
|
|
473
571
|
if (!web) {
|
|
474
|
-
startFallback();
|
|
572
|
+
startFallback(withStart);
|
|
475
573
|
return;
|
|
476
574
|
}
|
|
477
575
|
resume(web);
|
|
478
576
|
waitReady(web).then((ok) => {
|
|
479
577
|
if (id !== session) return;
|
|
480
|
-
if (ok) startWeb(web);
|
|
481
|
-
else startFallback();
|
|
578
|
+
if (ok) startWeb(web, withStart);
|
|
579
|
+
else startFallback(withStart);
|
|
482
580
|
});
|
|
483
581
|
}
|
|
484
582
|
function powerOff() {
|
|
@@ -489,31 +587,43 @@ function createAcAudioController(elements, options = {}) {
|
|
|
489
587
|
stopWeb(soundTimings.stopFadeMs);
|
|
490
588
|
stopFallback(soundTimings.stopFadeMs);
|
|
491
589
|
}
|
|
590
|
+
if (options.webAudio !== false) ensureEngine();
|
|
492
591
|
return {
|
|
592
|
+
unlock() {
|
|
593
|
+
unlockPlayback();
|
|
594
|
+
},
|
|
493
595
|
beep() {
|
|
494
596
|
if (soundMode === "mute") return;
|
|
495
597
|
const web = ensureEngine();
|
|
496
598
|
if (web) {
|
|
497
599
|
resume(web);
|
|
498
600
|
if (web.buffers.beep) {
|
|
499
|
-
playBuffer(web, web.buffers.beep, web.master, web.context.currentTime);
|
|
601
|
+
const source = playBuffer(web, web.buffers.beep, web.master, web.context.currentTime);
|
|
602
|
+
source.onended = () => {
|
|
603
|
+
try {
|
|
604
|
+
source.disconnect();
|
|
605
|
+
} catch {}
|
|
606
|
+
};
|
|
500
607
|
return;
|
|
501
608
|
}
|
|
609
|
+
if (playClick(web.context, web.master)) return;
|
|
502
610
|
}
|
|
503
611
|
applyElementVolume(elements.beep);
|
|
504
|
-
|
|
612
|
+
replayElement(elements.beep);
|
|
505
613
|
},
|
|
506
614
|
drip() {
|
|
507
615
|
if (soundMode !== "normal") return;
|
|
508
616
|
const web = ensureEngine();
|
|
509
|
-
if (!web || typeof web.context.createOscillator !== "function") return;
|
|
510
|
-
resume(web);
|
|
617
|
+
if (!web || web.context.state !== "running" || typeof web.context.createOscillator !== "function") return;
|
|
511
618
|
playDrip(web.context, web.master);
|
|
512
619
|
},
|
|
513
620
|
toggleStatusAudio(previousStatus) {
|
|
514
621
|
if (previousStatus) powerOff();
|
|
515
622
|
else powerOn();
|
|
516
623
|
},
|
|
624
|
+
resumeFan() {
|
|
625
|
+
powerOn(false);
|
|
626
|
+
},
|
|
517
627
|
setSoundMode(mode) {
|
|
518
628
|
soundMode = mode;
|
|
519
629
|
if (engine) engine.master.gain.setTargetAtTime(volume(), engine.context.currentTime, .08);
|
|
@@ -542,6 +652,8 @@ function createAcAudioController(elements, options = {}) {
|
|
|
542
652
|
cancelFallbackTween();
|
|
543
653
|
stopWeb(0);
|
|
544
654
|
stopFallback(0);
|
|
655
|
+
stopUnlock?.();
|
|
656
|
+
stopUnlock = void 0;
|
|
545
657
|
if (engine && !options.createAudioContext) Promise.resolve(engine.context.close()).catch(() => {});
|
|
546
658
|
engine = null;
|
|
547
659
|
}
|
|
@@ -717,7 +829,7 @@ function observeAcScale(target, designWidth, onScale) {
|
|
|
717
829
|
onScale(scale);
|
|
718
830
|
}
|
|
719
831
|
};
|
|
720
|
-
update(target.getBoundingClientRect().width);
|
|
832
|
+
update(target.offsetWidth ?? target.getBoundingClientRect().width);
|
|
721
833
|
const observer = new ResizeObserver((entries) => {
|
|
722
834
|
for (const entry of entries) update(entry.contentRect.width);
|
|
723
835
|
});
|
|
@@ -739,23 +851,38 @@ function observeAcTilt(target, onTilt, maxTilt = 4) {
|
|
|
739
851
|
const matchMedia = globalThis.matchMedia;
|
|
740
852
|
if (typeof matchMedia !== "function" || !matchMedia(finePointerQuery).matches || matchMedia("(prefers-reduced-motion: reduce)").matches) return () => {};
|
|
741
853
|
let frame = 0;
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
854
|
+
let pendingX = 0;
|
|
855
|
+
let pendingY = 0;
|
|
856
|
+
let tilted = false;
|
|
857
|
+
const apply = (rotateX, rotateY) => {
|
|
858
|
+
tilted = rotateX !== 0 || rotateY !== 0;
|
|
859
|
+
onTilt(rotateX, rotateY);
|
|
745
860
|
};
|
|
746
861
|
const onMove = (event) => {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
862
|
+
pendingX = event.clientX;
|
|
863
|
+
pendingY = event.clientY;
|
|
864
|
+
if (frame) return;
|
|
865
|
+
frame = requestAnimationFrame(() => {
|
|
866
|
+
frame = 0;
|
|
867
|
+
const rect = target.getBoundingClientRect();
|
|
868
|
+
if (!rect.width || !rect.height) return;
|
|
869
|
+
const x = (pendingX - rect.left) / rect.width - .5;
|
|
870
|
+
const y = (pendingY - rect.top) / rect.height - .5;
|
|
871
|
+
const round = (value) => Math.round(value * 100) / 100;
|
|
872
|
+
apply(round(-y * 2 * maxTilt), round(x * 2 * maxTilt));
|
|
873
|
+
});
|
|
753
874
|
};
|
|
754
|
-
const onLeave = () =>
|
|
755
|
-
|
|
756
|
-
|
|
875
|
+
const onLeave = () => {
|
|
876
|
+
if (frame) {
|
|
877
|
+
cancelAnimationFrame(frame);
|
|
878
|
+
frame = 0;
|
|
879
|
+
}
|
|
880
|
+
if (tilted) apply(0, 0);
|
|
881
|
+
};
|
|
882
|
+
target.addEventListener("pointermove", onMove, { passive: true });
|
|
883
|
+
target.addEventListener("pointerleave", onLeave, { passive: true });
|
|
757
884
|
return () => {
|
|
758
|
-
|
|
885
|
+
onLeave();
|
|
759
886
|
target.removeEventListener("pointermove", onMove);
|
|
760
887
|
target.removeEventListener("pointerleave", onLeave);
|
|
761
888
|
};
|
|
@@ -839,27 +966,34 @@ function bindAcPressRepeat(element, fire, options = {}) {
|
|
|
839
966
|
let timer;
|
|
840
967
|
let wait = interval;
|
|
841
968
|
let last;
|
|
969
|
+
let disposed = false;
|
|
970
|
+
let generation = 0;
|
|
842
971
|
const stop = () => {
|
|
972
|
+
generation++;
|
|
843
973
|
clearTimeout(timer);
|
|
844
974
|
timer = void 0;
|
|
845
975
|
};
|
|
846
|
-
const tick = () => {
|
|
976
|
+
const tick = (press) => {
|
|
977
|
+
if (disposed || press !== generation) return;
|
|
847
978
|
if (value && value() === last) return stop();
|
|
848
979
|
last = value?.();
|
|
849
980
|
fire();
|
|
981
|
+
if (disposed || press !== generation) return;
|
|
850
982
|
wait = Math.max(minInterval, wait * .85);
|
|
851
|
-
timer = setTimeout(tick, wait);
|
|
983
|
+
timer = setTimeout(tick, wait, press);
|
|
852
984
|
};
|
|
853
985
|
const onPointerDown = (event) => {
|
|
854
|
-
if (event.button !== 0) return;
|
|
986
|
+
if (disposed || event.button !== 0) return;
|
|
855
987
|
stop();
|
|
988
|
+
const press = generation;
|
|
856
989
|
last = value?.();
|
|
857
990
|
fire();
|
|
991
|
+
if (disposed || press !== generation) return;
|
|
858
992
|
wait = interval;
|
|
859
|
-
timer = setTimeout(tick, delay);
|
|
993
|
+
timer = setTimeout(tick, delay, press);
|
|
860
994
|
};
|
|
861
995
|
const onClick = (event) => {
|
|
862
|
-
if (event.detail === 0) fire();
|
|
996
|
+
if (!disposed && event.detail === 0) fire();
|
|
863
997
|
};
|
|
864
998
|
const onContextMenu = (event) => event.preventDefault();
|
|
865
999
|
element.addEventListener("pointerdown", onPointerDown);
|
|
@@ -869,6 +1003,7 @@ function bindAcPressRepeat(element, fire, options = {}) {
|
|
|
869
1003
|
element.addEventListener("click", onClick);
|
|
870
1004
|
element.addEventListener("contextmenu", onContextMenu);
|
|
871
1005
|
return () => {
|
|
1006
|
+
disposed = true;
|
|
872
1007
|
stop();
|
|
873
1008
|
element.removeEventListener("pointerdown", onPointerDown);
|
|
874
1009
|
element.removeEventListener("pointerup", stop);
|
|
@@ -1145,7 +1280,7 @@ function createAirConditionerStore(options = {}) {
|
|
|
1145
1280
|
...persistedState,
|
|
1146
1281
|
...options.initialState
|
|
1147
1282
|
}, limits);
|
|
1148
|
-
const listeners = /* @__PURE__ */ new
|
|
1283
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
1149
1284
|
return {
|
|
1150
1285
|
getState() {
|
|
1151
1286
|
return state;
|
|
@@ -1162,14 +1297,18 @@ function createAirConditionerStore(options = {}) {
|
|
|
1162
1297
|
};
|
|
1163
1298
|
state = nextState;
|
|
1164
1299
|
writeAcState(storage, storageKey, nextState);
|
|
1165
|
-
listeners.
|
|
1300
|
+
for (const [listener, subscription] of [...listeners]) if (listeners.get(listener) === subscription) listener(event);
|
|
1166
1301
|
if (notification) options.onNotify?.(notification, event);
|
|
1167
1302
|
return event;
|
|
1168
1303
|
},
|
|
1169
1304
|
subscribe(listener) {
|
|
1170
|
-
listeners.
|
|
1305
|
+
let subscription = listeners.get(listener);
|
|
1306
|
+
if (!subscription) {
|
|
1307
|
+
subscription = Symbol("subscription");
|
|
1308
|
+
listeners.set(listener, subscription);
|
|
1309
|
+
}
|
|
1171
1310
|
return () => {
|
|
1172
|
-
listeners.delete(listener);
|
|
1311
|
+
if (listeners.get(listener) === subscription) listeners.delete(listener);
|
|
1173
1312
|
};
|
|
1174
1313
|
}
|
|
1175
1314
|
};
|
package/package.json
CHANGED