despia-native 1.0.14 → 1.0.15
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 +9 -1
- package/index.js +20 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -640,9 +640,17 @@ These CSS variables are automatically provided by the Despia native runtime and
|
|
|
640
640
|
- **watch** (string[], optional): Array of variable names to watch for in the response
|
|
641
641
|
|
|
642
642
|
Returns a Promise that resolves when all watched variables are available:
|
|
643
|
-
- **Single variable**:
|
|
643
|
+
- **Single variable**: 30-second timeout with simple observation (Promise always resolves; on timeout it resolves with `undefined`)
|
|
644
644
|
- **Multiple variables**: Uses VariableTracker with 5-minute auto-cleanup
|
|
645
645
|
|
|
646
|
+
### Timeout behavior
|
|
647
|
+
|
|
648
|
+
When you call `despia(command, ['someVariable'])`, the SDK waits up to 30 seconds for
|
|
649
|
+
`window.someVariable` to be set by the native runtime. If it appears earlier, the
|
|
650
|
+
Promise resolves with that value. If it is never set, the Promise still resolves
|
|
651
|
+
after 30 seconds with `undefined` and a timeout is logged to the console. This
|
|
652
|
+
prevents hanging Promises for long-running or failing native operations.
|
|
653
|
+
|
|
646
654
|
### Direct Property Access
|
|
647
655
|
|
|
648
656
|
Access any window variable directly through the despia object:
|
package/index.js
CHANGED
|
@@ -42,20 +42,29 @@
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
// Simple variable observer with
|
|
46
|
-
function observeDespiaVariable(variableName, callback, timeout =
|
|
45
|
+
// Simple variable observer with longer timeout and guaranteed resolve
|
|
46
|
+
function observeDespiaVariable(variableName, callback, timeout = 30000) {
|
|
47
47
|
const startTime = Date.now();
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
function checkVariable() {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const val = window[variableName];
|
|
51
|
+
|
|
52
|
+
// Preserve existing semantics: any non-undefined value is "ready"
|
|
53
|
+
if (val !== undefined) {
|
|
54
|
+
callback(val);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (Date.now() - startTime < timeout) {
|
|
53
59
|
setTimeout(checkVariable, 100);
|
|
54
|
-
|
|
55
|
-
console.error(`Despia timeout: ${variableName} was not set within ${timeout} ms`);
|
|
60
|
+
return;
|
|
56
61
|
}
|
|
62
|
+
|
|
63
|
+
console.error(`Despia timeout: ${variableName} was not set within ${timeout} ms`);
|
|
64
|
+
// Ensure promises watching this variable never hang forever
|
|
65
|
+
callback(undefined);
|
|
57
66
|
}
|
|
58
|
-
|
|
67
|
+
|
|
59
68
|
checkVariable();
|
|
60
69
|
}
|
|
61
70
|
|
|
@@ -125,11 +134,11 @@
|
|
|
125
134
|
}
|
|
126
135
|
|
|
127
136
|
if (variables.length === 1) {
|
|
128
|
-
// Simple single variable observation with
|
|
137
|
+
// Simple single variable observation with 30-second timeout
|
|
129
138
|
return new Promise((resolve) => {
|
|
130
139
|
observeDespiaVariable(variables[0], (value) => {
|
|
131
140
|
resolve({ [variables[0]]: value });
|
|
132
|
-
},
|
|
141
|
+
}, 30000);
|
|
133
142
|
});
|
|
134
143
|
} else {
|
|
135
144
|
// Multiple variables - use VariableTracker (no timeout, expires after 5 minutes)
|