Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/ipc/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ export class IPC {
}
}

once<T>(channel: string, handler: (data: T) => void): () => void {
once<T>(channel: string, handler: (data: T) => void): () => void
once<T>(channel: string, handler: (data: T) => void, options: OnOptions<T>): () => void
once<T>(channel: string, handler: (data: T) => void, options?: OnOptions<T>): () => void {
let off: () => void
const wrapped = (data: T): void => {
off()
handler(data)
}
off = this.on(channel, wrapped)
off = options ? this.on(channel, wrapped, options) : this.on(channel, wrapped)
return off
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export declare class IPC {
on<T>(_: string, _: (_: T) => void): () => void;
on<T>(_: string, _: (_: T) => void, _: OnOptions<T>): () => void;
once<T>(_: string, _: (_: T) => void): () => void;
once<T>(_: string, _: (_: T) => void, _: OnOptions<T>): () => void;
}
// #endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class IPC {
dispose() {}
send(_, _, _) {}
on(_, _, _) {}
once(_, _) {}
once(_, _, _) {}
channelUrl(_, _) {}
sendBody(_, _, _) {}
handleReceive(_, _) {}
Expand Down
18 changes: 18 additions & 0 deletions packages/ipc/test/ipc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ describe('IPC', () => {
simulateReceive(url('test'), '42')
expect(handler).not.toHaveBeenCalled()
})

it('supports custom deserializer', () => {
const handler = vi.fn()
const deserializer = {
deserialize: (d: string) => Number.parseInt(d.replace('num:', ''), 10),
}
ipc.once('custom', handler, { deserializer })
simulateReceive(url('custom'), 'num:42')
expect(handler).toHaveBeenCalledWith(42)
})

it('with custom deserializer fires only once', () => {
const handler = vi.fn()
ipc.once('test', handler, { deserializer: { deserialize: (d: string) => d } })
simulateReceive(url('test'), 'a')
simulateReceive(url('test'), 'b')
expect(handler).toHaveBeenCalledTimes(1)
})
})

it('chunks large payloads', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ npm install @mcbe-mods/utils
```

```ts
import { Base64, color, Experience, getCubeRange, getRandomProbability, getRandomRangeValue, getSphereRange, ms2ticks, splitGroups, unique } from '@mcbe-mods/utils'
import { Base64, Color, Experience, getCubeRange, getRandomProbability, getRandomRangeValue, getSphereRange, ms2ticks, splitGroups, unique } from '@mcbe-mods/utils'

// splitGroups - split items into stacks
splitGroups(65) // => [64, 1]
splitGroups(140) // => [64, 64, 12]

// color - Minecraft formatting codes
color.green.italic.bold('Dedicated Ser') + color.reset('ver') + color.red.obfuscated('!!!')
// Color - Minecraft formatting codes
Color.green.italic.bold('Dedicated Ser') + Color.reset('ver') + Color.red.obfuscated('!!!')
// => '§a§o§lDedicated Ser§rver§c§k!!!'

// Experience - player XP calculation
Expand All @@ -44,7 +44,7 @@ getSphereRange({ x: 0, y: 0, z: 0 }, 1) // => 27 positions (sphere)
| Function | Description |
| --- | --- |
| `splitGroups(sum, groupSize?)` | Split a number into groups of a given size |
| `color` | Minecraft color/formatting code stylizer |
| `Color` | Minecraft color/formatting code stylizer |
| `Experience` | Player experience calculator (leveling up) |
| `ms2ticks(ms?, ticksPerSec?, msPerSec?)` | Convert real time to game ticks |
| `getCubeRange(location, radius?)` | Get block positions in a cube |
Expand Down
Loading