All files / core/src anchor.ts

100% Statements 292/292
100% Branches 108/108
100% Functions 22/22
100% Lines 292/292

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 4241x                                                       1x 1x                               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                           654x 654x 654x 654x 654x 654x 654x   654x 1x 1x     654x 1x 1x   654x 6x 6x 6x   654x 639x 639x   654x 654x 7x 639x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x   654x 2x 2x   654x 49x 1x 1x   49x 49x   49x 45x 15x 41x 29x 29x 49x 4x 4x 4x 4x 4x 4x 4x 49x 2x 2x 2x 2x 2x 2x 2x 49x       654x 8x 8x 8x   644x 99x 545x 35x 510x 67x 443x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x 654x     654x 654x     654x 654x 654x 654x   654x   654x 99x 99x 654x 102x 102x 102x   644x 644x 644x 644x 654x 654x 654x 654x   654x 654x   654x 654x 654x 654x 654x     654x 654x 654x 654x 654x 654x 654x     654x     654x 654x   1x 53x 53x 53x 53x 53x 1x 1x   52x 52x   1x 2x 2x   1x 1x 1x   1x 8x 8x   1x 3x   3x 1x 1x 1x 1x 1x 1x 1x 1x   3x 1x 1x 5x 4x   4x 3x 3x 3x 3x 4x 1x 1x 4x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x   1x 1x 1x 1x   1x 85x 85x   1x 3686x 3686x   1x   85x   85x 2x 2x 2x   85x 85x   1x 3x 3x   1x 20x   20x 20x 25x   25x 10x 10x   25x 1x 1x 1x 1x   1x 1x   25x 6x 6x 6x 6x   6x 6x   8x 25x 20x 4x 4x 4x 20x 1x 1x 1x 20x   20x 20x   1x 2x   2x 1x 1x 1x   2x 2x   1x 5x   5x 1x 1x 1x 1x 1x 1x   4x 4x   1x 16x 16x   1x 3x 3x     1x 1x 1x 1x   1x  
import { isArray, isFunction, isMap, isObject, isSet } from '@beerush/utils';
import type {
  Anchor,
  AnchorSettings,
  ArrayMutation,
  ExceptionMap,
  Immutable,
  Linkable,
  LinkableSchema,
  ModelError,
  ModelObject,
  ObjLike,
  SetMutation,
  State,
  StateController,
  StateExceptionHandlerList,
  StateGateway,
  StateGetter,
  StateMetadata,
  StateMutator,
  StateObserverList,
  StateOptions,
  StateRelation,
  StateRemover,
  StateSetter,
  StateSubscriberList,
  StateSubscriptionMap,
} from './types.js';
import { ANCHOR_SETTINGS, ARRAY_MUTATION_KEYS, COLLECTION_MUTATION_PROPS } from './constant.js';
import {
  BROADCASTER_REGISTRY,
  CONTROLLER_REGISTRY,
  EXCEPTION_HANDLER_REGISTRY,
  INIT_GATEWAY_REGISTRY,
  INIT_REGISTRY,
  META_INIT_REGISTRY,
  META_REGISTRY,
  MUTATOR_REGISTRY,
  RELATION_REGISTRY,
  SORTER_REGISTRY,
  STATE_GATEWAY_REGISTRY,
  STATE_REGISTRY,
  SUBSCRIBER_REGISTRY,
  SUBSCRIPTION_REGISTRY,
} from './registry.js';
import { linkable } from './internal.js';
import { createDestroyFactory, createLinkFactory, createSubscribeFactory, createUnlinkFactory } from './factory.js';
import { createProxyHandler, writeContract } from './proxy.js';
import { assign, clear, remove } from './helper.js';
import { shortId } from './utils/index.js';
import { createArrayMutator } from './array.js';
import { createCollectionMutator } from './collection.js';
import { captureStack } from './exception.js';
import { softClone } from './utils/clone.js';
import { getDevTool } from './dev.js';
import { createGetter, createRemover, createSetter } from './trap.js';
import { ArrayMutations, Linkables } from './enum.js';
import { createBroadcaster } from './broadcast.js';
 
/**
 * Anchors a given value, making it reactive and observable.
 *
 * This function initializes a state controller for the provided value,
 * optionally validating it against a Zod schema, and returns a proxied
 * version of the value that can be observed for changes.
 *
 * If the value is already anchored or linked, the existing anchored state is returned.
 *
 * @template T The type of the value to anchor.
 * @template S The Zod schema type for validation.
 * @param init The initial value to anchor.
 * @param schemaOptions
 * @param options Optional configuration for anchoring, including schema, strict mode, and recursive anchoring.
 * @param root - The root state's metadata.
 * @param parent - The parent state's metadata.
 * @returns The proxied, reactive version of the input value.
 * @throws If `strict` mode is enabled and schema validation fails during initialization.
 * @throws If `strict` mode is enabled and schema validation fails during property updates or array mutations.
 */
function anchorFn<T extends Linkable, S extends LinkableSchema>(
  init: T,
  schemaOptions?: S | StateOptions<S>,
  options?: StateOptions<S>,
  parent?: StateMetadata<Linkable>,
  root?: StateMetadata<Linkable>
): State<T> {
  // Return itself if the given object is a reactive state.
  if (CONTROLLER_REGISTRY.has(init)) {
    return init;
  }
 
  // Return the existing reactive state if the given init is already initialized.
  if (INIT_REGISTRY.has(init)) {
    return INIT_REGISTRY.get(init) as T;
  }
 
  if (!linkable(init)) {
    captureStack.violation.init(init, anchorFn);
    return init;
  }
 
  if (!(schemaOptions as LinkableSchema)?._zod) {
    options = schemaOptions as StateOptions<S>;
  }
 
  const cloned = options?.cloned ?? ANCHOR_SETTINGS.cloned;
  const schema = (schemaOptions as LinkableSchema)?._zod
    ? (schemaOptions as S)
    : (schemaOptions as StateOptions<S>)?.schema;
  const configs: StateOptions<S> = {
    cloned: false,
    deferred: true,
    strict: options?.strict ?? ANCHOR_SETTINGS.strict,
    ordered: (options?.ordered ?? false) && isFunction(options?.compare),
    recursive: options?.recursive ?? ANCHOR_SETTINGS.recursive,
    immutable: options?.immutable ?? ANCHOR_SETTINGS.immutable,
    observable: options?.observable ?? ANCHOR_SETTINGS.observable,
    silentInit: options?.silentInit ?? ANCHOR_SETTINGS.silentInit,
  };
  const observers: StateObserverList = new Set();
  const subscribers: StateSubscriberList<T> = new Set();
  const subscriptions: StateSubscriptionMap = new Map();
  const exceptionHandlers: StateExceptionHandlerList = new Set();
 
  if (cloned && !configs.immutable) {
    init = softClone(init, configs.recursive);
  }
 
  if (schema) {
    if (!isObject(init) && !isArray(init)) {
      captureStack.violation.schema('(object | array)', schema.type, configs.strict as false, anchorFn);
    }
 
    try {
      const result = schema.safeParse(init);
 
      if (result.success) {
        if (Array.isArray(init)) {
          init.splice(0, init.length, ...(result.data as unknown[]));
        } else if (isObject(init)) {
          Object.assign(init, result.data);
        }
      } else if (!configs.silentInit) {
        captureStack.error.validation(
          'Attempted to initialize state with schema:',
          result.error,
          configs.strict,
          anchorFn
        );
      }
    } catch (error) {
      captureStack.error.validation(
        'Something went wrong when validating schema.',
        error as Error,
        configs.strict,
        anchorFn
      );
    }
  }
 
  // Sort the initial array and register the compare function
  // if the state is marked as ordered and the given compare option is a function
  if (configs.ordered && Array.isArray(init)) {
    init.sort(options?.compare);
    SORTER_REGISTRY.set(init, options?.compare as (a: unknown, b: unknown) => number);
  }
 
  const type = isArray(init)
    ? Linkables.ARRAY
    : isSet(init)
      ? Linkables.SET
      : isMap(init)
        ? Linkables.MAP
        : Linkables.OBJECT;
  const meta: StateMetadata<T, S> = {
    id: shortId(),
    root,
    type,
    parent,
    cloned,
    schema,
    configs,
    observers,
    subscribers,
    subscriptions,
    exceptionHandlers,
  };
  META_REGISTRY.set(init, meta as never as StateMetadata);
  META_INIT_REGISTRY.set(meta as never as StateMetadata, init);
 
  // State broadcasting helpers.
  const broadcaster = createBroadcaster(init, meta);
  BROADCASTER_REGISTRY.set(init, broadcaster);
 
  // State relationship helpers.
  const link = createLinkFactory(init, meta);
  const unlink = createUnlinkFactory(meta);
  const relation: StateRelation = { link, unlink };
  RELATION_REGISTRY.set(init, relation);
 
  let mutators: ReturnType<typeof createArrayMutator> | ReturnType<typeof createCollectionMutator>;
 
  if (Array.isArray(init)) {
    mutators = createArrayMutator(init);
    MUTATOR_REGISTRY.set(init, mutators);
  } else if (init instanceof Map || init instanceof Set) {
    mutators = createCollectionMutator(init);
    MUTATOR_REGISTRY.set(init, mutators);
  }
 
  const gateway: StateGateway<T> = {
    getter: createGetter(init) as StateGetter<T>,
    setter: createSetter(init) as StateSetter<T>,
    remover: createRemover(init) as StateRemover<T>,
    mutator: mutators?.mutator as StateMutator<T>,
    broadcaster,
  };
  INIT_GATEWAY_REGISTRY.set(init, gateway as StateGateway);
 
  const proxyHandler = createProxyHandler<T>(init, gateway, meta);
  const state = new Proxy(init as ObjLike, proxyHandler) as State<T>;
 
  const controller: StateController<T, S> = {
    meta,
    destroy: createDestroyFactory(init, state, meta),
    subscribe: createSubscribeFactory(init, state, meta, { link, unlink }),
  };
 
  // Register the state with its controller for global access.
  INIT_REGISTRY.set(init, state);
  STATE_REGISTRY.set(state, init);
  CONTROLLER_REGISTRY.set(state, controller as never);
  SUBSCRIBER_REGISTRY.set(state, subscribers as never);
  SUBSCRIPTION_REGISTRY.set(state, subscriptions);
  EXCEPTION_HANDLER_REGISTRY.set(state, exceptionHandlers);
  STATE_GATEWAY_REGISTRY.set(state, gateway as StateGateway);
 
  // Trigger dev tool if it is available.
  getDevTool()?.onInit?.(init, meta);
 
  // Return the proxied state object
  return state;
}
 
anchorFn.immutable = <T extends Linkable, S extends LinkableSchema>(
  init: T,
  schemaOptions?: StateOptions<S> | S,
  options?: StateOptions<S>
): Immutable<T> => {
  if ((schemaOptions as ModelObject)?._zod) {
    return anchorFn(init, schemaOptions, { ...options, immutable: true }) as Immutable<T>;
  }
 
  return anchorFn(init, { ...schemaOptions, immutable: true }) as Immutable<T>;
};
 
anchorFn.model = ((schema, init, options) => {
  return anchorFn(init, schema, options);
}) as Anchor['model'];
 
anchorFn.flat = ((init, options) => {
  return anchorFn(init, { ...options, recursive: 'flat' });
}) as Anchor['flat'];
 
anchorFn.ordered = ((init, compare, options) => {
  return anchorFn(init, { ...options, ordered: true, compare });
}) as Anchor['ordered'];
 
anchorFn.catch = ((state, handler) => {
  const controller = CONTROLLER_REGISTRY.get(state);
 
  if (!controller) {
    const error = new Error('Object is not a state.');
    captureStack.error.external(
      'Attempted to capture exception of a state that does not exist.',
      error,
      anchorFn.destroy
    );
    return () => {};
  }
 
  if (typeof handler !== 'function') {
    const errors: ExceptionMap<ObjLike> = {};
    const unsubscribe = controller.subscribe.all((_, event) => {
      if (event.type !== 'init') {
        const key = event.keys.join('.');
 
        if (event.error) {
          exceptionMap.errors[key] = {
            error: event.error,
            message: (event.error as ModelError).issues.map((error) => error.message).join('\n'),
          };
        } else {
          delete exceptionMap.errors[key];
        }
      }
    });
    const destroy = () => {
      anchorFn.destroy(exceptionMap.errors);
      anchorFn.destroy(exceptionMap);
      unsubscribe();
    };
 
    const exceptionMap = anchorFn({ errors, destroy });
    return exceptionMap;
  }
 
  const { exceptionHandlers } = controller.meta;
  exceptionHandlers.add(handler);
 
  return () => {
    exceptionHandlers.delete(handler);
  };
}) as Anchor['catch'];
 
anchorFn.raw = ((init, options) => {
  return anchorFn(init, { ...options, cloned: false });
}) as Anchor['raw'];
 
anchorFn.has = ((state) => {
  return CONTROLLER_REGISTRY.has(state);
}) satisfies Anchor['has'];
 
anchorFn.get = ((state, silent = false) => {
  // This to make sure we can find a state that defined using write contract.
  const target = META_INIT_REGISTRY.get(CONTROLLER_REGISTRY.get(state)?.meta as StateMetadata);
 
  if (!target && !silent) {
    const error = new Error('State does not exist.');
    captureStack.error.external('Attempt to get the underlying object on non-existence state:', error, anchorFn.get);
  }
 
  return (target ?? state) as typeof state;
}) as Anchor['get'];
 
anchorFn.find = ((init) => {
  return INIT_REGISTRY.get(init) as typeof init;
}) satisfies Anchor['find'];
 
anchorFn.read = ((state) => {
  if (anchorFn.has(state)) state = anchorFn.get(state);
 
  const handler = {
    get: (target, prop, receiver) => {
      const value = Reflect.get(target, prop, receiver);
 
      if (linkable(value)) {
        return anchorFn.read(value);
      }
 
      if (ARRAY_MUTATION_KEYS.has(prop as ArrayMutations)) {
        const fn = (...args: unknown[]) => {
          captureStack.violation.methodCall(prop as ArrayMutation, fn);
          return (createArrayMutator.mock?.[prop as never] as Array<unknown>['push'])?.(target, ...args);
        };
 
        return fn;
      }
 
      if (COLLECTION_MUTATION_PROPS.has(prop as SetMutation)) {
        const fn = (...args: unknown[]) => {
          captureStack.violation.methodCall(prop as SetMutation, fn);
          return (createCollectionMutator.mock?.[prop as never] as Array<unknown>['push'])?.(target, ...args);
        };
 
        return fn;
      }
 
      return value;
    },
    set: (target, prop) => {
      captureStack.violation.setter(prop, handler.set);
      return true;
    },
    deleteProperty: (target, prop) => {
      captureStack.violation.remover(prop, handler.deleteProperty);
      return true;
    },
  } as ProxyHandler<typeof state>;
 
  return new Proxy(state, handler) as Immutable<typeof state>;
}) satisfies Anchor['read'];
 
anchorFn.snapshot = ((state, recursive = true) => {
  const target = META_INIT_REGISTRY.get(CONTROLLER_REGISTRY.get(state)?.meta as StateMetadata);
 
  if (!target) {
    const error = new Error('State does not exist.');
    captureStack.error.external('Cannot create snapshot of non-existence state.', error, anchorFn.snapshot);
  }
 
  return softClone(target ?? state, recursive) as typeof state;
}) as Anchor['snapshot'];
 
anchorFn.destroy = ((state, silent?: boolean) => {
  const controller = CONTROLLER_REGISTRY.get(state);
 
  if (!controller) {
    if (!silent) {
      const error = new Error('Object is not a state.');
      captureStack.error.external('Attempted to destroy a state that does not exist.', error, anchorFn.destroy);
    }
    return;
  }
 
  controller.destroy();
}) as Anchor['destroy'];
 
anchorFn.configure = ((config: Partial<AnchorSettings>) => {
  Object.assign(ANCHOR_SETTINGS, config);
}) as Anchor['configure'];
 
anchorFn.configs = ((): AnchorSettings => {
  return ANCHOR_SETTINGS;
}) as Anchor['configs'];
 
// Assign utility functions.
anchorFn.writable = writeContract;
anchorFn.assign = assign;
anchorFn.remove = remove;
anchorFn.clear = clear;
 
export const anchor = anchorFn as Anchor;