MCPcopy Create free account
hub / github.com/Effect-TS/effect / CollectionImpl

Class CollectionImpl

packages/effect/src/unstable/reactivity/AtomRef.ts:302–352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

300}
301
302class CollectionImpl<A> extends ReadonlyRefImpl<Array<AtomRef<A>>> implements Collection<A> {
303 constructor(items: Iterable<A>) {
304 super([])
305 for (const item of items) {
306 this.value.push(this.makeRef(item))
307 }
308 }
309
310 makeRef(value: A) {
311 const ref = new AtomRefImpl(value)
312 const notify = (value: A) => {
313 ref.notify(value)
314 this.notify(this.value)
315 }
316 return new Proxy(ref, {
317 get(target, p, _receiver) {
318 if (p === "notify") {
319 return notify
320 }
321 return target[p as keyof AtomRef<A>]
322 }
323 })
324 }
325
326 push(item: A) {
327 const ref = this.makeRef(item)
328 this.value.push(ref)
329 this.notify(this.value)
330 return this
331 }
332
333 insertAt(index: number, item: A) {
334 const ref = this.makeRef(item)
335 this.value.splice(index, 0, ref)
336 this.notify(this.value)
337 return this
338 }
339
340 remove(ref: AtomRef<A>) {
341 const index = this.value.indexOf(ref)
342 if (index !== -1) {
343 this.value.splice(index, 1)
344 this.notify(this.value)
345 }
346 return this
347 }
348
349 toArray() {
350 return this.value.map((ref) => ref.value)
351 }
352}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected