Skip to content
Merged

V2 #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
452 changes: 136 additions & 316 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue-cache-store",
"type": "module",
"version": "1.3.0",
"version": "2.0.0",
"packageManager": "[email protected]",
"description": "Cache and re-use computed/reactive properties in vue",
"author": {
Expand Down Expand Up @@ -66,7 +66,7 @@
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.5.4",
"vitest": "^3.2.4",
"vitest": "3.2.4",
"vue": "^3.5.17"
},
"repository": {
Expand Down
107 changes: 0 additions & 107 deletions src/defineCacheStore.ts

This file was deleted.

54 changes: 0 additions & 54 deletions src/defineRecordStore.ts

This file was deleted.

16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export {
defineCacheStore,
type CacheStore,
type CacheStoreFactory,
type GenericCacheStore,
type GenericCacheStoreFactory,
} from './defineCacheStore'
export { defineRecordStore, watchRecordStore } from './defineRecordStore'
export { type Options } from './storeOptions'
makeRecordStore,
type GenericRecordStore,
type RecordStore,
} from './makeRecordStore'

export {
watchRecordStore,
} from './watchRecordStore'
62 changes: 62 additions & 0 deletions src/makeRecordStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { type Reactive, reactive, type ToRefs } from 'vue'
import { reactiveToRefs } from './reactiveToRefs'

export type RecordStore<ID extends NonNullable<any>, T extends object> = {
// get cached ids
ids(): ID[],
// get reactive object
get(id: ID): Reactive<T>,
// get refs wrapped object like pinia's storeToRefs(useMyStore())
getRefs(id: ID): ToRefs<Reactive<T>>,
// check if id is cached
has(id: ID): boolean,
// remove cached id
remove(id: ID): void,
// clear all cache ids
clear(): void,
// loop over each cached item
forEach(callbackFunction: (value: Reactive<T>, key: ID) => void): void;
}

export type GenericRecordStore = ReturnType<typeof makeRecordStore>

export function makeRecordStore<
ID extends NonNullable<any>,
T extends object,
>(creatorFunction: (id: ID, context: RecordStore<ID, T>) => T) {
type ReactiveResult = Reactive<T>

const cache = new Map<ID, ReactiveResult>()

const get = (id: ID): ReactiveResult => {
let result = cache.get(id)
if (result) {
return result
}

const object = creatorFunction(id, context)
result = reactive(object) as ReactiveResult
cache.set(id, result)

return result
}

const getRefs = (id: ID) => {
const obj = get(id) as ReactiveResult
return reactiveToRefs(obj)
}

const context: RecordStore<ID, T> = {
ids: () => [...cache.keys()],
get,
getRefs,
has: (id: ID) => cache.has(id),
remove: (id: ID) => cache.delete(id),
clear: () => cache.clear(),
forEach: (callbackFunction: (value: ReactiveResult, key: ID) => void) => {
cache.forEach(callbackFunction)
}
}

return context
}
2 changes: 1 addition & 1 deletion src/reactiveToRefs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// based on pinia storeToRefs()
import { computed, isReactive, isRef, toRaw, toRef, type ToRefs } from 'vue'

// based on pinia storeToRefs()
export function reactiveToRefs<T extends object>(obj: T) {
const rawStore = toRaw(obj)
const refs = {}
Expand Down
37 changes: 0 additions & 37 deletions src/storeOptions.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/watchRecordStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { computed, watch } from 'vue'
import { makeRecordStore, type RecordStore } from './makeRecordStore'

export function watchRecordStore<
ID extends NonNullable<any> = Parameters<Parameters<typeof watchRecordStore>[0]>[0],
R extends object = NonNullable<ReturnType<Parameters<typeof watchRecordStore>[0]>>,
T extends object = ReturnType<Parameters<typeof watchRecordStore>[1]>,
>
(
getRecord: (id: ID) => R | undefined,
create: (record: R, context: RecordStore<ID, T>) => T,
) {
type Result = RecordStore<ID, T>

const creatorFunction = (id: ID, context: Result) => {

const comp = computed(() => getRecord(id))
watch(comp, () => {
if (!comp.value) {
context.remove(id)
}
})

const record = comp.value
if (!record) {
throw new Error(`watchRecordStore(): Record id "${id}" not found.`)
}
return create(record, context)
}

return makeRecordStore<ID, T>(creatorFunction)
}
Loading
Loading