Skip to content

Commit

Permalink
list act as proxies, not wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Dec 17, 2022
1 parent 1e23865 commit 46be4fb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/houdini/src/runtime/public/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Cache as _Cache, rootID } from '../cache/cache'
import type { ListCollection as _Collection } from '../cache/lists'
import { SchemaManager, TypeInfo } from '../cache/schema'
import { ListCollection } from './list'
import { Record } from './record'
Expand Down Expand Up @@ -68,9 +69,11 @@ Please acknowledge this by setting acceptImperativeInstability to true in your c
name: Name,
{ parentID, allLists }: { parentID?: string; allLists?: boolean } = {}
): ListCollection<Def, Name> {
return new ListCollection({
return new ListCollection<Def, Name>({
cache: this,
collection: this._internal_unstable.list(name, parentID, allLists),
name,
parentID,
allLists,
})
}
}
Expand Down
71 changes: 66 additions & 5 deletions packages/houdini/src/runtime/public/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ import { Record } from './record'
import { CacheTypeDef, ListType, ValidLists, ListFilters } from './types'

export class ListCollection<Def extends CacheTypeDef, ListName extends ValidLists<Def>> {
#collection: _Collection
#parentID: string | undefined
#allLists: boolean | undefined
#when: ListFilters<Def, ListName> | undefined
#cache: Cache<Def>

constructor({ collection, cache }: { collection: _Collection; cache: Cache<Def> }) {
this.#collection = collection
#name: ValidLists<Def>

constructor({
parentID,
allLists,
when,
cache,
name,
}: {
name: ValidLists<Def>
parentID?: string
allLists?: boolean
when?: ListFilters<Def, ListName>
cache: Cache<Def>
}) {
this.#parentID = parentID
this.#allLists = allLists
this.#when = when
this.#cache = cache
this.#name = name
}

append(...records: ListType<Def, ListName>[]) {
if (!this.#collection) {
return
}

const { selection, data } = this.#listOperationPayload(records)
for (const entry of data) {
if (entry) {
Expand All @@ -24,6 +46,10 @@ export class ListCollection<Def extends CacheTypeDef, ListName extends ValidList
}

prepend(...records: ListType<Def, ListName>[]) {
if (!this.#collection) {
return
}

const { selection, data } = this.#listOperationPayload(records)
for (const entry of data) {
if (entry) {
Expand All @@ -33,6 +59,10 @@ export class ListCollection<Def extends CacheTypeDef, ListName extends ValidList
}

toggle(where: 'first' | 'last', ...records: ListType<Def, ListName>[]) {
if (!this.#collection) {
return
}

const { selection, data } = this.#listOperationPayload(records)
for (const entry of data) {
if (entry) {
Expand All @@ -42,13 +72,24 @@ export class ListCollection<Def extends CacheTypeDef, ListName extends ValidList
}

when(filter: ListFilters<Def, ListName>): ListCollection<Def, ListName> {
if (!this.#collection) {
return this
}

return new ListCollection({
collection: this.#collection.when(filter),
parentID: this.#parentID,
allLists: this.#allLists,
when: this.#when,
cache: this.#cache,
name: this.#name,
})
}

remove(...records: ListType<Def, ListName>[]) {
if (!this.#collection) {
return
}

for (const record of records) {
if (record) {
this.#collection.remove(record.idFields)
Expand All @@ -57,11 +98,31 @@ export class ListCollection<Def extends CacheTypeDef, ListName extends ValidList
}

*[Symbol.iterator]() {
if (!this.#collection) {
return
}

for (const entry of this.#collection) {
yield entry
}
}

get #collection(): _Collection | null {
try {
const list = this.#cache._internal_unstable.list(
this.#name,
this.#parentID,
this.#allLists
)
if (this.#when) {
return list.when(this.#when)
}
return list
} catch {
return null
}
}

#listOperationPayload(records: ListType<Def, ListName>[]): {
selection: SubscriptionSelection
data: GraphQLObject[]
Expand Down
6 changes: 6 additions & 0 deletions packages/houdini/src/runtime/public/tests/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,9 @@ test('can remove record from all lists', function () {
).toBeTruthy()
expect([...cache.list('All_Users')]).toHaveLength(0)
})

test('list operations fail silently if there is no matching list', function () {
const cache = testCache()
const user = cache.get('User', { id: '1' })
expect(() => cache.list('All_Pets').i).not.toThrow()
})

0 comments on commit 46be4fb

Please sign in to comment.