chem-rx 0.0.21 → 0.0.22

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.cjs.js CHANGED
@@ -307,6 +307,23 @@ function useAtom(atom) {
307
307
  return value;
308
308
  }
309
309
 
310
+ function useSignal(signal, callback, id) {
311
+ react.useEffect(function () {
312
+ // Assuming the signal might not have an initial value method like `atom.value()`,
313
+ // If your signal class has a method to get the current/latest value, use it here to initialize.
314
+
315
+ var subscription;
316
+ if (callback) {
317
+ subscription = signal.subscribe(callback, id);
318
+ }
319
+ return function () {
320
+ if (subscription) {
321
+ subscription.unsubscribe();
322
+ }
323
+ };
324
+ }, [signal]);
325
+ }
326
+
310
327
  function useSelectAtom(atom, key) {
311
328
  var _useState = react.useState(atom.get(key)),
312
329
  value = _useState[0],
@@ -378,3 +395,4 @@ exports.Signal = Signal;
378
395
  exports.hydrateAtoms = hydrateAtoms;
379
396
  exports.useAtom = useAtom;
380
397
  exports.useSelectAtom = useSelectAtom;
398
+ exports.useSignal = useSignal;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Atom } from "./Atom";
2
2
  export { useAtom } from "./useAtom";
3
+ export { useSignal } from "./useSignal";
3
4
  export { useSelectAtom } from "./useSelectAtom";
4
5
  export { hydrateAtoms } from "./useHydrateAtoms";
5
6
  export { Signal } from "./Signal";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
@@ -305,6 +305,23 @@ var chemicalRx = (function (exports, rxjs, react) {
305
305
  return value;
306
306
  }
307
307
 
308
+ function useSignal(signal, callback, id) {
309
+ react.useEffect(function () {
310
+ // Assuming the signal might not have an initial value method like `atom.value()`,
311
+ // If your signal class has a method to get the current/latest value, use it here to initialize.
312
+
313
+ var subscription;
314
+ if (callback) {
315
+ subscription = signal.subscribe(callback, id);
316
+ }
317
+ return function () {
318
+ if (subscription) {
319
+ subscription.unsubscribe();
320
+ }
321
+ };
322
+ }, [signal]);
323
+ }
324
+
308
325
  function useSelectAtom(atom, key) {
309
326
  var _useState = react.useState(atom.get(key)),
310
327
  value = _useState[0],
@@ -376,6 +393,7 @@ var chemicalRx = (function (exports, rxjs, react) {
376
393
  exports.hydrateAtoms = hydrateAtoms;
377
394
  exports.useAtom = useAtom;
378
395
  exports.useSelectAtom = useSelectAtom;
396
+ exports.useSignal = useSignal;
379
397
 
380
398
  return exports;
381
399
 
package/dist/index.js CHANGED
@@ -225,6 +225,23 @@ function useAtom(atom) {
225
225
  return value;
226
226
  }
227
227
 
228
+ function useSignal(signal, callback, id) {
229
+ useEffect(() => {
230
+ // Assuming the signal might not have an initial value method like `atom.value()`,
231
+ // If your signal class has a method to get the current/latest value, use it here to initialize.
232
+
233
+ let subscription;
234
+ if (callback) {
235
+ subscription = signal.subscribe(callback, id);
236
+ }
237
+ return () => {
238
+ if (subscription) {
239
+ subscription.unsubscribe();
240
+ }
241
+ };
242
+ }, [signal]);
243
+ }
244
+
228
245
  function useSelectAtom(atom, key) {
229
246
  const [value, setValue] = useState(atom.get(key));
230
247
  useEffect(() => {
@@ -281,4 +298,4 @@ class Signal {
281
298
  // Optionally, implement unsubscribe logic to manage subscriptions.
282
299
  }
283
300
 
284
- export { Atom, Signal, hydrateAtoms, useAtom, useSelectAtom };
301
+ export { Atom, Signal, hydrateAtoms, useAtom, useSelectAtom, useSignal };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chem-rx",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "description": "react state primitives powered by rx.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Atom } from "./Atom";
2
2
  export { useAtom } from "./useAtom";
3
+ export { useSignal } from "./useSignal";
3
4
  export { useSelectAtom } from "./useSelectAtom";
4
5
  export { hydrateAtoms } from "./useHydrateAtoms";
5
6
  export { Signal } from "./Signal";