Skip to content

Commit 9af77a5

Browse files
committed
PR Requests, removing thenable, event promise becomes private, next event only allows minecraft signals
1 parent 3526fa6 commit 9af77a5

File tree

9 files changed

+27
-414
lines changed

9 files changed

+27
-414
lines changed

change/@minecraft-gameplay-utilities-8aec48fc-0353-4ce9-81d8-835ede162757.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"type": "major",
3-
"comment": "Event promise and thenable added to new gameplay utility library",
3+
"comment": "EventPromise and nextEvent() added to gameplay-utilities",
44
"packageName": "@minecraft/gameplay-utilities",
55
"email": "agriffin@microsoft.com",
66
"dependentChangeType": "patch"

libraries/gameplay-utilities/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22

33
A set of utilities and functions for common gameplay operations. Major pieces are covered below.
44

5-
## Thenable
5+
## nextEvent() and EventPromise
66

7-
A promise-like object which allows for cancellation through external resolution with it's `fulfill` and `reject` functions.
8-
9-
## EventThenable
10-
11-
This object provides a "wait for next event" utility. A wrapper around the `Thenable` object which is designed to be used with Minecraft script event signals. Provide the constructor with a signal and it will resolve the promise when the next event for the provided signal is raised. Also provides a `cancel` function to unregister the event and fulfill the promise with `undefined`.
7+
`nextEvent()` is a function which takes a Minecraft event signal and wraps a promise around the next event being raised. The function returns an `EventPromise` object which is a promise type. When the event is raised, the promise will resolve with the event data, and unsubscribe from the event's signal. The `EventPromise` type also adds a `cancel()` function which will unsubscribe from the event's signal, and fulfill the promise with `undefined`.
128

139
### Can be awaited to receive the event
1410

1511
```ts
16-
const event = await new EventThenable(world.afterEvents.buttonPush);
12+
const event = await nextEvent(world.afterEvents.buttonPush);
1713
```
1814

1915
### Can be used like a promise
2016

2117
```ts
22-
new EventThenable(world.afterEvents.leverAction).then(
18+
await nextEvent(world.afterEvents.leverAction).then(
2319
(event) => {
2420
// do something with the event
2521
}).finally(() => {
@@ -30,7 +26,7 @@ new EventThenable(world.afterEvents.leverAction).then(
3026
### Optionally provide filters for the signal and use helper function
3127

3228
```ts
33-
const creeperDeathEvent = await waitForNextEvent(world.afterEvents.entityDie, { entityTypes: ['minecraft:creeper'] });
29+
const creeperDeathEvent = await nextEvent(world.afterEvents.entityDie, { entityTypes: ['minecraft:creeper'] });
3430
```
3531

3632
## How to use @minecraft/gameplay-utilities in your project

libraries/gameplay-utilities/api-report/gameplay-utilities.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class Thenable<T> {
4040
}
4141

4242
// @public
43-
export function waitForNextEvent<T, U>(signal: EventSignal<T, U>, filter?: U): EventThenable<T, U>;
43+
export function nextEvent<T, U>(signal: EventSignal<T, U>, filter?: U): EventThenable<T, U>;
4444

4545
// (No @packageDocumentation comment for this package)
4646

libraries/gameplay-utilities/src/events/eventThenable.ts renamed to libraries/gameplay-utilities/src/events/eventPromise.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { Thenable } from '../thenable.js';
4+
import { world, system } from '@minecraft/server';
55

66
/**
7-
* Interface representing the functions required to subscribe and unsubscribe to events.
8-
*
7+
* A promise wrapper utility which returns a new promise that will resolve when the next
8+
* event is raised.
9+
*
910
* @public
1011
*/
11-
export interface EventSignal<T, U> {
12-
subscribe(closure: (event: T) => void, filter?: U): (event: T) => void;
13-
unsubscribe(closure: (event: T) => void): void;
12+
export interface EventPromise {
13+
/**
14+
* Cancels the promise and unsubscribes from the event signal. Cancellation is done
15+
* by fulfilling with undefined.
16+
*
17+
* @public
18+
*/
19+
cancel(): void;
1420
}
1521

1622
/**
17-
* Helper to create a new EventThenable from an event signal.
23+
* Helper to create a new EventPromise from an after event signal.
1824
*
1925
* @public
2026
*/
21-
export function waitForNextEvent<T, U>(signal: EventSignal<T, U>, filter?: U) {
22-
return new EventThenable(signal, filter);
27+
export function nextEvent<U>(signal: (typeof world.afterEvents[keyof typeof world.afterEvents]) | (typeof system.afterEvents[keyof typeof system.afterEvents]), filter?: U) : EventPromise {
28+
return new EventPromiseImpl(signal, filter);
2329
}
2430

2531
/**
2632
* A promise wrapper utility which returns a new promise that will resolve when the next
2733
* event is raised.
2834
*
29-
* @public
35+
* @private
3036
*/
31-
export class EventThenable<T, U = undefined> extends Thenable<T | undefined> {
37+
class EventPromiseImpl<T, U = undefined> extends Promise<T | undefined> {
3238
private onCancel?: () => void;
3339

34-
constructor(signal: EventSignal<T, U>, filter?: U) {
40+
constructor(signal: typeof world.afterEvents[keyof typeof world.afterEvents], filter?: U) {
3541
let cancelFn: (() => void) | undefined = undefined;
3642
super((resolve, _) => {
3743
let sub: (event: T) => void;
@@ -61,8 +67,6 @@ export class EventThenable<T, U = undefined> extends Thenable<T | undefined> {
6167
* Cancels the promise by resolving it with undefined and unsubscribing from the event signal.
6268
*/
6369
cancel() {
64-
if (this.onCancel) {
65-
this.onCancel();
66-
}
70+
this.onCancel?.();
6771
}
6872
}

libraries/gameplay-utilities/src/events/eventThenable.test.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
export * from './eventThenable.js';
4+
export * from './eventPromise.js';

libraries/gameplay-utilities/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
// Licensed under the MIT License.
33

44
export * from './events/index.js';
5-
export * from './thenable.js';

libraries/gameplay-utilities/src/thenable.test.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)