# on(_:)

> Window events: ready, open, close, size, loadError.

Source: https://docs.tryyoursize.com/reference/ios/on/

*Method* · `TryYourSize (Swift)`

Window events: ready, open, close, size, loadError.

```swift
@discardableResult
public func on(_ handler: @escaping (Event) -> Void) -> TYSSubscription
```

## Parameters

- **`handler`** (`@escaping (Event) -> Void`, required): Called with each window event.

## Events

- **`.ready(product: String)`** (optional): The window is shown and connected.
- **`.open(product: String)`** (optional)
- **`.close(product: String)`** (optional)
- **`.size(SizeSelection)`** (optional): The shopper chose a size: preselect it.
- **`.loadError(TryYourSizeError)`** (optional): Our window could not be reached, or this app is not registered for the key.

## Returns

A subscription; `cancel()` stops the events.

## Example

**Swift**

```swift
let sub = tys.on { event in
    switch event {
    case .size(let selection): selectedSize = selection.size   // "Use this size"
    case .loadError(let error): print(error.code)
    default: break
    }
}
```

**Kotlin**

```kotlin
val sub = tys.on { event ->
    when (event) {
        is TryYourSize.Event.Size -> selected = event.selection.size
        is TryYourSize.Event.LoadError -> Log.w("tys", event.error.code.wire)
        else -> Unit
    }
}
```

**React Native**

```tsx
const sub = addEventListener((e) => {
  if (e.type === "size") setSize(e.selection.size);
  if (e.type === "loaderror") console.warn(e.code);
});
```

**Flutter**

```dart
tys.events.listen((e) {
  if (e is SizeSelected) setState(() => size = e.selection.size);
  if (e is LoadError) debugPrint(e.error.code);
});
```
