silentium 0.0.165 → 0.0.167

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.
@@ -150,6 +150,12 @@ declare class Rejections {
150
150
  destroy(): this;
151
151
  }
152
152
 
153
+ /**
154
+ * Silence is null or undefined or duplicated values
155
+ * Everything else is not silence
156
+ */
157
+ declare function Silence<T>(resolve: ConstructorType<[T]>): (v: T) => void;
158
+
153
159
  /**
154
160
  * Resolver that does nothing with the passed value,
155
161
  * needed for silent message triggering
@@ -190,28 +196,49 @@ declare function Applied<const T, R>(base: MaybeMessage<T>, applier: Constructor
190
196
  declare function AppliedDestructured<const T extends any[], R>($base: MaybeMessage<T>, applier: ConstructorType<any[], R>): MessageRx<R>;
191
197
 
192
198
  /**
193
- * A component that allows creating linked objects of information and its owner
194
- * in such a way that if a new value is assigned to the owner, this value
195
- * will become the value of the linked information source
196
- * https://silentium-lab.github.io/silentium/#/en/information/of
199
+ * Helps represent an message as a primitive type, which can be useful
200
+ * for cases when you need to always have a reference to the current value
201
+ * without updating the shared value when the current one changes.
202
+ * For example, this could be used when passing an authorization token.
203
+ * It can also be useful for testing or logging purposes.
197
204
  */
198
- declare function Late<T>(v?: T): LateImpl<T>;
199
- declare class LateImpl<T> implements MessageSourceType<T> {
200
- private v?;
201
- private rejections;
202
- private lateR;
203
- private notify;
204
- constructor(v?: T | undefined);
205
- then(r: ConstructorType<[T]>): this;
205
+ declare function Primitive<T>($base: MessageType<T>, theValue?: T | null): PrimitiveImpl<T>;
206
+ declare class PrimitiveImpl<T> {
207
+ private $base;
208
+ private theValue;
209
+ private touched;
210
+ constructor($base: MessageType<T>, theValue?: T | null);
211
+ private ensureTouched;
212
+ [Symbol.toPrimitive](): T | null;
213
+ primitive(): T | null;
214
+ primitiveWithException(): T & ({} | undefined);
215
+ }
216
+
217
+ /**
218
+ * An information object that helps multiple owners access
219
+ * a single another information object
220
+ */
221
+ declare function Shared<T>($base: MessageType<T> | MessageSourceType<T>): SharedImpl<T>;
222
+ declare class SharedImpl<T> implements MessageSourceType<T>, ChainableType<T> {
223
+ private $base;
224
+ private resolver;
225
+ private lastV;
226
+ private resolvers;
227
+ private source?;
228
+ constructor($base: MessageType<T> | MessageSourceType<T>);
229
+ then(resolved: ConstructorType<[T]>): this;
206
230
  use(value: T): this;
207
231
  catch(rejected: ConstructorType<[unknown]>): this;
232
+ destroy(): this;
233
+ value(): PrimitiveImpl<T>;
234
+ chain(m: MessageType<T>): this;
208
235
  }
209
236
 
210
237
  /**
211
238
  * Message with error catched
212
239
  * inside another message
213
240
  */
214
- declare function Catch<T>($base: MessageType): LateImpl<T>;
241
+ declare function Catch<T>($base: MessageType): SharedImpl<T>;
215
242
 
216
243
  type Last<T extends readonly any[]> = T extends readonly [...infer _, infer L] ? L : never;
217
244
  /**
@@ -310,50 +337,23 @@ declare function Freeze<T>($base: MessageType<T>, $invalidate?: MessageType<T>):
310
337
  declare function FromEvent<T>(emitter: MaybeMessage<any>, eventName: MaybeMessage<string>, subscribeMethod: MaybeMessage<string>, unsubscribeMethod?: MaybeMessage<string>): MessageRx<T>;
311
338
 
312
339
  /**
313
- * Helps represent an message as a primitive type, which can be useful
314
- * for cases when you need to always have a reference to the current value
315
- * without updating the shared value when the current one changes.
316
- * For example, this could be used when passing an authorization token.
317
- * It can also be useful for testing or logging purposes.
318
- */
319
- declare function Primitive<T>($base: MessageType<T>, theValue?: T | null): PrimitiveImpl<T>;
320
- declare class PrimitiveImpl<T> {
321
- private $base;
322
- private theValue;
323
- private touched;
324
- constructor($base: MessageType<T>, theValue?: T | null);
325
- private ensureTouched;
326
- [Symbol.toPrimitive](): T | null;
327
- primitive(): T | null;
328
- primitiveWithException(): T & ({} | undefined);
329
- }
330
-
331
- /**
332
- * An information object that helps multiple owners access
333
- * a single another information object
340
+ * A component that allows creating linked objects of information and its owner
341
+ * in such a way that if a new value is assigned to the owner, this value
342
+ * will become the value of the linked information source
343
+ * https://silentium-lab.github.io/silentium/#/en/information/of
334
344
  */
335
- declare function Shared<T>($base: MessageType<T> | MessageSourceType<T>): SharedImpl<T>;
336
- declare class SharedImpl<T> implements MessageSourceType<T>, ChainableType<T> {
337
- private $base;
338
- private resolver;
339
- private lastV;
340
- private resolvers;
341
- private source?;
342
- constructor($base: MessageType<T> | MessageSourceType<T>);
343
- then(resolved: ConstructorType<[T]>): this;
345
+ declare function Late<T>(v?: T): SharedImpl<T>;
346
+ declare class LateImpl<T> implements MessageSourceType<T> {
347
+ private v?;
348
+ private rejections;
349
+ private lateR;
350
+ private notify;
351
+ constructor(v?: T | undefined);
352
+ then(r: ConstructorType<[T]>): this;
344
353
  use(value: T): this;
345
354
  catch(rejected: ConstructorType<[unknown]>): this;
346
- destroy(): this;
347
- value(): PrimitiveImpl<T>;
348
- chain(m: MessageType<T>): this;
349
355
  }
350
356
 
351
- /**
352
- * An message with a value that will be set later,
353
- * capable of responding to many resolvers
354
- */
355
- declare function LateShared<T>(value?: T): SharedImpl<T>;
356
-
357
357
  /**
358
358
  * Component that applies an info object constructor to each data item,
359
359
  * producing an information source with new values
@@ -404,4 +404,4 @@ declare function isDestroyable(o: unknown): o is DestroyableType;
404
404
  */
405
405
  declare function isDestroyed(o: unknown): o is DestroyedType;
406
406
 
407
- export { ActualMessage, All, Any, Applied, AppliedDestructured, Catch, Chain, Chainable, ChainableImpl, Computed, type ConstructorType, Context, ContextChain, ContextOf, type ContextType, DestroyContainer, DestroyContainerImpl, Destroyable, DestroyableImpl, type DestroyableType, type DestroyedType, Empty, EmptyImpl, ExecutorApplied, Filtered, Freeze, FromEvent, Late, LateImpl, LateShared, Local, Map$1 as Map, type MaybeMessage, Message, type MessageExecutorType, MessageRx, MessageSource, MessageSourceImpl, type MessageSourceType, type MessageType, type MessageTypeValue, New, Nothing, Of, Once, Primitive, PrimitiveImpl, Process, Rejections, Sequence, Shared, SharedImpl, type SourceType, Stream, Void, ensureFunction, ensureMessage, isDestroyable, isDestroyed, isFilled, isMessage, isSource };
407
+ export { ActualMessage, All, Any, Applied, AppliedDestructured, Catch, Chain, Chainable, ChainableImpl, Computed, type ConstructorType, Context, ContextChain, ContextOf, type ContextType, DestroyContainer, DestroyContainerImpl, Destroyable, DestroyableImpl, type DestroyableType, type DestroyedType, Empty, EmptyImpl, ExecutorApplied, Filtered, Freeze, FromEvent, Late, LateImpl, Local, Map$1 as Map, type MaybeMessage, Message, type MessageExecutorType, MessageRx, MessageSource, MessageSourceImpl, type MessageSourceType, type MessageType, type MessageTypeValue, New, Nothing, Of, Once, Primitive, PrimitiveImpl, Process, Rejections, Sequence, Shared, SharedImpl, Silence, type SourceType, Stream, Void, ensureFunction, ensureMessage, isDestroyable, isDestroyed, isFilled, isMessage, isSource };
package/dist/silentium.js CHANGED
@@ -80,6 +80,16 @@ class Rejections {
80
80
  }
81
81
  }
82
82
 
83
+ function Silence(resolve) {
84
+ let lastValue;
85
+ return (v) => {
86
+ if (isFilled(v) && v !== lastValue) {
87
+ lastValue = v;
88
+ resolve(v);
89
+ }
90
+ };
91
+ }
92
+
83
93
  function ensureFunction(v, label) {
84
94
  if (typeof v !== "function") {
85
95
  throw new Error(`${label}: is not function`);
@@ -106,7 +116,7 @@ class MessageRx {
106
116
  }
107
117
  then(resolve) {
108
118
  try {
109
- this.dc.add(this.executor(resolve, this.rejections.reject));
119
+ this.dc.add(this.executor(Silence(resolve), this.rejections.reject));
110
120
  } catch (e) {
111
121
  this.rejections.reject(e);
112
122
  }
@@ -253,16 +263,109 @@ function AppliedDestructured($base, applier) {
253
263
 
254
264
  var __defProp$3 = Object.defineProperty;
255
265
  var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
256
- var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
266
+ var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, key + "" , value);
267
+ function Primitive($base, theValue = null) {
268
+ return new PrimitiveImpl($base, theValue);
269
+ }
270
+ class PrimitiveImpl {
271
+ constructor($base, theValue = null) {
272
+ this.$base = $base;
273
+ this.theValue = theValue;
274
+ __publicField$3(this, "touched", false);
275
+ }
276
+ ensureTouched() {
277
+ if (!this.touched) {
278
+ this.$base.then((v) => {
279
+ this.theValue = v;
280
+ });
281
+ }
282
+ this.touched = true;
283
+ }
284
+ [Symbol.toPrimitive]() {
285
+ this.ensureTouched();
286
+ return this.theValue;
287
+ }
288
+ primitive() {
289
+ this.ensureTouched();
290
+ return this.theValue;
291
+ }
292
+ primitiveWithException() {
293
+ this.ensureTouched();
294
+ if (this.theValue === null) {
295
+ throw new Error("Primitive value is null");
296
+ }
297
+ return this.theValue;
298
+ }
299
+ }
300
+
301
+ var __defProp$2 = Object.defineProperty;
302
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
303
+ var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
304
+ function Shared($base) {
305
+ return new SharedImpl($base);
306
+ }
307
+ class SharedImpl {
308
+ constructor($base) {
309
+ this.$base = $base;
310
+ __publicField$2(this, "resolver", (v) => {
311
+ this.lastV = v;
312
+ this.resolvers.forEach((r) => {
313
+ r(v);
314
+ });
315
+ });
316
+ __publicField$2(this, "lastV");
317
+ __publicField$2(this, "resolvers", /* @__PURE__ */ new Set());
318
+ __publicField$2(this, "source");
319
+ if (isSource($base)) {
320
+ this.source = $base;
321
+ }
322
+ }
323
+ then(resolved) {
324
+ this.resolvers.add(resolved);
325
+ if (this.resolvers.size === 1) {
326
+ this.$base.then(this.resolver);
327
+ } else if (isFilled(this.lastV)) {
328
+ resolved(this.lastV);
329
+ }
330
+ return this;
331
+ }
332
+ use(value) {
333
+ if (this.source) {
334
+ this.source.use(value);
335
+ } else {
336
+ this.resolver(value);
337
+ }
338
+ return this;
339
+ }
340
+ catch(rejected) {
341
+ this.$base.catch(rejected);
342
+ return this;
343
+ }
344
+ destroy() {
345
+ this.resolvers.clear();
346
+ return this;
347
+ }
348
+ value() {
349
+ return Primitive(this);
350
+ }
351
+ chain(m) {
352
+ m.then(this.use.bind(this));
353
+ return this;
354
+ }
355
+ }
356
+
357
+ var __defProp$1 = Object.defineProperty;
358
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
359
+ var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
257
360
  function Late(v) {
258
- return new LateImpl(v);
361
+ return Shared(new LateImpl(v));
259
362
  }
260
363
  class LateImpl {
261
364
  constructor(v) {
262
365
  this.v = v;
263
- __publicField$3(this, "rejections", new Rejections());
264
- __publicField$3(this, "lateR", null);
265
- __publicField$3(this, "notify", () => {
366
+ __publicField$1(this, "rejections", new Rejections());
367
+ __publicField$1(this, "lateR", null);
368
+ __publicField$1(this, "notify", () => {
266
369
  if (isFilled(this.v) && this.lateR) {
267
370
  try {
268
371
  this.lateR(this.v);
@@ -278,7 +381,7 @@ class LateImpl {
278
381
  "Late component gets new resolver, when another was already connected!"
279
382
  );
280
383
  }
281
- this.lateR = r;
384
+ this.lateR = Silence(r);
282
385
  this.notify();
283
386
  return this;
284
387
  }
@@ -370,106 +473,8 @@ function ContextChain(base) {
370
473
  };
371
474
  }
372
475
 
373
- var __defProp$2 = Object.defineProperty;
374
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
375
- var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, key + "" , value);
376
- function Primitive($base, theValue = null) {
377
- return new PrimitiveImpl($base, theValue);
378
- }
379
- class PrimitiveImpl {
380
- constructor($base, theValue = null) {
381
- this.$base = $base;
382
- this.theValue = theValue;
383
- __publicField$2(this, "touched", false);
384
- }
385
- ensureTouched() {
386
- if (!this.touched) {
387
- this.$base.then((v) => {
388
- this.theValue = v;
389
- });
390
- }
391
- this.touched = true;
392
- }
393
- [Symbol.toPrimitive]() {
394
- this.ensureTouched();
395
- return this.theValue;
396
- }
397
- primitive() {
398
- this.ensureTouched();
399
- return this.theValue;
400
- }
401
- primitiveWithException() {
402
- this.ensureTouched();
403
- if (this.theValue === null) {
404
- throw new Error("Primitive value is null");
405
- }
406
- return this.theValue;
407
- }
408
- }
409
-
410
- var __defProp$1 = Object.defineProperty;
411
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
412
- var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
413
- function Shared($base) {
414
- return new SharedImpl($base);
415
- }
416
- class SharedImpl {
417
- constructor($base) {
418
- this.$base = $base;
419
- __publicField$1(this, "resolver", (v) => {
420
- this.lastV = v;
421
- this.resolvers.forEach((r) => {
422
- r(v);
423
- });
424
- });
425
- __publicField$1(this, "lastV");
426
- __publicField$1(this, "resolvers", /* @__PURE__ */ new Set());
427
- __publicField$1(this, "source");
428
- if (isSource($base)) {
429
- this.source = $base;
430
- }
431
- }
432
- then(resolved) {
433
- this.resolvers.add(resolved);
434
- if (this.resolvers.size === 1) {
435
- this.$base.then(this.resolver);
436
- } else if (isFilled(this.lastV)) {
437
- resolved(this.lastV);
438
- }
439
- return this;
440
- }
441
- use(value) {
442
- if (this.source) {
443
- this.source.use(value);
444
- } else {
445
- this.resolver(value);
446
- }
447
- return this;
448
- }
449
- catch(rejected) {
450
- this.$base.catch(rejected);
451
- return this;
452
- }
453
- destroy() {
454
- this.resolvers.clear();
455
- return this;
456
- }
457
- value() {
458
- return Primitive(this);
459
- }
460
- chain(m) {
461
- m.then(this.use.bind(this));
462
- return this;
463
- }
464
- }
465
-
466
- function LateShared(value) {
467
- const l = Late(value);
468
- return Shared(l);
469
- }
470
-
471
476
  function ContextOf(transport) {
472
- const $msg = LateShared();
477
+ const $msg = Late();
473
478
  Context.transport.set(transport, $msg.use.bind($msg));
474
479
  return Message((resolve, reject) => {
475
480
  $msg.catch(reject);
@@ -501,7 +506,7 @@ function Empty($base) {
501
506
  class EmptyImpl {
502
507
  constructor($base) {
503
508
  this.$base = $base;
504
- __publicField(this, "$empty", LateShared());
509
+ __publicField(this, "$empty", Late());
505
510
  }
506
511
  message() {
507
512
  Shared(this.$base).then((v) => {
@@ -616,7 +621,7 @@ function Once($base) {
616
621
 
617
622
  function Process($base, builder) {
618
623
  return Message((resolve, reject) => {
619
- const $res = LateShared();
624
+ const $res = Late();
620
625
  const dc = DestroyContainer();
621
626
  $base.then((v) => {
622
627
  dc.destroy();
@@ -639,7 +644,7 @@ function Sequence($base) {
639
644
  $base.catch(reject);
640
645
  $base.then((v) => {
641
646
  result.push(v);
642
- resolve(result);
647
+ resolve(result.slice());
643
648
  });
644
649
  });
645
650
  }
@@ -656,5 +661,5 @@ function Stream(base) {
656
661
  });
657
662
  }
658
663
 
659
- export { ActualMessage, All, Any, Applied, AppliedDestructured, Catch, Chain, Chainable, ChainableImpl, Computed, Context, ContextChain, ContextOf, DestroyContainer, DestroyContainerImpl, Destroyable, DestroyableImpl, Empty, EmptyImpl, ExecutorApplied, Filtered, Freeze, FromEvent, Late, LateImpl, LateShared, Local, Map$1 as Map, Message, MessageRx, MessageSource, MessageSourceImpl, New, Nothing, Of, Once, Primitive, PrimitiveImpl, Process, Rejections, Sequence, Shared, SharedImpl, Stream, Void, ensureFunction, ensureMessage, isDestroyable, isDestroyed, isFilled, isMessage, isSource };
664
+ export { ActualMessage, All, Any, Applied, AppliedDestructured, Catch, Chain, Chainable, ChainableImpl, Computed, Context, ContextChain, ContextOf, DestroyContainer, DestroyContainerImpl, Destroyable, DestroyableImpl, Empty, EmptyImpl, ExecutorApplied, Filtered, Freeze, FromEvent, Late, LateImpl, Local, Map$1 as Map, Message, MessageRx, MessageSource, MessageSourceImpl, New, Nothing, Of, Once, Primitive, PrimitiveImpl, Process, Rejections, Sequence, Shared, SharedImpl, Silence, Stream, Void, ensureFunction, ensureMessage, isDestroyable, isDestroyed, isFilled, isMessage, isSource };
660
665
  //# sourceMappingURL=silentium.js.map