import { type MaybePromise, PothosValidationError, type SchemaTypes } from '@pothos/core'; import type SubscriptionCache from './cache.js'; import FieldSubscriptionManager from './manager/field.js'; import TypeSubscriptionManager from './manager/type.js'; export default class CacheNode { path: string; value: unknown; fieldManager: FieldSubscriptionManager | null = null; typeManagers = new Map(); cache: SubscriptionCache; refetch: () => MaybePromise; constructor( cache: SubscriptionCache, path: string, value: unknown, refetch: () => MaybePromise, ) { this.cache = cache; this.path = path; this.value = value; this.refetch = refetch; } reRegister() { if (this.fieldManager) { this.fieldManager.reRegister(); } for (const manager of this.typeManagers.values()) { manager.reRegister(); } } managerForField() { this.fieldManager = new FieldSubscriptionManager(this.cache.manager, this); return this.fieldManager; } managerForType(key: number | string) { if (this.typeManagers.has(key)) { return null; } const typeManager = new TypeSubscriptionManager( this.cache.manager, (value) => { this.replaceValue(value, key); }, this.refetch, ); this.typeManagers.set(key, typeManager); return typeManager; } replaceValue(value: unknown, key: number | string) { if (typeof key === 'number') { const list = this.valueAsList(); this.cache.invalidPaths.push(`${this.path}.${key}`); list[key] = value; } else { this.cache.invalidPaths.push(`${this.path}.`); this.value = value; } this.typeManagers.delete(key); } private valueAsList(): unknown[] { if (Array.isArray(this.value)) { return this.value as unknown[]; } if ( typeof this.value === 'object' && this.value !== null && Symbol.iterator in this.value && typeof (this.value as Iterable)[Symbol.iterator] === 'function' ) { const list = [...(this.value as Iterable)]; this.value = list; return list; } throw new PothosValidationError( 'Expected value of CacheNode for list path to be an array or iterable', ); } }