despia-native 1.0.15 → 1.0.17
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 +8 -1
- package/index.js +32 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -640,7 +640,7 @@ 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**: 30-second timeout with
|
|
643
|
+
- **Single variable**: 30-second timeout with observation that ignores empty placeholders and requires a fresh write (Promise always resolves; on timeout it resolves with `undefined`)
|
|
644
644
|
- **Multiple variables**: Uses VariableTracker with 5-minute auto-cleanup
|
|
645
645
|
|
|
646
646
|
### Timeout behavior
|
|
@@ -651,6 +651,13 @@ Promise resolves with that value. If it is never set, the Promise still resolves
|
|
|
651
651
|
after 30 seconds with `undefined` and a timeout is logged to the console. This
|
|
652
652
|
prevents hanging Promises for long-running or failing native operations.
|
|
653
653
|
|
|
654
|
+
### Fresh-data behavior
|
|
655
|
+
|
|
656
|
+
Before observing, watched variables are cleared to avoid resolving on stale values.
|
|
657
|
+
The observer ignores empty placeholders (`undefined`, `null`, `"n/a"`, `{}`, `[]`)
|
|
658
|
+
and requires the value to change from its baseline before resolving. This ensures
|
|
659
|
+
each call waits for a fresh write from the native side.
|
|
660
|
+
|
|
654
661
|
### Direct Property Access
|
|
655
662
|
|
|
656
663
|
Access any window variable directly through the despia object:
|
package/index.js
CHANGED
|
@@ -41,16 +41,39 @@
|
|
|
41
41
|
processQueue();
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
+
|
|
45
|
+
// Generate a safe signature for change detection
|
|
46
|
+
function safeSig(val) {
|
|
47
|
+
if (val === undefined) return 'u';
|
|
48
|
+
if (val === null) return 'n';
|
|
49
|
+
const t = typeof val;
|
|
50
|
+
if (t !== 'object') return `${t}:${String(val)}`;
|
|
51
|
+
try { return `o:${JSON.stringify(val)}`; } catch { return 'o:[unserializable]'; }
|
|
52
|
+
}
|
|
44
53
|
|
|
45
|
-
// Simple variable observer with longer timeout and guaranteed resolve
|
|
54
|
+
// Simple variable observer with longer timeout, change detection, and guaranteed resolve
|
|
46
55
|
function observeDespiaVariable(variableName, callback, timeout = 30000) {
|
|
47
56
|
const startTime = Date.now();
|
|
57
|
+
const initialRef = window[variableName];
|
|
58
|
+
const initialSig = safeSig(initialRef);
|
|
59
|
+
|
|
60
|
+
const ready = (val) => {
|
|
61
|
+
if (val === undefined || val === null || val === "n/a") return false;
|
|
62
|
+
if (Array.isArray(val) && val.length === 0) return false;
|
|
63
|
+
if (val && typeof val === 'object' && !Array.isArray(val) && Object.keys(val).length === 0) return false;
|
|
64
|
+
return true;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const changed = (val) => {
|
|
68
|
+
if (val !== initialRef) return true;
|
|
69
|
+
return safeSig(val) !== initialSig;
|
|
70
|
+
};
|
|
48
71
|
|
|
49
72
|
function checkVariable() {
|
|
50
73
|
const val = window[variableName];
|
|
51
74
|
|
|
52
|
-
|
|
53
|
-
|
|
75
|
+
if (ready(val) && changed(val)) {
|
|
76
|
+
// Fresh, non-empty value arrived → resolve with it
|
|
54
77
|
callback(val);
|
|
55
78
|
return;
|
|
56
79
|
}
|
|
@@ -60,8 +83,8 @@
|
|
|
60
83
|
return;
|
|
61
84
|
}
|
|
62
85
|
|
|
86
|
+
// Timeout: still resolve so promises don't hang
|
|
63
87
|
console.error(`Despia timeout: ${variableName} was not set within ${timeout} ms`);
|
|
64
|
-
// Ensure promises watching this variable never hang forever
|
|
65
88
|
callback(undefined);
|
|
66
89
|
}
|
|
67
90
|
|
|
@@ -133,6 +156,11 @@
|
|
|
133
156
|
return Promise.resolve({});
|
|
134
157
|
}
|
|
135
158
|
|
|
159
|
+
// Pre-clear watched vars to avoid stale immediate resolves
|
|
160
|
+
variables.forEach((name) => {
|
|
161
|
+
try { delete window[name]; } catch (e) { window[name] = undefined; }
|
|
162
|
+
});
|
|
163
|
+
|
|
136
164
|
if (variables.length === 1) {
|
|
137
165
|
// Simple single variable observation with 30-second timeout
|
|
138
166
|
return new Promise((resolve) => {
|