-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
182 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,86 @@ | ||
import { useEffect } from 'react'; | ||
import { useTableContext } from '..'; | ||
import { Queue } from '../misc/queue'; | ||
|
||
const KEYS = ['sort', 'selection', 'expanded', 'hiddenColumns'] as const; | ||
|
||
export type TableStateStorage = { | ||
getItem: (key: string) => string | null | Promise<string | null>; | ||
setItem: (key: string, value: string) => unknown | Promise<unknown>; | ||
removeItem: (key: string) => unknown | Promise<unknown>; | ||
} & ( | ||
| { | ||
keys: () => string[] | Promise<string[]>; | ||
} | ||
| { | ||
length: number | (() => number | Promise<number>); | ||
key: (keyIndex: number) => string | null | Promise<string | null>; | ||
} | ||
); | ||
|
||
export function useTableStateStorage() { | ||
const state = useTableContext(); | ||
|
||
// On mount: load | ||
useEffect(() => { | ||
const { storeState, debug } = state.getState().props; | ||
if (!storeState) return; | ||
|
||
let isCanceled = false; | ||
const q = new Queue(); | ||
|
||
q.run(async () => { | ||
try { | ||
const { storage, id = 'table', include } = storeState; | ||
const json = await storage.getItem(`${id}_state`); | ||
if (isCanceled || !json) return; | ||
|
||
const data = JSON.parse(json); | ||
|
||
state.update((state) => { | ||
for (const key of KEYS) { | ||
if (!include || include[key]) { | ||
const value = key === 'sort' ? data.sort : new Set(data[key]); | ||
state[key] = value; | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
debug?.('Failed to load table state:', e); | ||
} | ||
}); | ||
|
||
state.subscribe( | ||
(state) => { | ||
if (!state.props.storeState) return; | ||
|
||
const data: any = {}; | ||
|
||
for (const key of KEYS) { | ||
if (!state.props.storeState.include || state.props.storeState.include[key]) { | ||
const value = key === 'sort' ? state.sort : Array.from(state[key]); | ||
data[key] = value; | ||
} | ||
} | ||
|
||
return { | ||
storage: state.props.storeState.storage, | ||
id: state.props.storeState.id, | ||
data, | ||
}; | ||
}, | ||
(props) => { | ||
if (!props) return; | ||
const { storage, id = 'table', data } = props; | ||
|
||
q.run(async () => { | ||
await storage.setItem(`${id}_state`, JSON.stringify(data)); | ||
}); | ||
}, | ||
); | ||
|
||
return () => { | ||
isCanceled = true; | ||
}; | ||
}, []); | ||
} |
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,28 @@ | ||
export class Queue { | ||
private q = new Array<{ job: () => Promise<any>; resolve: (value: any) => void; reject: (reason?: any) => void }>(); | ||
private isRunning = false; | ||
|
||
run<T>(job: () => Promise<T>) { | ||
return new Promise<T>((resolve, reject) => { | ||
this.q.push({ job, resolve, reject }); | ||
|
||
if (!this.isRunning) this.start(); | ||
}); | ||
} | ||
|
||
private async start() { | ||
this.isRunning = true; | ||
|
||
let next; | ||
while ((next = this.q.shift())) { | ||
try { | ||
const result = await next.job(); | ||
next.resolve(result); | ||
} catch (e) { | ||
next.reject(e); | ||
} | ||
} | ||
|
||
this.isRunning = false; | ||
} | ||
} |
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