Skip to content

Comments

Refactor pushEvent promise handling and document promise support#3763

Closed
SteffenDE wants to merge 3 commits intomainfrom
sd-pushWithReply-catch
Closed

Refactor pushEvent promise handling and document promise support#3763
SteffenDE wants to merge 3 commits intomainfrom
sd-pushWithReply-catch

Conversation

@SteffenDE
Copy link
Collaborator

@SteffenDE SteffenDE commented Apr 16, 2025

EDIT: the original content of this PR was moved to #3766.

Refactors the pushEvent promise handling.

As there can be multiple targets, if no callback is passed, a promise is returned that matches the return value of
[`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#return_value).
* `handleEvent(event, (payload) => ...)` - method to handle an event pushed from the server. Returns a value that can be passed to `removeHandleEvent` to remove the event handler.
* `removeHandleEvent(ref)` - method to remove an event handler added via `handleEvent`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This one was not documented, but I think it should be

* `liveSocket` - the reference to the underlying `LiveSocket` instance
* `pushEvent(event, payload, (reply, ref) => ...)` - method to push an event from the client to the LiveView server
* `pushEvent(event, payload, (reply, ref) => ...)` - method to push an event from the client to the LiveView server.
If no callback function is passed, a promise that resolves to the reply is returned.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We now document the promise support

Comment on lines 168 to 169
As there can be multiple targets, if no callback is passed, a promise is returned that matches the return value of
[`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#return_value).
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This commit changes the Promise value, but we did not document it until now, so we should be fine hopefully

Comment on lines +261 to +263
{status: "fulfilled", value: {ref: 0, reply: {transactionID: "1001"}}},
// we only stubbed one reply
{status: "rejected", reason: expect.any(Error)}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One difference between pushEvent and pushEventTo is that for pushEvent, we only resolve the reply, but here the fulfilled value is { ref, reply }. We could map over the values and remove the ref if we want.

@SteffenDE
Copy link
Collaborator Author

We should only merge this for 1.1. For 1.0 if we want to backport, we should skip the pushEvent changes.

@SteffenDE SteffenDE force-pushed the sd-pushWithReply-catch branch 3 times, most recently from 154ae43 to 7c081f0 Compare April 16, 2025 16:52
@SteffenDE SteffenDE force-pushed the sd-pushWithReply-catch branch from 7c081f0 to 25c1f75 Compare April 16, 2025 16:52
@SteffenDE SteffenDE changed the title Catch promise rejections from pushWithReply Refactor pushEvent promise handling and document promise support Apr 16, 2025
@SteffenDE SteffenDE added this to the v1.1 milestone Apr 16, 2025
SteffenDE added a commit that referenced this pull request May 8, 2025
This got a bit bigger than expected, but it addresses multiple things:

1. includes type definitions for all public methods and interfaces. No
   need for `@types/phoenix_live_view`. The exported types are compatible
   with the currently documented type hints. Editors like VSCode now show
   detailed Intellisense.
2. rewrites some files in Typescript
3. updates eslint configuration to handle typescript
4. includes #3763
5. changes all commonjs files to modules
SteffenDE added a commit that referenced this pull request May 8, 2025
This got a bit bigger than expected, but it addresses multiple things:

1. includes type definitions for all public methods and interfaces. No
   need for `@types/phoenix_live_view`. The exported types are compatible
   with the currently documented type hints. Editors like VSCode now show
   detailed Intellisense.
2. rewrites some files in Typescript
3. updates eslint configuration to handle typescript
4. includes #3763
5. changes all commonjs files to modules
SteffenDE added a commit that referenced this pull request May 8, 2025
This got a bit bigger than expected, but it addresses multiple things:

1. includes type definitions for all public methods and interfaces. No
   need for `@types/phoenix_live_view`. The exported types are compatible
   with the currently documented type hints. Editors like VSCode now show
   detailed Intellisense.
2. rewrites some files in Typescript
3. updates eslint configuration to handle typescript
4. includes #3763
5. changes all commonjs files to modules
SteffenDE added a commit that referenced this pull request May 12, 2025
This got a bit bigger than expected, but it addresses multiple things:

1. includes type definitions for all public methods and interfaces. No
   need for `@types/phoenix_live_view`. The exported types are compatible
   with the currently documented type hints. Editors like VSCode now show
   detailed Intellisense.
2. rewrites some files in Typescript
3. updates eslint configuration to handle typescript
4. includes #3763
5. changes all commonjs files to modules
SteffenDE added a commit that referenced this pull request May 19, 2025
* Add type definitions for public interfaces

This got a bit bigger than expected, but it addresses multiple things:

1. includes type definitions for all public methods and interfaces. No
   need for `@types/phoenix_live_view`. The exported types are compatible
   with the currently documented type hints. Editors like VSCode now show
   detailed Intellisense.
2. rewrites some files in Typescript
3. updates eslint configuration to handle typescript
4. includes #3763
5. changes all commonjs files to modules

* don't rely on typescript for tests

* let's call it ViewHook

* use typescript in tests

* use prettier for js/ts formatting
@SteffenDE
Copy link
Collaborator Author

Closed via #3789 and 05e3635.

@SteffenDE SteffenDE closed this May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants