js-gei 1.0.1 → 1.0.3

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.
@@ -1,5 +1,6 @@
1
1
  import axios from 'axios';
2
- var RpcClient = initRpcClient();
2
+
3
+ let RpcClient = initRpcClient();
3
4
 
4
5
  function initRpcClient() {
5
6
  let dt = new Date();
@@ -8,7 +9,7 @@ function initRpcClient() {
8
9
  let _preempted = null;
9
10
  let _pendingAjaxCount = 0;
10
11
 
11
- let _currentPath = window.location.pathname.trim().replace(/(^[\/]*|[\/]*$)/g, "");
12
+ let _currentPath = window.location.pathname.trim().replace(/(^[/]*|[/]*$)/g, "");
12
13
  let _currentPathTopLevel = '(unavailable)';
13
14
  try {
14
15
  _currentPathTopLevel = window.top.location.pathname.trim().replace(/(^[\/]*|[\/]*$)/g, "");
@@ -866,4 +867,7 @@ function initRpcClient() {
866
867
  };
867
868
  }
868
869
  };
869
- }
870
+ }
871
+
872
+ // Export RpcClient for use in other modules
873
+ export { RpcClient };
@@ -0,0 +1,284 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RpcClient = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ exports.RpcClient = (() => {
9
+ let _id = Date.now();
10
+ let _ctxid = 0;
11
+ let _preempted = null;
12
+ let _pendingAjaxCount = 0;
13
+ const _currentPath = window.location.pathname.trim().replace(/(^[\/]*|[\/]*$)/g, "");
14
+ const _currentPathTopLevel = (() => {
15
+ var _a;
16
+ try {
17
+ return ((_a = window.top) === null || _a === void 0 ? void 0 : _a.location.pathname.trim().replace(/(^[\/]*|[\/]*$)/g, "")) || '(unavailable)';
18
+ }
19
+ catch (_b) {
20
+ return '(unavailable)';
21
+ }
22
+ })();
23
+ const configTemplate = {
24
+ url: "json.rpc",
25
+ batch: true,
26
+ cors: false,
27
+ jsonRpcPreemptAfter: 7,
28
+ autoQueryPreempted: true,
29
+ log: console.log,
30
+ unhandledError: console.log
31
+ };
32
+ const baseUrl = (() => {
33
+ const baseElement = document.querySelector("head base");
34
+ return baseElement ? baseElement.getAttribute("href") : null;
35
+ })();
36
+ const urlCombine = (parts) => parts.map(part => part.trim().replace(/(^[\/]*|[\/]*$)/g, "")).join("/");
37
+ if (_currentPath) {
38
+ configTemplate.url = baseUrl
39
+ ? urlCombine([baseUrl, _currentPath, configTemplate.url])
40
+ : urlCombine([_currentPath, configTemplate.url]);
41
+ }
42
+ else if (baseUrl) {
43
+ configTemplate.url = urlCombine([baseUrl, configTemplate.url]);
44
+ }
45
+ const preparePacket = (method, ...args) => ({
46
+ method,
47
+ params: args.length === 1 && (Array.isArray(args[0]) || typeof args[0] === 'object') ? args[0] : args
48
+ });
49
+ const schedulePreemptedQuery = (id, ticket, processor, progressor, context, queue) => {
50
+ if (!_preempted)
51
+ _preempted = [];
52
+ _preempted.push({ id, ticket, processor, progressor, context, queue });
53
+ const query = () => {
54
+ if (!_preempted || !_preempted.length) {
55
+ _preempted = null;
56
+ return;
57
+ }
58
+ const ctx = exports.RpcClient.context().onFinally(() => setTimeout(query, 2000));
59
+ const tickets = _preempted.map(item => item.ticket);
60
+ exports.RpcClient.call('$.QueryPreemptedResponses', { tickets })
61
+ .done((response) => {
62
+ if (response.error) {
63
+ const items = _preempted.splice(0, tickets.length);
64
+ items.forEach(item => item.processor.process(response));
65
+ items.forEach(item => {
66
+ if (item.context.preempted) {
67
+ item.context.preempted = null;
68
+ item.context.__leave();
69
+ }
70
+ });
71
+ return false;
72
+ }
73
+ Object.entries(response.result).forEach(([ticket, report]) => {
74
+ const idx = _preempted.findIndex(p => p.ticket === ticket);
75
+ if (idx === -1)
76
+ return;
77
+ const item = _preempted[idx];
78
+ if (report.status === 'error') {
79
+ _preempted.splice(idx, 1);
80
+ item.processor.process({ id: item.id, error: { code: -1, message: report.reason } });
81
+ }
82
+ else if (report.status === 'ok') {
83
+ exports.RpcClient.call('$.RetrievePreemptedResponse', { ticket })
84
+ .done((realResponse) => item.processor.process(realResponse))
85
+ .post(ctx);
86
+ }
87
+ else if (item.progressor) {
88
+ item.progressor.progress(report);
89
+ }
90
+ });
91
+ return false;
92
+ })
93
+ .post(ctx);
94
+ };
95
+ setTimeout(query, 2000);
96
+ };
97
+ const dispatcher = (() => {
98
+ let _queue = [];
99
+ let _last = null;
100
+ const _delay = 700;
101
+ const postQueue = (config) => {
102
+ const queue = _queue;
103
+ _queue = [];
104
+ if (!queue.length)
105
+ return;
106
+ const batch = queue.length === 1 ? queue[0].batch : queue.flatMap(q => q.batch);
107
+ const request = batch.length === 1
108
+ ? batch[0].request
109
+ : [{ method: '$.BeginSynchronizedBatch', params: [] }, ...batch.map(b => b.request)];
110
+ const handler = (data, xhr) => {
111
+ if (!data) {
112
+ queue.forEach(q => q.fail(true, "Server returned an empty response."));
113
+ return;
114
+ }
115
+ const responses = Array.isArray(data) ? data : [data];
116
+ responses.forEach(response => {
117
+ const item = queue.find(q => q.batch.some(b => b.request.id === response.id));
118
+ if (!item)
119
+ return;
120
+ const processor = {
121
+ context: item.context,
122
+ callbacks: item.batch.find(b => b.request.id === response.id).callbacks,
123
+ process: (response) => {
124
+ const doDefaultProcessing = processor.context.__preview(response);
125
+ if (!doDefaultProcessing)
126
+ return;
127
+ processor.callbacks.forEach(callback => callback(response, xhr));
128
+ }
129
+ };
130
+ if (response.error && response.error.code === -32099 && config.autoQueryPreempted) {
131
+ const ticket = response.error.data;
132
+ if (!item.context.preempted)
133
+ item.context.preempted = [];
134
+ item.context.preempted.push(ticket);
135
+ schedulePreemptedQuery(response.id, ticket, processor, null, item.context, queue);
136
+ }
137
+ else {
138
+ processor.process(response, xhr);
139
+ }
140
+ });
141
+ queue.forEach(q => {
142
+ if (!q.context.preempted)
143
+ q.context.__leave();
144
+ });
145
+ };
146
+ axios_1.default.post(config.url, request, { headers: config.headers, timeout: 0, responseType: 'blob', withCredentials: true })
147
+ .then((response) => handler(JSON.parse(response.data), response.request))
148
+ .catch(error => handler({ error: { status: error.code, details: error.message }, ajaxCallFailed: true }));
149
+ };
150
+ return {
151
+ dispatch: (batch, context, fail, config) => {
152
+ const now = new Date();
153
+ if (!_queue.length)
154
+ _queue.created = now;
155
+ _queue.push({ batch, context, fail });
156
+ if (!config.batch || !_pendingAjaxCount) {
157
+ postQueue(config);
158
+ return;
159
+ }
160
+ const diff = _last ? now.getTime() - _last.getTime() : 0;
161
+ const span = now.getTime() - _queue.created.getTime();
162
+ if (span < 3000 && (_pendingAjaxCount > 3 || diff < _delay)) {
163
+ clearTimeout(_queue.timeoutHandle);
164
+ }
165
+ _queue.timeoutHandle = setTimeout(() => postQueue(config), _delay + 50);
166
+ _last = now;
167
+ }
168
+ };
169
+ })();
170
+ const main = (packet, config) => {
171
+ const batch = [
172
+ { request: packet, callbacks: [], progressCallbacks: [] }
173
+ ];
174
+ let context = null;
175
+ const fail = (rpc, e) => {
176
+ if (context && rpc === true && context.__rpcFail(e))
177
+ return;
178
+ if (config && config.unhandledError)
179
+ config.unhandledError(e);
180
+ else
181
+ console.log(e);
182
+ };
183
+ return {
184
+ call: function (...args) {
185
+ const p = preparePacket(...args);
186
+ _id++;
187
+ p.id = _id;
188
+ batch.push({ request: p, callbacks: [], progressCallbacks: [] });
189
+ return this;
190
+ },
191
+ notify: function (...args) {
192
+ const p = preparePacket(...args);
193
+ batch.push({ request: p, callbacks: [], progressCallbacks: [] });
194
+ return this;
195
+ },
196
+ done: function (callback) {
197
+ if (typeof callback === 'function')
198
+ batch[batch.length - 1].callbacks.push(callback);
199
+ return this;
200
+ },
201
+ post: function (ctx, customConfig) {
202
+ context = ctx || exports.RpcClient.context();
203
+ context.__enter();
204
+ const config = customConfig || exports.RpcClient.config;
205
+ if (!config) {
206
+ fail(true, "RpcClient was invoked without a valid config object!");
207
+ context.__leave();
208
+ return;
209
+ }
210
+ dispatcher.dispatch(batch, context, fail, config);
211
+ }
212
+ };
213
+ };
214
+ return {
215
+ config: configTemplate,
216
+ call: function (...args) {
217
+ const packet = preparePacket(...args);
218
+ _id++;
219
+ packet.id = _id;
220
+ return main(packet, this.config);
221
+ },
222
+ notify: function (...args) {
223
+ const packet = preparePacket(...args);
224
+ return main(packet, this.config);
225
+ },
226
+ context: function () {
227
+ _ctxid++;
228
+ const ctx = {
229
+ id: _ctxid,
230
+ count: 0,
231
+ rpcErrorHandlers: [],
232
+ apiErrorHandlers: [],
233
+ apiSuccessHandlers: [],
234
+ responsePreviewers: [],
235
+ finallyHandlers: [],
236
+ onRpcError(callback) {
237
+ if (typeof callback === 'function')
238
+ this.rpcErrorHandlers.push(callback);
239
+ return this;
240
+ },
241
+ onDefaultApiError(callback) {
242
+ if (typeof callback === 'function')
243
+ this.apiErrorHandlers.push(callback);
244
+ return this;
245
+ },
246
+ onDefaultApiSuccess(callback) {
247
+ if (typeof callback === 'function')
248
+ this.apiSuccessHandlers.push(callback);
249
+ return this;
250
+ },
251
+ onFinally(callback) {
252
+ if (typeof callback === 'function')
253
+ this.finallyHandlers.push(callback);
254
+ return this;
255
+ },
256
+ onPreviewResponse(callback) {
257
+ if (typeof callback === 'function')
258
+ this.responsePreviewers.push(callback);
259
+ return this;
260
+ },
261
+ __enter() {
262
+ this.count++;
263
+ },
264
+ __preview(response) {
265
+ return this.responsePreviewers.every(callback => callback(response));
266
+ },
267
+ __rpcFail(error) {
268
+ this.rpcErrorHandlers.forEach(callback => callback(error));
269
+ },
270
+ __defaultApiFail(error) {
271
+ this.apiErrorHandlers.forEach(callback => callback(error));
272
+ },
273
+ __defaultApiSucceed(response) {
274
+ this.apiSuccessHandlers.forEach(callback => callback(response));
275
+ },
276
+ __leave() {
277
+ if (--this.count === 0)
278
+ this.finallyHandlers.forEach(callback => callback());
279
+ }
280
+ };
281
+ return ctx;
282
+ }
283
+ };
284
+ })();
package/dist/index.js CHANGED
@@ -2,9 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SetConfig = SetConfig;
4
4
  exports.GetViewObject = GetViewObject;
5
+ // @ts-ignore
6
+ const jsonrpc_2_0_1 = require("./Library/jsonrpc-2.0");
5
7
  function SetConfig(url) {
6
- RpcClient.config.url = url;
7
- RpcClient.config.cors = true;
8
+ jsonrpc_2_0_1.RpcClient.config.url = url;
9
+ jsonrpc_2_0_1.RpcClient.config.cors = true;
8
10
  }
9
11
  function GetViewObject(legislation, seller, buyer, properties, document, payments, ctx, cb) {
10
12
  CallRT("GEI.EvaluateObject1", {
@@ -18,7 +20,7 @@ function GetViewObject(legislation, seller, buyer, properties, document, payment
18
20
  }, ctx, cb);
19
21
  }
20
22
  function CallRT(method, params, ctx, cb) {
21
- RpcClient.call(method, params)
23
+ jsonrpc_2_0_1.RpcClient.call(method, params)
22
24
  .done(function (response) {
23
25
  console.log(response);
24
26
  cb(response);
@@ -26,7 +28,7 @@ function CallRT(method, params, ctx, cb) {
26
28
  .post(getStdRpcContext());
27
29
  }
28
30
  function getStdRpcContext() {
29
- return RpcClient.context().onRpcError(function (e) {
31
+ return jsonrpc_2_0_1.RpcClient.context().onRpcError(function (e) {
30
32
  }).onDefaultApiError(function (e) {
31
33
  }).onDefaultApiSuccess(function (res) {
32
34
  });
package/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- declare var RpcClient:any;
1
+ // @ts-ignore
2
+ import { RpcClient } from './Library/jsonrpc-2.0';
2
3
 
3
4
 
4
5
  export function SetConfig(url:string)
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "js-gei",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
7
- "build": "tsc"
7
+ "build": "tsc",
8
+ "dev": "vite"
8
9
  },
9
10
  "keywords": [],
10
11
  "author": "",