Skip to content

Memorize

Used to cache the execution results of listeners, returning the cached result directly for the same input, avoiding repeated calculations.

Usage

typescript
import { memorize } from 'fastevent/pipes'; 
emitter.on(
    'calculate',
    (msg) => {
        return expensiveCalculation(msg.payload);
    },
    {
        pipes: [memorize()],
    },
);

Custom Cache Logic

ts
import { memorize } from 'fastevent/pipes'; 

let counter = 0
const mockFn = vi.fn().mockImplementation(() => ++counter)
const predicate = vi.fn().mockImplementation((message) => message.payload === 'use-cache')

emitter.on('test', mockFn, {
    pipes: [memorize(predicate)]
})

// First call, direct execution
const [promise1] = emitter.emit('test', 'use-cache')
const result1 = await promise1

// Second call, predicate returns true, should use cache
const [promise2] = emitter.emit('test', 'use-cache')
const result2 = await promise2

// Third call, predicate returns false, should not use cache
const [promise3] = emitter.emit('test', 'no-cache')
const result3 = await promise3

expect(result1).toBe(1)
expect(result2).toBe(1) // Using cache
expect(result3).toBe(2) // Re-executed
expect(mockFn).toHaveBeenCalledTimes(2)
expect(predicate).toHaveBeenCalledTimes(2)

Note

By default, memorize determines whether to use the cache based on the payload.