MCPcopy Index your code
hub / github.com/TanStack/db / createMockCollection

Function createMockCollection

packages/angular-db/tests/inject-live-query.test.ts:67–165  ·  view source on GitHub ↗
(
  initial: Array<T & Record<`id`, K>> = [],
  initialStatus: CollectionStatus = `ready`,
)

Source from the content-addressed store, hash-verified

65}
66
67function createMockCollection<T extends object, K extends string | number>(
68 initial: Array<T & Record<`id`, K>> = [],
69 initialStatus: CollectionStatus = `ready`,
70): Collection<T, K, Record<string, never>> &
71 NonSingleResult & {
72 __setStatus: (s: CollectionStatus) => void
73 __replaceAll: (rows: Array<T & Record<`id`, K>>) => void
74 __upsert: (row: T & Record<`id`, K>) => void
75 __delete: (key: K) => void
76 } {
77 const map = new Map<K, T>()
78 for (const r of initial) {
79 map.set(r.id, r)
80 }
81
82 let status: CollectionStatus = initialStatus
83 const subs = new Set<(changes: Array<any>) => void>()
84 const readySubs = new Set<() => void>()
85 const id = `mock-col-` + Math.random().toString(36).slice(2)
86
87 const notify = (changes: Array<any> = []) => {
88 for (const cb of subs) cb(changes)
89 }
90
91 const notifyReady = () => {
92 for (const cb of readySubs) cb()
93 }
94
95 const api: any = {
96 id,
97 get status() {
98 return status
99 },
100 entries: () => Array.from(map.entries()),
101 values: () => Array.from(map.values()),
102 get: (key: K) => map.get(key),
103 has: (key: K) => map.has(key),
104 size: () => map.size,
105 subscribeChanges: (cb: (changes: Array<any>) => void) => {
106 subs.add(cb)
107 return {
108 unsubscribe: () => subs.delete(cb),
109 }
110 },
111 onFirstReady: (cb: () => void) => {
112 if (status === `ready`) {
113 setTimeout(cb, 0)
114 } else {
115 readySubs.add(cb)
116 }
117 return () => readySubs.delete(cb)
118 },
119 preload: () => Promise.resolve(),
120 startSyncImmediate: () => {
121 const wasNotReady = status !== `ready`
122 if (status === `idle`) {
123 status = `ready`
124 }

Callers 1

Calls 11

notifyFunction · 0.85
fromMethod · 0.80
setMethod · 0.45
toStringMethod · 0.45
entriesMethod · 0.45
valuesMethod · 0.45
getMethod · 0.45
hasMethod · 0.45
addMethod · 0.45
deleteMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…