Skip to content

Commit d992ef0

Browse files
committed
fix: perf
1 parent 0663d65 commit d992ef0

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ios-vibrator-pro-max",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "Bringing the vibes back to iOS safari...",
55
"homepage": "https://ios-vibrate-api-demo.vercel.app/",
66
"type": "module",

src/mergeVibrations.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
3737
currentTime - startTime + interval
3838
);
3939

40-
// Mark the vibration period in the timeline
41-
for (let i = fromIndex; i < toIndex; i++) {
42-
timeline[i] = true;
43-
}
40+
timeline.fill(true, fromIndex, toIndex);
4441
}
4542
currentTime += interval;
4643
isVibrating = !isVibrating;
@@ -49,12 +46,13 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
4946

5047
// Convert timeline back to intervals
5148
const result: number[] = [];
49+
const length = timeline.length;
5250
let currentState = timeline[0];
5351
let currentCount = 0;
5452

5553
// Process the timeline including the final state transition
56-
for (let i = 0; i <= timeline.length; i++) {
57-
const newState = i < timeline.length ? timeline[i] : !currentState;
54+
for (let i = 0; i <= length; i++) {
55+
const newState = i < length ? timeline[i] : !currentState;
5856

5957
if (newState !== currentState) {
6058
if (currentCount > 0) {
@@ -77,3 +75,36 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
7775

7876
return result;
7977
}
78+
79+
export function trimVibrations(amount: number, patterns: number[]): number[] {
80+
// Initialize result array
81+
const result: number[] = [];
82+
83+
// Apply remaining amount to the first element
84+
let remainingAmount = amount;
85+
86+
// Process each vibration in the pattern
87+
for (let i = 0; i < patterns.length; i++) {
88+
const currentVibration = patterns[i];
89+
90+
// If we still have amount to trim
91+
if (remainingAmount > 0) {
92+
// Calculate what's left after trimming
93+
const remaining = currentVibration - remainingAmount;
94+
95+
// If there's duration left, add it to the result
96+
if (remaining > 0) {
97+
result.push(remaining);
98+
remainingAmount = 0; // Used all the trim amount
99+
} else {
100+
// This vibration was completely consumed
101+
remainingAmount = Math.abs(remaining); // Carry over the remaining amount
102+
}
103+
} else {
104+
// No more trimming needed, add the vibration as is
105+
result.push(currentVibration);
106+
}
107+
}
108+
109+
return result;
110+
}

src/pro-max-vibrator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sorry Tim Cook, PWAs deserve some love too...
22

3-
import { mergeVibrations, type Vibration } from "./mergeVibrations.js";
3+
import { trimVibrations, type Vibration } from "./mergeVibrations.js";
44

55
const SAFARI_VERSION = getSafariVersion();
66
const MAGIC_NUMBER = 26.26;
@@ -49,7 +49,10 @@ async function grantedVibrate() {
4949
let adjustment = 0;
5050

5151
while (true) {
52-
vibration = [Date.now(), mergeVibrations([Date.now(), []], vibration)];
52+
vibration = [
53+
Date.now(),
54+
trimVibrations(Date.now() - vibration[0], vibration[1]),
55+
];
5356

5457
const [vibrateMs, waitMs] = vibration[1] as (number | undefined)[];
5558

0 commit comments

Comments
 (0)