/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/javascript/code-411-113-004/main.js
47 строк
1 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
// Сторона JavaScript class NativeBridge { constructor() { this.callbacks = new Map(); this.callId = 0; } call(method, params = {}) { return new Promise((resolve, reject) => { const id = ++this.callId; this.callbacks.set(id, { resolve, reject }); const message = { id: id, method: method, params: params, timestamp: Date.now() }; this.sendToNative(JSON.stringify(message)); }); } sendToNative(message) { // Платформенно-зависимая реализация if (window.AndroidBridge) { window.AndroidBridge.postMessage(message); } else if (window.webkit?.messageHandlers?.nativeBridge) { window.webkit.messageHandlers.nativeBridge.postMessage(message); } } handleResponse(response) { const { id, result, error } = JSON.parse(response); const callback = this.callbacks.get(id); if (callback) { if (error) { callback.reject(new Error(error)); } else { callback.resolve(result); } this.callbacks.delete(id); } } } window.bridge = new NativeBridge();