Skip to content

Commit

Permalink
feat(utils): export attachSetter
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Apr 15, 2024
1 parent 2aca8a6 commit 8035ea9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 63 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Expand Up @@ -16,6 +16,15 @@ export type {
ValSubscriber,
ValVersion,
} from "./typings";
export { arrayShallowEqual, identity, isVal, strictEqual } from "./utils";
export {
arrayShallowEqual,
attachSetter,
identity,
isVal,
reaction,
setValue,
strictEqual,
subscribe,
unsubscribe,
} from "./utils";
export { groupVals, readonlyVal, val } from "./val";
export { reaction, setValue, subscribe, unsubscribe } from "./value-enhancer";
62 changes: 59 additions & 3 deletions src/utils.ts
@@ -1,10 +1,66 @@
import type {
ReadonlyVal,
Val,
ValDisposer,
ValInputsValueTuple,
ValSubscriber,
ValVersion,
} from "./typings";

/**
* @deprecated
* Set the value of a val.
* It works for both `Val` and `ReadonlyVal` type (if the `ReadonlyVal` is actually a `Val`).
* Throws error if the val is really `ReadonlyVal`.
*/
export const setValue = <TValue>(
val: ReadonlyVal<TValue>,
value: TValue
): void => {
try {
(val as Val<TValue>).set?.(value);
} catch {
// ignore
}
};

/**
* Subscribe to value changes with immediate emission.
* @param val
* @param subscriber
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously.
* @returns a disposer function that cancels the subscription
*/
export const subscribe = <TValue>(
val: ReadonlyVal<TValue>,
subscriber: ValSubscriber<TValue>,
eager?: boolean
): ValDisposer => val.subscribe(subscriber, eager);

/**
* Subscribe to value changes without immediate emission.
* @param val
* @param subscriber
* @param eager by default subscribers will be notified on next tick. set `true` to notify subscribers of value changes synchronously.
* @returns a disposer function that cancels the subscription
*/
export const reaction = <TValue>(
val: ReadonlyVal<TValue>,
subscriber: ValSubscriber<TValue>,
eager?: boolean
): ValDisposer => val.reaction(subscriber, eager);

/**
* Remove the given subscriber.
* Remove all if no subscriber provided.
* @param val
* @param subscriber
*/
export const unsubscribe = <TValue>(
val: ReadonlyVal<TValue>,
subscriber?: (...args: any[]) => any
): void => val.unsubscribe(subscriber);

/** Returns the value passed in. */
export const identity = <TValue>(value: TValue): TValue => value;

Expand Down Expand Up @@ -62,12 +118,12 @@ export const invoke = <TValue>(
};

/**
* Make a readonly Val writable by providing a set method
* Attach a new setter to a val.
* @param val$ a readonly Val
* @param set a function that sets the value of val$
* @returns The same val$ but writable
* @returns The same val$ with the new setter.
*/
export const makeWritable = <TValue>(
export const attachSetter = <TValue>(
val$: ReadonlyVal<TValue>,
set: (this: void, value: TValue) => void
): Val<TValue> => (((val$ as Val<TValue>).set = set), val$ as Val<TValue>);
Expand Down
6 changes: 3 additions & 3 deletions src/val.ts
Expand Up @@ -10,7 +10,7 @@ import type {
} from "./typings";

import { SubscriberMode, Subscribers } from "./subscribers";
import { invoke, makeWritable, strictEqual } from "./utils";
import { attachSetter, invoke, strictEqual } from "./utils";

/**
* Bare minimum implementation of a readonly val.
Expand Down Expand Up @@ -62,7 +62,7 @@ export class ValImpl<TValue = any> implements ReadonlyVal<TValue> {

public ref(writable?: boolean): ReadonlyVal<TValue> {
const val$ = new ValRefImpl(this, this.#config);
return writable ? makeWritable(val$, this.set) : val$;
return writable ? attachSetter(val$, this.set) : val$;
}

public reaction(
Expand Down Expand Up @@ -251,7 +251,7 @@ export function val<TValue = any>(
config?: ValConfig<TValue>
): Val<NoInfer<TValue | undefined>> {
const [val$, set] = readonlyVal(value, config);
return makeWritable(val$, set);
return attachSetter(val$, set);
}

/**
Expand Down
55 changes: 0 additions & 55 deletions src/value-enhancer.ts

This file was deleted.

0 comments on commit 8035ea9

Please sign in to comment.