polkadot-api 1.7.7 → 1.7.9

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/dist/index.js CHANGED
@@ -125,11 +125,10 @@ const compatibilityHelper = (descriptors, getDescriptorEntryPoint, getRuntimeEnt
125
125
  descriptors,
126
126
  (threshold, runtime) => getCompatibilityLevel(runtime) >= threshold
127
127
  );
128
- const waitDescriptors = () => descriptors;
129
- const compatibleRuntime$ = (chainHead, hash) => rxjs.combineLatest([waitDescriptors(), chainHead.getRuntimeContext$(hash)]);
128
+ const compatibleRuntime$ = (chainHead, hash) => rxjs.combineLatest([descriptors, chainHead.getRuntimeContext$(hash)]);
130
129
  const withCompatibleRuntime = (chainHead, mapper) => (source$) => rxjs.combineLatest([
131
130
  source$.pipe(chainHead.withRuntime(mapper)),
132
- waitDescriptors()
131
+ descriptors
133
132
  ]).pipe(rxjs.map(([[x, ctx], descriptors2]) => [x, descriptors2, ctx]));
134
133
  const argsAreCompatible = (descriptors2, ctx, args) => {
135
134
  if (descriptors2 instanceof RuntimeToken) return true;
@@ -161,7 +160,7 @@ const compatibilityHelper = (descriptors, getDescriptorEntryPoint, getRuntimeEnt
161
160
  isCompatible,
162
161
  getCompatibilityLevel,
163
162
  getCompatibilityLevels,
164
- waitDescriptors,
163
+ descriptors,
165
164
  withCompatibleRuntime,
166
165
  compatibleRuntime$,
167
166
  argsAreCompatible,
@@ -180,7 +179,7 @@ const withOptionalToken = (compatibilityToken, fn) => (...args) => {
180
179
 
181
180
  const createConstantEntry = (palletName, name, {
182
181
  valuesAreCompatible,
183
- waitDescriptors,
182
+ descriptors,
184
183
  isCompatible,
185
184
  getCompatibilityLevel
186
185
  }) => {
@@ -209,7 +208,7 @@ const createConstantEntry = (palletName, name, {
209
208
  );
210
209
  return value;
211
210
  }
212
- return waitDescriptors().then(fn);
211
+ return descriptors.then(fn);
213
212
  };
214
213
  return Object.assign(fn, { isCompatible, getCompatibilityLevel });
215
214
  };
@@ -248,130 +247,46 @@ function firstValueFromWithSignal(source, signal) {
248
247
  });
249
248
  }
250
249
 
251
- const concatMapEager = (mapper, concurrent = Infinity) => (source$) => new rxjs.Observable((observer) => {
252
- let topSubscription;
253
- const queues = /* @__PURE__ */ new Map();
254
- const innerSubscriptions = /* @__PURE__ */ new Map();
255
- const results = /* @__PURE__ */ new Map();
256
- let mapperIdx = 0;
257
- let subscriptionIdx = 0;
258
- let observerIdx = 0;
259
- const nextSubscription = () => {
260
- const inner$ = queues.get(subscriptionIdx);
261
- if (!inner$) {
262
- if (innerSubscriptions.size === 0 && (typeof topSubscription === "undefined" || topSubscription.closed)) {
263
- observer.complete();
264
- }
265
- return;
266
- }
267
- const idx = subscriptionIdx++;
268
- queues.delete(idx);
269
- if (observerIdx !== idx) {
270
- results.set(idx, []);
271
- }
272
- innerSubscriptions.set(
273
- idx,
274
- inner$.subscribe({
275
- next(x) {
276
- if (observerIdx === idx) {
277
- observer.next(x);
278
- } else {
279
- results.get(idx).push(x);
280
- }
281
- },
282
- complete() {
283
- innerSubscriptions.delete(idx);
284
- if (idx === observerIdx) {
285
- observerIdx++;
286
- while (results.has(observerIdx)) {
287
- results.get(observerIdx).forEach((x) => observer.next(x));
288
- results.delete(observerIdx);
289
- if (innerSubscriptions.has(observerIdx)) {
290
- break;
291
- }
292
- observerIdx++;
293
- }
294
- }
295
- nextSubscription();
296
- },
297
- error(e) {
298
- observer.error(e);
299
- }
300
- })
301
- );
302
- };
303
- topSubscription = source$.subscribe({
304
- next(outterValue) {
305
- const idx = mapperIdx++;
306
- queues.set(
307
- idx,
308
- rxjs.defer(() => mapper(outterValue, idx))
309
- );
310
- if (innerSubscriptions.size < concurrent) {
311
- nextSubscription();
312
- }
313
- },
314
- error(e) {
315
- observer.error(e);
316
- },
317
- complete() {
318
- if (innerSubscriptions.size === 0) {
319
- observer.complete();
320
- }
321
- }
322
- });
323
- return () => {
324
- innerSubscriptions.forEach((subscription) => subscription.unsubscribe());
325
- topSubscription.unsubscribe();
326
- queues.clear();
327
- results.clear();
328
- };
329
- });
330
-
331
- const raceMap = (mapper, concurrent) => (source$) => new rxjs.Observable((observer) => {
332
- let innerSubscriptions = new Array();
333
- let isOuterDone = false;
334
- const createSubscription = (value) => {
335
- const sub = new rxjs.Subscription();
336
- innerSubscriptions.push(sub);
337
- if (innerSubscriptions.length > concurrent) {
338
- innerSubscriptions[0].unsubscribe();
339
- innerSubscriptions = innerSubscriptions.slice(1);
340
- }
341
- const subscription = mapper(value).subscribe({
342
- next(value2) {
343
- const index = innerSubscriptions.indexOf(sub);
344
- innerSubscriptions.slice(0, index).forEach((s) => s.unsubscribe());
345
- innerSubscriptions = innerSubscriptions.slice(index);
346
- observer.next(value2);
250
+ const EMPTY_VALUE = Symbol("EMPTY_VALUE");
251
+ const lossLessExhaustMap = (mapper) => (source$) => new rxjs.Observable((observer) => {
252
+ let innerSubscription = null;
253
+ let queuedValue = EMPTY_VALUE;
254
+ let isOutterDone = false;
255
+ const setInnerSubscription = () => {
256
+ const observable = mapper(queuedValue);
257
+ queuedValue = EMPTY_VALUE;
258
+ innerSubscription = observable.subscribe({
259
+ next(vv) {
260
+ observer.next(vv);
347
261
  },
348
- error(error) {
349
- observer.error(error);
262
+ error(ee) {
263
+ observer.error(ee);
350
264
  },
351
265
  complete() {
352
- const index = innerSubscriptions.indexOf(sub);
353
- innerSubscriptions.splice(index, 1);
354
- if (innerSubscriptions.length === 0 && isOuterDone)
355
- observer.complete();
266
+ if (queuedValue !== EMPTY_VALUE) setInnerSubscription();
267
+ else {
268
+ innerSubscription = null;
269
+ if (isOutterDone) observer.complete();
270
+ }
356
271
  }
357
272
  });
358
- sub.add(subscription);
359
273
  };
360
- const outerSubscription = source$.subscribe({
361
- next(value) {
362
- createSubscription(value);
274
+ const subscription = source$.subscribe({
275
+ next(v) {
276
+ queuedValue = v;
277
+ if (!innerSubscription) setInnerSubscription();
363
278
  },
364
- error(err) {
365
- observer.error(err);
279
+ error(e) {
280
+ observer.error(e);
366
281
  },
367
282
  complete() {
368
- if (innerSubscriptions.length === 0) observer.complete();
369
- isOuterDone = true;
283
+ if (!innerSubscription) observer.complete();
284
+ isOutterDone = true;
370
285
  }
371
286
  });
372
287
  return () => {
373
- outerSubscription.unsubscribe();
374
- innerSubscriptions.forEach((sub) => sub.unsubscribe());
288
+ innerSubscription?.unsubscribe();
289
+ subscription.unsubscribe();
375
290
  };
376
291
  });
377
292
 
@@ -420,7 +335,7 @@ const createEventEntry = (pallet, name, chainHead, {
420
335
  if (!argsAreCompatible(runtime, ctx, null)) throw compatibilityError();
421
336
  return [block, runtime, ctx];
422
337
  }),
423
- concatMapEager(
338
+ observableClient.concatMapEager(
424
339
  ([block, runtime, ctx]) => chainHead.eventsAt$(block.hash).pipe(
425
340
  rxjs.map((events) => {
426
341
  const winners = events.filter(
@@ -486,12 +401,12 @@ const createRuntimeCallEntry = (api, method, chainHead, {
486
401
  return Object.assign(fn, { getCompatibilityLevel, isCompatible });
487
402
  };
488
403
 
404
+ const toMapped = rxjs.map((x) => x.mapped);
489
405
  const createStorageEntry = (pallet, name, chainHead, {
490
406
  isCompatible,
491
407
  getCompatibilityLevel,
492
408
  getCompatibilityLevels,
493
- waitDescriptors,
494
- withCompatibleRuntime,
409
+ descriptors: descriptorsPromise,
495
410
  argsAreCompatible,
496
411
  valuesAreCompatible
497
412
  }) => {
@@ -513,48 +428,29 @@ const createStorageEntry = (pallet, name, chainHead, {
513
428
  const getCodec = (ctx) => {
514
429
  try {
515
430
  return ctx.dynamicBuilder.buildStorage(pallet, name);
516
- } catch {
431
+ } catch (e) {
517
432
  throw new Error(`Runtime entry Storage(${pallet}.${name}) not found`);
518
433
  }
519
434
  };
520
435
  const watchValue = (...args) => {
521
436
  const target = args[args.length - 1];
522
- const actualArgs = target === "best" || target === "finalized" ? args.slice(0, -1) : args;
523
- if (isSystemNumber)
524
- return chainHead.bestBlocks$.pipe(
525
- rxjs.map((blocks) => blocks.at(target === "best" ? 0 : -1).number),
526
- rxjs.distinctUntilChanged(),
527
- bigIntOrNumber
528
- );
529
- return chainHead[target === "best" ? "best$" : "finalized$"].pipe(
530
- rxjs.debounceTime(0),
531
- withCompatibleRuntime(chainHead, (x) => x.hash),
532
- raceMap(([block, runtime, ctx]) => {
533
- const codecs = getCodec(ctx);
534
- if (!argsAreCompatible(runtime, ctx, actualArgs))
535
- throw incompatibleError();
536
- return chainHead.storage$(block.hash, "value", () => codecs.enc(...actualArgs)).pipe(
537
- rxjs.map((val) => {
538
- if (!valuesAreCompatible(runtime, ctx, val))
539
- throw incompatibleError();
540
- return { val, codecs };
541
- })
542
- );
543
- }, 4),
544
- rxjs.distinctUntilChanged((a, b) => a.val === b.val),
545
- rxjs.map(
546
- ({ val, codecs }) => val === null ? codecs.fallback : codecs.dec(val)
547
- )
437
+ const isBest = target === "best";
438
+ const actualArgs = isBest || target === "finalized" ? args.slice(0, -1) : args;
439
+ return chainHead[isBest ? "best$" : "finalized$"].pipe(
440
+ lossLessExhaustMap(
441
+ () => getRawValue$(...actualArgs, isBest ? { at: "best" } : {})
442
+ ),
443
+ rxjs.distinctUntilChanged((a, b) => a.raw === b.raw),
444
+ toMapped
548
445
  );
549
446
  };
550
- const getValue = async (...args) => {
447
+ const getRawValue$ = (...args) => {
551
448
  const lastArg = args[args.length - 1];
552
449
  const isLastArgOptional = isOptionalArg(lastArg);
553
- const { signal, at: _at } = isLastArgOptional ? lastArg : {};
450
+ const { at: _at } = isLastArgOptional ? lastArg : {};
554
451
  const at = _at ?? null;
555
- let result$;
556
- if (isSystemNumber) {
557
- result$ = chainHead.bestBlocks$.pipe(
452
+ if (isSystemNumber)
453
+ return chainHead.bestBlocks$.pipe(
558
454
  rxjs.map((blocks) => {
559
455
  if (at === "finalized" || !at) return blocks.at(-1);
560
456
  if (at === "best") return blocks.at(0);
@@ -565,39 +461,50 @@ const createStorageEntry = (pallet, name, chainHead, {
565
461
  return block.number;
566
462
  }),
567
463
  rxjs.distinctUntilChanged(),
568
- bigIntOrNumber
569
- );
570
- } else {
571
- const descriptors = await waitDescriptors();
572
- result$ = chainHead.storage$(
573
- at,
574
- "value",
575
- (ctx) => {
576
- const codecs = getCodec(ctx);
577
- const actualArgs = args.length === codecs.len ? args : args.slice(0, -1);
578
- if (args !== actualArgs && !isLastArgOptional) throw invalidArgs(args);
579
- if (!argsAreCompatible(descriptors, ctx, actualArgs))
580
- throw incompatibleError();
581
- return codecs.enc(...actualArgs);
582
- },
583
- null,
584
- (data, ctx) => {
585
- const codecs = getCodec(ctx);
586
- const value = data === null ? codecs.fallback : codecs.dec(data);
587
- if (!valuesAreCompatible(descriptors, ctx, value))
588
- throw incompatibleError();
589
- return value;
590
- }
464
+ bigIntOrNumber,
465
+ rxjs.map((mapped) => ({ raw: "", mapped }))
591
466
  );
592
- }
593
- return firstValueFromWithSignal(result$, signal);
467
+ return rxjs.from(descriptorsPromise).pipe(
468
+ rxjs.mergeMap(
469
+ (descriptors) => chainHead.storage$(
470
+ at,
471
+ "value",
472
+ (ctx) => {
473
+ const codecs = getCodec(ctx);
474
+ const actualArgs = args.length === codecs.len ? args : args.slice(0, -1);
475
+ if (args !== actualArgs && !isLastArgOptional)
476
+ throw invalidArgs(args);
477
+ if (!argsAreCompatible(descriptors, ctx, actualArgs))
478
+ throw incompatibleError();
479
+ return codecs.enc(...actualArgs);
480
+ },
481
+ null,
482
+ (data, ctx) => {
483
+ const codecs = getCodec(ctx);
484
+ const value = data === null ? codecs.fallback : codecs.dec(data);
485
+ if (!valuesAreCompatible(descriptors, ctx, value))
486
+ throw incompatibleError();
487
+ return value;
488
+ }
489
+ )
490
+ )
491
+ );
492
+ };
493
+ const getValue = async (...args) => {
494
+ const lastArg = args[args.length - 1];
495
+ const isLastArgOptional = isOptionalArg(lastArg);
496
+ const { signal } = isLastArgOptional ? lastArg : {};
497
+ return firstValueFromWithSignal(
498
+ getRawValue$(...args).pipe(toMapped),
499
+ signal
500
+ );
594
501
  };
595
502
  const getEntries = async (...args) => {
596
503
  const lastArg = args[args.length - 1];
597
504
  const isLastArgOptional = isOptionalArg(lastArg);
598
505
  const { signal, at: _at } = isLastArgOptional ? lastArg : {};
599
506
  const at = _at ?? null;
600
- const descriptors = await waitDescriptors();
507
+ const descriptors = await descriptorsPromise;
601
508
  const result$ = chainHead.storage$(
602
509
  at,
603
510
  "descendantsValues",
@@ -624,7 +531,7 @@ const createStorageEntry = (pallet, name, chainHead, {
624
531
  throw incompatibleError();
625
532
  return decodedValues;
626
533
  }
627
- );
534
+ ).pipe(toMapped);
628
535
  return firstValueFromWithSignal(result$, signal);
629
536
  };
630
537
  const getValues = (keyArgs, options) => Promise.all(