Skip to content

Commit

Permalink
feat: performFailures option
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Dec 13, 2024
1 parent 68a825f commit f92fc90
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## master

- Add `performFailures: 'throw' | 'warn' | 'ignore'` option to `createCable()`. ([@palkan][])

## 0.9.1 (2024-07-31)

- Add `info` event to Cable and Channel. ([@palkan][])
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,23 @@ export default createCable({
});
```

## Other configuration options

There is a plenty of options available to configure the client. Please, refer to the full list in the [TS definition file](./packages/core/cable/index.d.ts).
Here are the most popular/useful ones (and not yet mentioned in the docs):

```js
import { createCable } from '@anycable/core'

// Below you can find some options and their default values
const cable = createCable({
logLevel: 'info', // use 'debug' for more verbose output and troubleshooting
performFailures: 'throw', // indicates how to treat channel.perform(...) failures; use `warn` or 'ignore' to silence errors
lazy: true, // if true, the connection would be established only when the first subscription is made
logger: console, // custom logger (must implement `log`, `info`, `warn`, `error`, `debug` methods)
})
```

## Further reading

- [Architecture](./docs/architecture.md)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add `performFailures: 'throw' | 'warn' | 'ignore'` option to `createCable()`. ([@palkan][])

## 0.9.1 (2024-07-31)

- Add `info` event to Cable and Channel. ([@palkan][])
Expand Down
1 change: 1 addition & 0 deletions packages/core/cable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type CableOptions = {
logger?: Logger
lazy?: boolean
hubOptions?: HubOptions
performFailures?: 'throw' | 'warn' | 'ignore'
}

export type CableState =
Expand Down
26 changes: 25 additions & 1 deletion packages/core/cable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,21 @@ export class PubSubChannel extends Channel {
export const STATE = Symbol('state')

export class Cable {
constructor({ transport, protocol, encoder, logger, lazy, hubOptions }) {
constructor({
transport,
protocol,
encoder,
logger,
lazy,
hubOptions,
performFailures
}) {
this.emitter = createNanoEvents()
this.transport = transport
this.encoder = encoder
this.logger = logger || new NoopLogger()
this.protocol = protocol
this.performFailures = performFailures || 'throw'

this.protocol.attached(this)

Expand Down Expand Up @@ -566,6 +575,21 @@ export class Cable {
}

async perform(identifier, action, payload) {
if (this.performFailures === 'throw') {
return this._perform(identifier, action, payload)
}

try {
return await this._perform(identifier, action, payload)
} catch (err) {
if (this.performFailures === 'warn') {
this.logger.warn('perform failed', { error: err })
}
return undefined
}
}

async _perform(identifier, action, payload) {
if (this.state === 'connecting') {
await this.pendingConnect()
}
Expand Down
36 changes: 32 additions & 4 deletions packages/core/cable/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../index.js'
import { TestTransport } from '../transport/testing'
import { TestLogger } from '../logger/testing'
import { PUBSUB_CHANNEL, PubSubChannel } from './index.js'
import { PUBSUB_CHANNEL, PubSubChannel, CableOptions } from './index.js'

class TestProtocol implements Protocol {
cable!: Cable
Expand Down Expand Up @@ -101,18 +101,22 @@ let transport: TestTransport
let logger: TestLogger
let cable: Cable
let encoder: Encoder
let cableOptions: CableOptions

beforeEach(() => {
logger = new TestLogger()
transport = new TestTransport('ws:///')
encoder = new JSONEncoder()
protocol = new TestProtocol()
cable = new Cable({

cableOptions = {
protocol,
encoder,
logger,
transport
})
}

cable = new Cable(cableOptions)
})

describe('initialize', () => {
Expand Down Expand Up @@ -887,9 +891,33 @@ describe('channels', () => {

cable.closed()

return expect(
await expect(
cable.perform(channel.identifier, 'do', { foo: 'bar' })
).rejects.toEqual(Error('No connection'))

let cable2 = new Cable({ ...cableOptions, performFailures: 'warn' })
cable2.connect()
cable2.connected()
let channel2 = cable2.subscribeTo('test')
await channel2.ensureSubscribed()
cable2.closed()

await expect(
cable2.perform(channel.identifier, 'do', { foo: 'bar' })
).resolves.toBeUndefined()
expect(logger.warnings).toHaveLength(1)

let cable3 = new Cable({ ...cableOptions, performFailures: 'ignore' })
cable3.connect()
cable3.connected()
let channel3 = cable3.subscribeTo('test')
await channel3.ensureSubscribed()
cable3.closed()

await expect(
cable3.perform(channel.identifier, 'do', { foo: 'bar' })
).resolves.toBeUndefined()
expect(logger.warnings).toHaveLength(1)
})

it('send when closed', () => {
Expand Down

0 comments on commit f92fc90

Please sign in to comment.