Store the splits as a byte array

From now on we never serialize the splits to a string and instead
directly slice the WebAssembly memory and directly export, upload, and
save the splits from there.

Additionally this does another optimization where livesplit-core now
provides a way to directly query the length of the returned buffer
instead of having to manually search of the nul-terminator.
This commit is contained in:
Christopher Serr
2020-03-12 21:39:50 -05:00
committed by wooferzfg
parent a0b7b40033
commit e1144a8ea4
4 changed files with 12 additions and 13 deletions
+9 -10
View File
@@ -41,7 +41,7 @@ type Menu =
{ kind: MenuKind.About };
export interface Props {
splits: string,
splits: Int8Array,
layout: any,
settings: any,
layoutWidth: number,
@@ -63,7 +63,7 @@ const DEFAULT_LAYOUT_WIDTH = 300;
export class LiveSplit extends React.Component<Props, State> {
public static async loadStoredData() {
const splits = await localforage.getItem("splits") as string;
const splits = await localforage.getItem("splits") as Int8Array ?? new Int8Array();
const layout = await localforage.getItem("layout");
const settings = await localforage.getItem("settings");
const layoutWidth = await localforage.getItem("layoutWidth") as number;
@@ -119,8 +119,8 @@ export class LiveSplit extends React.Component<Props, State> {
"The Default Loading Run should be a valid Run",
);
this.loadFromSplitsIO(window.location.hash.substr("#/splits-io/".length));
} else {
const result = Run.parseString(props.splits, "", false);
} else if (props.splits.length > 0) {
const result = Run.parseArray(props.splits, "", false);
if (result.parsedSuccessfully()) {
result.unwrap().with((r) => timer.writeWith((t) => t.setRun(r)))?.dispose();
}
@@ -377,10 +377,10 @@ export class LiveSplit extends React.Component<Props, State> {
}
public async uploadToSplitsIO(): Promise<Option<Window>> {
const lss = this.readWith((t) => t.saveAsLss());
const lss = this.readWith((t) => t.saveAsLssBytes());
try {
const claimUri = await SplitsIO.uploadLss(lss);
const claimUri = await SplitsIO.uploadLss(new Blob([lss]));
return window.open(claimUri);
} catch (_) {
toast.error("Failed to upload the splits.");
@@ -390,9 +390,9 @@ export class LiveSplit extends React.Component<Props, State> {
public exportSplits() {
const [lss, name] = this.writeWith((t) => {
const lss = t.saveAsLss();
const name = t.getRun().extendedFileName(true);
t.markAsUnmodified();
const name = t.getRun().extendedFileName(true);
const lss = t.saveAsLssBytes();
return [lss, name];
});
try {
@@ -404,9 +404,8 @@ export class LiveSplit extends React.Component<Props, State> {
public async saveSplits() {
const lss = this.writeWith((t) => {
const lss = t.saveAsLss();
t.markAsUnmodified();
return lss;
return t.saveAsLssBytes();
});
try {
await localforage.setItem("splits", lss);
+1 -1
View File
@@ -58,7 +58,7 @@ export async function openFileAsString(): Promise<[string, File]> {
return convertFileToString(file);
}
export function exportFile(filename: string, data: any) {
export function exportFile(filename: string, data: BlobPart) {
const url = URL.createObjectURL(new Blob([data], { type: "application/octet-stream" }));
try {
const element = document.createElement("a");
+1 -1
View File
@@ -28,7 +28,7 @@ export enum UploadError {
UploadRequestErrored,
}
export async function uploadLss(lss: string): Promise<string> {
export async function uploadLss(lss: string | Blob): Promise<string> {
const response = await validatedFetch(
"https://splits.io/api/v4/runs",
{