|
| 1 | +import type { Objectish, Patch } from 'immer' |
| 2 | +import type { EventEmitter } from '../types/events' |
| 3 | +import { applyPatches, produce, produceWithPatches } from 'immer' |
| 4 | +import { createEventEmitter } from './events' |
| 5 | + |
| 6 | +// eslint-disable-next-line ts/no-unsafe-function-type |
| 7 | +type ImmutablePrimitive = undefined | null | boolean | string | number | Function |
| 8 | + |
| 9 | +export type Immutable<T> |
| 10 | + = T extends ImmutablePrimitive ? T |
| 11 | + : T extends Array<infer U> ? ImmutableArray<U> |
| 12 | + : T extends Map<infer K, infer V> ? ImmutableMap<K, V> |
| 13 | + : T extends Set<infer M> ? ImmutableSet<M> : ImmutableObject<T> |
| 14 | + |
| 15 | +export type ImmutableArray<T> = ReadonlyArray<Immutable<T>> |
| 16 | +export type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>> |
| 17 | +export type ImmutableSet<T> = ReadonlySet<Immutable<T>> |
| 18 | +export type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> } |
| 19 | + |
| 20 | +/** |
| 21 | + * State host that is immutable by default with explicit mutate. |
| 22 | + */ |
| 23 | +export interface SharedState<T> { |
| 24 | + /** |
| 25 | + * Get the current state. Immutable. |
| 26 | + */ |
| 27 | + get: () => Immutable<T> |
| 28 | + /** |
| 29 | + * Subscribe to state changes. |
| 30 | + */ |
| 31 | + on: EventEmitter<SharedStateEvents<T>>['on'] |
| 32 | + /** |
| 33 | + * Mutate the state. |
| 34 | + */ |
| 35 | + mutate: (fn: (state: T) => void) => void |
| 36 | + /** |
| 37 | + * Apply patches to the state. |
| 38 | + */ |
| 39 | + patch: (patches: Patch[]) => void |
| 40 | +} |
| 41 | + |
| 42 | +export interface SharedStateEvents<T> { |
| 43 | + updated: (state: T) => void |
| 44 | + patches: (patches: Patch[]) => void |
| 45 | +} |
| 46 | + |
| 47 | +export interface SharedStateOptions<T> { |
| 48 | + /** |
| 49 | + * Initial state. |
| 50 | + */ |
| 51 | + initialState: T |
| 52 | + /** |
| 53 | + * Enable patches. |
| 54 | + * |
| 55 | + * @default false |
| 56 | + */ |
| 57 | + enablePatches?: boolean |
| 58 | +} |
| 59 | + |
| 60 | +export function createSharedState<T extends Objectish>( |
| 61 | + options: SharedStateOptions<T>, |
| 62 | +): SharedState<T> { |
| 63 | + const { |
| 64 | + enablePatches = false, |
| 65 | + } = options |
| 66 | + |
| 67 | + const events = createEventEmitter<SharedStateEvents<T>>() |
| 68 | + let state = options.initialState |
| 69 | + |
| 70 | + return { |
| 71 | + on: events.on, |
| 72 | + get: () => state as Immutable<T>, |
| 73 | + patch: (patches: Patch[]) => { |
| 74 | + state = applyPatches<T>(state, patches) |
| 75 | + events.emit('updated', state) |
| 76 | + }, |
| 77 | + mutate: (fn) => { |
| 78 | + if (enablePatches) { |
| 79 | + const [newState, patches] = produceWithPatches(state, fn) |
| 80 | + state = newState |
| 81 | + events.emit('patches', patches) |
| 82 | + } |
| 83 | + else { |
| 84 | + state = produce(state, fn) |
| 85 | + } |
| 86 | + events.emit('updated', state) |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
0 commit comments