# EventContainer

`com.jsmacrosce.jsmacros.core.language.EventContainer`

**Since:** 1.4.0

Tracks an event delivery's lock and callbacks queued until its release. A delivery may be joined or read-only.

## Type Parameters

- `T`: associated script-context type

## Constructors

### new EventContainer {#com.jsmacrosce.jsmacros.core.language.EventContainer_constructor-T-}

```js
new EventContainer(T extends BaseScriptContext<?> ctx)
```

Stores the supplied context reference and starts locked with no lock thread or queued callbacks.  
 A `null` context is accepted, but the first [releaseLock()](#com.jsmacrosce.jsmacros.core.language.EventContainer_releaseLock-) call then fails with [NullPointerException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NullPointerException.html).

**Parameters:**
- `ctx`: context associated with this container; may be `null`

## Methods

### awaitLock {#com.jsmacrosce.jsmacros.core.language.EventContainer_awaitLock-Runnable-}

```js
awaitLock(Runnable then): void
```

Waits once while locked, queueing a non-null callback; the wait may return spuriously.  
 If already released, runs the callback immediately. Callback failures are logged.

**Parameters:**
- `then`: callback to run on release; when called from a thread bound to the context, a non-null
             callback must be a [MethodWrapper](../MethodWrapper.md). May be `null` while locked, but must be non-null if already released.

**Since:** 1.4.0

### getCtx {#com.jsmacrosce.jsmacros.core.language.EventContainer_getCtx-}

```js
getCtx(): T extends BaseScriptContext<?>
```

Returns the associated script context, or `null` if none was supplied.

**Returns:** associated context, possibly `null`

### getLockThread {#com.jsmacrosce.jsmacros.core.language.EventContainer_getLockThread-}

```js
getLockThread(): Thread
```

Returns the associated thread, or `null` before assignment.

**Returns:** lock thread

### isLocked {#com.jsmacrosce.jsmacros.core.language.EventContainer_isLocked-}

```js
isLocked(): boolean
```

Returns whether this container's event lock is still held.

**Returns:** `true` until the first release

### releaseLock {#com.jsmacrosce.jsmacros.core.language.EventContainer_releaseLock-}

```js
releaseLock(): void
```

Releases this lock once, runs queued callbacks, wakes waiters, and removes its thread association from the context.  
 If still locked, a null context causes [NullPointerException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NullPointerException.html); an already released lock is a no-op.  
 Callback failures are logged and do not prevent later callbacks from running.

**Since:** 1.4.0

### setLockThread {#com.jsmacrosce.jsmacros.core.language.EventContainer_setLockThread-Thread-}

```js
setLockThread(Thread lockThread): void
```

Sets the lock thread; while locked and when a context is present, registers this container in its event map.  
 A non-null thread can be assigned only while no non-null thread has been assigned; assigning `null` leaves it assignable.

**Parameters:**
- `lockThread`: thread associated with the lock; may be `null`

### toString {#com.jsmacrosce.jsmacros.core.language.EventContainer_toString-}

```js
toString(): String
```

Returns a diagnostic string containing the current lock state and lock-thread name; it is not a serialization format.  
 Thread names are not escaped. With no assigned thread, the name is `"null"`; for example, `ContextContainer:{"locked": true, "lockThread": "null"}`.

**Returns:** diagnostic text in the form `ContextContainer:{"locked": boolean, "lockThread": "name"}`
