-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
210 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './store' | ||
export * from './utils' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {ulid} from 'ulid' | ||
import {arrify} from '../../utils/arrify' | ||
import {applyInIndex} from '../applyInIndex' | ||
import {assignId} from './utils' | ||
import type {DocumentIndex, MergeObject} from '../../apply' | ||
import type {ToIdentified, ToStored} from '../applyInIndex' | ||
import type {Mutation, SanityDocumentBase} from '../../mutations/types' | ||
|
||
export type RequiredSelect<T, K extends keyof T> = Omit<T, K> & { | ||
[P in K]-?: T[P] | ||
} | ||
|
||
function update<Doc extends ToIdentified<SanityDocumentBase>>( | ||
doc: Doc, | ||
revision: string, | ||
): ToStored<Doc> { | ||
return { | ||
...doc, | ||
_rev: revision, | ||
_createdAt: doc._createdAt || new Date().toISOString(), | ||
_updatedAt: new Date().toISOString(), | ||
} | ||
} | ||
|
||
const empty: DocumentIndex<any> = {} | ||
|
||
export const createStore = <Doc extends SanityDocumentBase>( | ||
initialEntries?: Doc[], | ||
) => { | ||
let version = 0 | ||
|
||
let index: DocumentIndex<MergeObject<ToStored<Doc & SanityDocumentBase>>> = | ||
initialEntries && initialEntries?.length > 0 | ||
? Object.fromEntries( | ||
initialEntries.map(entry => { | ||
const doc = update(assignId(entry, ulid), ulid()) | ||
return [doc._id, doc] | ||
}), | ||
) | ||
: empty | ||
|
||
return { | ||
get version() { | ||
return version | ||
}, | ||
// todo: support listening for changes | ||
entries: () => Object.entries(index), | ||
get: <Id extends string>( | ||
id: Id, | ||
): MergeObject< | ||
Omit<(typeof index)[keyof typeof index], '_id'> & {_id: Id} | ||
> => index[id] as any, | ||
apply: (mutations: Mutation[] | Mutation) => { | ||
const nextIndex = applyInIndex(index, arrify(mutations)) | ||
if (nextIndex !== index) { | ||
index = nextIndex | ||
version++ | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type {StoredDocument} from '../applyInIndex' | ||
import type {SanityDocumentBase} from '../../mutations/types' | ||
|
||
export function hasId(doc: SanityDocumentBase): doc is StoredDocument { | ||
return '_id' in doc | ||
} | ||
export function assignId<Doc extends SanityDocumentBase>( | ||
doc: Doc, | ||
generateId: () => string, | ||
): Doc & {_id: string} { | ||
return hasId(doc) ? doc : {...doc, _id: generateId()} | ||
} |
Oops, something went wrong.