silentium-components 0.0.17 → 0.0.18

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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [0.0.18](https://github.com/silentium-lab/silentium-components/compare/v0.0.17...v0.0.18) (2025-05-21)
6
+
7
+
8
+ ### Features
9
+
10
+ * **41-issue:** shot component done ([525698f](https://github.com/silentium-lab/silentium-components/commit/525698f81e67a4280fca187c94c30d20962c5137))
11
+
5
12
  ### [0.0.17](https://github.com/silentium-lab/silentium-components/compare/v0.0.16...v0.0.17) (2025-05-19)
6
13
 
7
14
  ### [0.0.16](https://github.com/silentium-lab/silentium-components/compare/v0.0.15...v0.0.16) (2025-05-19)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "silentium-components",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/silentium-components.js",
@@ -0,0 +1,30 @@
1
+ import { patron, sourceOf, sourceSync, value } from "silentium";
2
+ import { shot } from "../behaviors/Shot";
3
+ import { expect, test, vi } from "vitest";
4
+
5
+ test("Shot.test", () => {
6
+ const baseSrc = sourceOf();
7
+ const shotSrc = sourceOf();
8
+ const result = sourceOf();
9
+ const resultSync = sourceSync(result);
10
+
11
+ const shotResult = shot(baseSrc, shotSrc);
12
+ value(shotResult, patron(result));
13
+
14
+ baseSrc.give(1);
15
+ shotSrc.give(1);
16
+
17
+ expect(resultSync.syncValue()).toBe(1);
18
+
19
+ baseSrc.give(2);
20
+
21
+ expect(resultSync.syncValue()).toBe(1);
22
+
23
+ shotSrc.give(1);
24
+
25
+ expect(resultSync.syncValue()).toBe(2);
26
+
27
+ const g = vi.fn();
28
+ value(shotResult, g);
29
+ expect(g).not.toBeCalled();
30
+ });
@@ -0,0 +1,33 @@
1
+ import {
2
+ patron,
3
+ sourceOf,
4
+ sourceResettable,
5
+ sourceSync,
6
+ SourceType,
7
+ value,
8
+ } from "silentium";
9
+
10
+ /**
11
+ * Helps to represent only last fresh value of some source, refreshing controls by shotSrc
12
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/shot
13
+ */
14
+ export const shot = <T>(
15
+ baseSrc: SourceType<T>,
16
+ shotSrc: SourceType<unknown>,
17
+ ) => {
18
+ const resetResult = sourceOf();
19
+ const result = sourceOf<T>();
20
+
21
+ const baseSrcSync = sourceSync(baseSrc, null);
22
+ value(
23
+ shotSrc,
24
+ patron(() => {
25
+ if (baseSrcSync.syncValue() !== null) {
26
+ result.give(baseSrcSync.syncValue() as T);
27
+ resetResult.give(1);
28
+ }
29
+ }),
30
+ );
31
+
32
+ return sourceResettable(result, resetResult);
33
+ };