2019-06-27 01:30:50 +05:30
|
|
|
import { StorageError } from "../../common.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {Platform} from "../../../../Platform.js";
|
2019-07-01 13:30:29 +05:30
|
|
|
import { encodeUint32 } from "../utils.js";
|
2019-06-27 01:19:42 +05:30
|
|
|
|
2019-06-27 01:25:33 +05:30
|
|
|
function encodeKey(roomId, fragmentId) {
|
2019-07-01 13:30:29 +05:30
|
|
|
return `${roomId}|${encodeUint32(fragmentId)}`;
|
2019-06-27 01:25:33 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class TimelineFragmentStore {
|
2019-05-11 19:11:09 +05:30
|
|
|
constructor(store) {
|
|
|
|
this._store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
_allRange(roomId) {
|
2019-06-27 01:30:50 +05:30
|
|
|
try {
|
|
|
|
return IDBKeyRange.bound(
|
|
|
|
encodeKey(roomId, Platform.minStorageKey),
|
|
|
|
encodeKey(roomId, Platform.maxStorageKey)
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
throw new StorageError(`error from IDBKeyRange with roomId ${roomId}`, err);
|
|
|
|
}
|
2019-05-11 19:11:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
all(roomId) {
|
|
|
|
return this._store.selectAll(this._allRange(roomId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the fragment without a nextToken and without nextId,
|
|
|
|
if any, with the largest id if there are multiple (which should not happen) */
|
|
|
|
liveFragment(roomId) {
|
|
|
|
// why do we need this?
|
|
|
|
// 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 ...
|
|
|
|
// we assume that the one without a nextToken and without a nextId is a live one
|
|
|
|
// there should really be only one like this
|
|
|
|
|
|
|
|
// reverse because assuming live fragment has bigger id than non-live ones
|
|
|
|
return this._store.findReverse(this._allRange(roomId), fragment => {
|
|
|
|
return typeof fragment.nextId !== "number" && typeof fragment.nextToken !== "string";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// should generate an id an return it?
|
|
|
|
// depends if we want to do anything smart with fragment ids,
|
|
|
|
// like give them meaning depending on range. not for now probably ...
|
|
|
|
add(fragment) {
|
2019-06-27 01:25:33 +05:30
|
|
|
fragment.key = encodeKey(fragment.roomId, fragment.id);
|
2019-05-11 19:11:09 +05:30
|
|
|
return this._store.add(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(fragment) {
|
|
|
|
return this._store.put(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(roomId, fragmentId) {
|
2019-06-27 01:25:33 +05:30
|
|
|
return this._store.get(encodeKey(roomId, fragmentId));
|
2019-05-11 19:11:09 +05:30
|
|
|
}
|
|
|
|
}
|