Add type annotations to TimelineFragmentStore
This commit is contained in:
parent
3ec222eae3
commit
526ff53728
1 changed files with 26 additions and 12 deletions
|
@ -17,17 +17,31 @@ limitations under the License.
|
||||||
import { StorageError } from "../../common";
|
import { StorageError } from "../../common";
|
||||||
import {KeyLimits} from "../../common";
|
import {KeyLimits} from "../../common";
|
||||||
import { encodeUint32 } from "../utils";
|
import { encodeUint32 } from "../utils";
|
||||||
|
import {Store} from "../Store";
|
||||||
|
|
||||||
function encodeKey(roomId, fragmentId) {
|
interface Fragment {
|
||||||
|
roomId: string
|
||||||
|
id: number
|
||||||
|
previousId: number | null
|
||||||
|
nextId: number | null
|
||||||
|
previousToken: string | null
|
||||||
|
nextToken: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
type FragmentEntry = Fragment & { key: string }
|
||||||
|
|
||||||
|
function encodeKey(roomId: string, fragmentId: number): string {
|
||||||
return `${roomId}|${encodeUint32(fragmentId)}`;
|
return `${roomId}|${encodeUint32(fragmentId)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TimelineFragmentStore {
|
export class TimelineFragmentStore {
|
||||||
constructor(store) {
|
private _store: Store<FragmentEntry>
|
||||||
|
|
||||||
|
constructor(store: Store<FragmentEntry>) {
|
||||||
this._store = store;
|
this._store = store;
|
||||||
}
|
}
|
||||||
|
|
||||||
_allRange(roomId) {
|
_allRange(roomId: string): IDBKeyRange {
|
||||||
try {
|
try {
|
||||||
return this._store.IDBKeyRange.bound(
|
return this._store.IDBKeyRange.bound(
|
||||||
encodeKey(roomId, KeyLimits.minStorageKey),
|
encodeKey(roomId, KeyLimits.minStorageKey),
|
||||||
|
@ -38,13 +52,13 @@ export class TimelineFragmentStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
all(roomId) {
|
all(roomId: string): Promise<FragmentEntry[]> {
|
||||||
return this._store.selectAll(this._allRange(roomId));
|
return this._store.selectAll(this._allRange(roomId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the fragment without a nextToken and without nextId,
|
/** Returns the fragment without a nextToken and without nextId,
|
||||||
if any, with the largest id if there are multiple (which should not happen) */
|
if any, with the largest id if there are multiple (which should not happen) */
|
||||||
liveFragment(roomId) {
|
liveFragment(roomId: string): Promise<FragmentEntry | undefined> {
|
||||||
// why do we need this?
|
// why do we need this?
|
||||||
// Ok, take the case where you've got a /context fragment and a /sync fragment
|
// Ok, take the case where you've got a /context fragment and a /sync fragment
|
||||||
// They are not connected. So, upon loading the persister, which one do we take? We can't sort them ...
|
// They are not connected. So, upon loading the persister, which one do we take? We can't sort them ...
|
||||||
|
@ -60,20 +74,20 @@ export class TimelineFragmentStore {
|
||||||
// should generate an id an return it?
|
// should generate an id an return it?
|
||||||
// depends if we want to do anything smart with fragment ids,
|
// depends if we want to do anything smart with fragment ids,
|
||||||
// like give them meaning depending on range. not for now probably ...
|
// like give them meaning depending on range. not for now probably ...
|
||||||
add(fragment) {
|
add(fragment: Fragment) {
|
||||||
fragment.key = encodeKey(fragment.roomId, fragment.id);
|
(fragment as any).key = encodeKey(fragment.roomId, fragment.id);
|
||||||
this._store.add(fragment);
|
this._store.add(fragment as FragmentEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(fragment) {
|
update(fragment: FragmentEntry) {
|
||||||
this._store.put(fragment);
|
this._store.put(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
get(roomId, fragmentId) {
|
get(roomId: string, fragmentId: number): Promise<FragmentEntry | null> {
|
||||||
return this._store.get(encodeKey(roomId, fragmentId));
|
return this._store.get(encodeKey(roomId, fragmentId));
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAllForRoom(roomId) {
|
removeAllForRoom(roomId: string): Promise<undefined> {
|
||||||
this._store.delete(this._allRange(roomId));
|
return this._store.delete(this._allRange(roomId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue