Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / youpipe / src / main / assets / sabr_webpo_client.js

sabr_webpo_client.js69 lines · 2.7 KiBRaw
1(function () {
2 'use strict';
3
4 var contentBinding = window.__SABR_WEBPO_CONTENT_BINDING;
5
6 function stage(name, detail) {
7 SabrPocBridge.onStage(name, String(detail));
8 }
9
10 stage('script_start', 'bindingLength=' + (contentBinding ? contentBinding.length : -1));
11 console.info('[sabr-webpo] script start bindingLength=' +
12 (contentBinding ? contentBinding.length : -1));
13
14 function report(result) {
15 console.info('[sabr-webpo] report ok=' + !!result.ok);
16 SabrPocBridge.onResult(JSON.stringify(result));
17 }
18
19 function waitForClient(attempt) {
20 var type = typeof window.top['havuokmhhs-0']?.bevasrs?.wpc;
21 stage('client_poll', 'attempt=' + attempt + ' type=' + type);
22 console.info('[sabr-webpo] client attempt=' + attempt + ' type=' + type);
23 if (type === 'function') {
24 return Promise.resolve();
25 }
26 if (attempt >= 10) {
27 return Promise.reject(new Error('WebPoClient unavailable'));
28 }
29 return new Promise(function (resolve) {
30 setTimeout(resolve, 1000);
31 }).then(function () {
32 return waitForClient(attempt + 1);
33 });
34 }
35
36 function mint(attempt) {
37 stage('mint_start', 'attempt=' + attempt);
38 console.info('[sabr-webpo] mint attempt=' + attempt);
39 return window.top['havuokmhhs-0'].bevasrs.wpc().then(function (client) {
40 stage('client_resolved', 'attempt=' + attempt + ' mws=' + typeof client?.mws);
41 console.info('[sabr-webpo] client resolved mws=' + typeof client?.mws);
42 return client.mws({c: contentBinding, mc: false, me: false});
43 }).catch(function (error) {
44 stage('mint_error', 'attempt=' + attempt + ' error=' + String(error));
45 console.warn('[sabr-webpo] mint error=' + String(error));
46 if (String(error).indexOf('SDF:notready') >= 0 && attempt < 10) {
47 return new Promise(function (resolve) {
48 setTimeout(resolve, 1000);
49 }).then(function () {
50 return mint(attempt + 1);
51 });
52 }
53 throw error;
54 });
55 }
56
57 waitForClient(0).then(function () {
58 return mint(0);
59 }).then(function (poToken) {
60 stage('mint_success', 'tokenLength=' + (poToken ? poToken.length : -1));
61 console.info('[sabr-webpo] mint success tokenLength=' +
62 (poToken ? poToken.length : -1));
63 report({ok: true, poToken: poToken});
64 }).catch(function (error) {
65 stage('pipeline_error', String(error));
66 console.error('[sabr-webpo] pipeline error=' + String(error));
67 report({ok: false, error: String(error)});
68 });
69})();