Skip to content

Commit

Permalink
feat: allow isReactive() to take non-object types
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Beast committed Dec 25, 2023
1 parent 6e9996d commit 4f8d8ef
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/signals/reactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export const IS_REACTIVE = Symbol("reactive");
export const ORIGINAL_REF = Symbol("original_ref");
export const CONNECTED_SIGNAL = Symbol("connected_signal");

export function isReactive<T extends object>(input: T): input is Reactive<T> {
return IS_REACTIVE in input;
export function isReactive<T>(input: T): input is Reactive<T> {
return input instanceof Object && IS_REACTIVE in input;
}

export function getConnectedSignal<T extends object>(input: T | Reactive<T>): Signal<T> {
export function getConnectedSignal<T extends object>(
input: T | Reactive<T>,
): Signal<T> {
if (isReactive(input)) {
return input[CONNECTED_SIGNAL];
}
Expand Down Expand Up @@ -116,7 +118,10 @@ export function makeMapMethodsReactive<T extends Map<unknown, unknown>, S>(
*
* When set gets in any way updated `propagate` method gets called on provided signal.
*/
export function makeSetMethodsReactive<T extends Set<unknown>, S>(set: T, signal: Signal<S>): T {
export function makeSetMethodsReactive<T extends Set<unknown>, S>(
set: T,
signal: Signal<S>,
): T {
Object.defineProperties(set, {
[IS_REACTIVE]: { value: true },
[ORIGINAL_REF]: { value: set },
Expand Down Expand Up @@ -248,19 +253,19 @@ export function makeArrayMethodsReactive<T extends Array<unknown>, S>(
*
* @returns new object, not direct reference to given object
*/
export function makeObjectPropertiesReactive<T, S>(object: T, signal: Signal<S>, watchObjectIndex = false): T {
export function makeObjectPropertiesReactive<T, S>(
object: T,
signal: Signal<S>,
watchObjectIndex = false,
): T {
if (typeof object !== "object") {
throw new Error("parameter object needs to be typeof 'object'");
}

if (Array.isArray(object)) {
makeArrayMethodsReactive(object, signal);
if (!watchObjectIndex) return object;
} else if (
!Object.getOwnPropertyDescriptor(object, IS_REACTIVE) &&
!Object.getOwnPropertyDescriptor(object, ORIGINAL_REF) &&
!Object.getOwnPropertyDescriptor(object, CONNECTED_SIGNAL)
) {
} else if (!isReactive(object)) {
Object.defineProperties(object, {
[IS_REACTIVE]: { value: true },
[ORIGINAL_REF]: { value: object },
Expand Down

0 comments on commit 4f8d8ef

Please sign in to comment.