Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clarify TS Docs for better DX #3076

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/ts/react-signals/src/FullStackSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import { createSetStateEvent, type StateEvent } from './events.js';
const ENDPOINT = 'SignalsHandler';

/**
* A return type for signal operations.
* A return type for signal operations that exposes a `result` property of type
* `Promise`, that resolves when the operation is completed. It allows defining
* callbacks to be run after the operation is completed, or error handling when
* the operation fails.
*
* @example
* ```ts
* const sharedName = NameService.sharedName({ defaultValue: '' });
* sharedName.replace('John').result
* .then(() => console.log('Name updated successfully'))
* .catch((error) => console.error('Failed to update the name:', error));
* ```
*/
export type Operation = {
export interface Operation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to rollback this change as types are to be preferred to interfaces.

Copy link
Contributor Author

@taefi taefi Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know about the general preference of types over interfaces, but I explained the motivation for this change internally, here: https://vaadin.slack.com/archives/C01EJJNMAC9/p1736339840532009

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDEs can definitely propose types, so it must be a glitch in your own IDE. Or maybe the solution is the export you added in index.ts. But I'd still like to keep it as a type.

result: Promise<void>;
};
}

/**
* An abstraction of a signal that tracks the number of subscribers, and calls
Expand Down Expand Up @@ -215,7 +226,7 @@ export abstract class FullStackSignal<T> extends DependencyTrackingSignal<T> {
}
>();

// creates the obejct to be returned by operations to allow defining callbacks
// creates the object to be returned by operations to allow defining callbacks
protected [$createOperation]({ id, promise }: { id?: string; promise?: Promise<void> }): Operation {
const thens = this.#operationPromises;
const promises: Array<Promise<void>> = [];
Expand Down
2 changes: 2 additions & 0 deletions packages/ts/react-signals/src/ListSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export class ListSignal<T> extends CollectionSignal<ReadonlyArray<ValueSignal<T>
/**
* Inserts a new value at the end of the list.
* @param value - The value to insert.
* @returns An operation object that allows to perform additional actions.
*/
insertLast(value: T): Operation {
const event = createInsertLastStateEvent(value);
Expand All @@ -164,6 +165,7 @@ export class ListSignal<T> extends CollectionSignal<ReadonlyArray<ValueSignal<T>
/**
* Removes the given item from the list.
* @param item - The item to remove.
* @returns An operation object that allows to perform additional actions.
*/
remove(item: ValueSignal<T>): Operation {
const entryToRemove = this.#values.get(item.id);
Expand Down
4 changes: 3 additions & 1 deletion packages/ts/react-signals/src/ValueSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class ValueSignal<T> extends FullStackSignal<T> {
* `replace` method instead.
*
* @param value - The new value.
* @returns An operation object that allows to perform additional actions.
*/
set(value: T): Operation {
const { parentClientSignalId } = this.server.config;
Expand Down Expand Up @@ -81,7 +82,8 @@ export class ValueSignal<T> extends FullStackSignal<T> {
*
* @param callback - The function that is applied on the current value to
* produce the new value.
* @returns An operation object that allows to perform additional actions, including cancellation.
* @returns An operation object that allows to perform additional actions,
* including cancellation.
*/
update(callback: (value: T) => T): OperationSubscription {
const newValue = callback(this.value);
Expand Down
1 change: 1 addition & 0 deletions packages/ts/react-signals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { ValueSignal } from './ValueSignal.js';
export { ListSignal } from './ListSignal.js';
export type { OperationSubscription } from './ValueSignal.js';
export { FullStackSignal } from './FullStackSignal.js';
export type { Operation } from './FullStackSignal.js';
Loading