@adbl/cells 0.0.2 → 0.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.
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- /// <reference types="./types/index.d.ts" />
1
+ // <reference types="./types/index.d.ts" />
2
2
 
3
3
  export * from './library/index.js';
@@ -686,6 +686,9 @@ export class SourceCell extends Cell {
686
686
  /** @type {Partial<CellOptions<T>>} */
687
687
  options;
688
688
 
689
+ /** @type {object | undefined} */
690
+ #originalObject;
691
+
689
692
  /**
690
693
  * Creates a new Cell with the provided value.
691
694
  * @param {T} value
@@ -696,6 +699,32 @@ export class SourceCell extends Cell {
696
699
 
697
700
  this.setValue(options?.shallowProxied ? value : this.proxy(value));
698
701
  this.options = options ?? {};
702
+
703
+ if (typeof value === 'object' && value !== null) {
704
+ this.#originalObject = value;
705
+ }
706
+ }
707
+
708
+ /**
709
+ * For cells containing objects, returns the object itself.
710
+ * This can be useful in scenarios where unfettered access to the original object is needed,
711
+ * such as when using the object as a cache.
712
+ *
713
+ * @example
714
+ * const cell = new SourceCell({ a: 1, b: 2 });
715
+ * console.log(cell.originalObject); // { a: 1, b: 2 }
716
+ *
717
+ * cell.value = { a: 3, b: 4 };
718
+ * console.log(cell.originalObject); // { a: 3, b: 4 }
719
+ *
720
+ * @returns {T extends object ? T : never} The original object if T is an object, otherwise never.
721
+ */
722
+ deproxy() {
723
+ const originalObject = this.#originalObject;
724
+ if (typeof originalObject === 'object' && originalObject !== null) {
725
+ return /** @type {T extends object ? T : never} */ (originalObject);
726
+ }
727
+ throw new Error('Cannot deproxy a non-object cell.');
699
728
  }
700
729
 
701
730
  get value() {
@@ -731,6 +760,9 @@ export class SourceCell extends Cell {
731
760
  }
732
761
 
733
762
  this.setValue(this.options?.shallowProxied ? value : this.proxy(value));
763
+ if (typeof value === 'object' && value !== null) {
764
+ this.#originalObject = value;
765
+ }
734
766
  this.update();
735
767
  }
736
768
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adbl/cells",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A simple implementation of reactive updates for JavaScript",
5
5
  "main": "index.js",
6
6
  "private": false,
@@ -264,6 +264,21 @@ export class SourceCell<T> extends Cell<T> {
264
264
  constructor(value: T, options?: Partial<CellOptions<T>> | undefined);
265
265
  /** @type {Partial<CellOptions<T>>} */
266
266
  options: Partial<CellOptions<T>>;
267
+ /**
268
+ * For cells containing objects, returns the object itself.
269
+ * This can be useful in scenarios where unfettered access to the original object is needed,
270
+ * such as when using the object as a cache.
271
+ *
272
+ * @example
273
+ * const cell = new SourceCell({ a: 1, b: 2 });
274
+ * console.log(cell.originalObject); // { a: 1, b: 2 }
275
+ *
276
+ * cell.value = { a: 3, b: 4 };
277
+ * console.log(cell.originalObject); // { a: 3, b: 4 }
278
+ *
279
+ * @returns {T extends object ? T : never} The original object if T is an object, otherwise never.
280
+ */
281
+ deproxy(): T extends object ? T : never;
267
282
  /**
268
283
  * Sets the value stored in the Cell and triggers an update.
269
284
  * @param {T} value
@@ -278,6 +293,7 @@ export class SourceCell<T> extends Cell<T> {
278
293
  * @private
279
294
  */
280
295
  private proxy;
296
+ #private;
281
297
  }
282
298
  export type AsyncRequestAtoms<Input, Output> = {
283
299
  /**