Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/mobile/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const WEB_PAGE = "the server answered with a web page (a Wi-Fi login?)";
export const canCall = () => isConfigured && !isOfflineMode() && !!getToken();

/** One request; `body` is sent as JSON. */
export async function call<T>(path: string, init: { method?: string; body?: unknown } = {}): Promise<T> {
export async function call<T>(path: string, init: { method?: string; body?: unknown; headers?: Record<string, string> } = {}): Promise<T> {
if (!isConfigured) throw new ApiError(0, "API not configured in this build");
if (isOfflineMode()) throw new ApiError(0, "offline mode is on");
if (!getToken()) throw new ApiError(401, "not signed in");
const started = Date.now();
const method = init.method ?? "GET";
const headers = authHeaders(init.body === undefined ? {} : { "content-type": "application/json" });
const headers = authHeaders({ ...(init.body === undefined ? {} : { "content-type": "application/json" }), ...init.headers });
let res: Response;
try {
res = await fetch(`${API_URL}${path}`, { method, headers, body: init.body === undefined ? undefined : JSON.stringify(init.body) });
Expand Down Expand Up @@ -130,6 +130,12 @@ export async function listFiles(root: Root, q: { type?: string; container?: stri
export const getFile = (id: string) => call<{ file: FileRec }>(`/api/files/${id}`).then((r) => r.file);
export const putFile = (id: string, f: Partial<Pick<FileRec, "name" | "container" | "body" | "internal_of">> & { type: string; refs?: { kind: string; to: string; data?: Record<string, unknown> | null }[] } & ({ project_id: string } | { home: true })) =>
call<{ file: FileRec }>(`/api/files/${id}`, { method: "PUT", body: f }).then((r) => r.file);
/** A save under an edit lock this client holds (docs/api.md, "the edit lock"): the holder goes along. */
export const putLockedFile = (id: string, holder: string, f: Parameters<typeof putFile>[1]) =>
call<{ file: FileRec }>(`/api/files/${id}`, { method: "PUT", body: f, headers: { "x-lock-holder": holder } }).then((r) => r.file);
/** Take or renew a lockable file's edit lock (it holds 60 s); 423 with the lock when someone else holds it. */
export const lockFile = (id: string, holder: string) => call<{ lock: { by: string | null; until: string } }>(`/api/files/${id}/lock`, { method: "POST", body: { holder } }).then((r) => r.lock);
export const unlockFile = (id: string, holder: string) => call<unknown>(`/api/files/${id}/lock?holder=${encodeURIComponent(holder)}`, { method: "DELETE" });
export const moveFiles = (ids: string[], container: string | null) => call<unknown>("/api/files/move", { method: "POST", body: { ids, container } });
/** Property values of one file (null removes one) → the file's values after. */
export const putValues = (id: string, values: Record<string, unknown>) => call<{ values: Record<string, unknown> }>(`/api/files/${id}/properties`, { method: "PUT", body: { values } }).then((r) => r.values);
Expand Down
106 changes: 106 additions & 0 deletions apps/mobile/src/ext/flows/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { randomUUID } from "expo-crypto";
import { emptyFlow, flowError, type FlowBody } from "@openclapper/ext-flows";
import { ApiError, canCall, getFile, isRejection, listFiles, putFile, putLockedFile, type FileRec } from "../../api";
import { log } from "../../log";
import { persisted } from "../../store";
import { onSync } from "../../sync";
import { toast } from "../../ui";

/**
* The project's flows on this device (docs/features/flows.md, "On the device"; #426): kept here so a flow opens,
* changes and plays without a connection, as on set. An edit is saved here first and sent when the server answers:
* under the edit lock while the editor holds it, else as a save of the version it started from. When the server's
* flow changed meanwhile (the Studio edited it), or it is gone, the edit is saved as a copy beside it: nothing made
* on the device is lost, nothing made elsewhere is overwritten.
*/
export interface LocalFlow { id: string; projectId: string; name: string; body: FlowBody; /** The server's version; null: not on the server yet. */ updatedAt: string | null }
interface Pending { id: string; projectId: string; name: string; body: FlowBody; /** The server version the edit started from; null: new here. */ base: string | null; rev: number; holder?: string; error?: string }

const isList = (v: unknown) => Array.isArray(v);
const isMap = (v: unknown) => typeof v === "object" && v !== null && !Array.isArray(v);
const kept = persisted<LocalFlow[]>("flows.v1", [], isList);
const pending = persisted<Record<string, Pending>>("flowEdits.v1", {}, isMap);
let rev = 0;

const fromFile = (f: FileRec, projectId: string): LocalFlow => ({ id: f.id, projectId, name: f.name, body: { ...emptyFlow(), ...(f.body as Partial<FlowBody>) }, updatedAt: f.updated_at });
const upsert = (list: LocalFlow[], f: LocalFlow) => [...list.filter((x) => x.id !== f.id), f];

/** The server's flows of a project, kept for offline use (the device's unsent edits stay on top of them). */
export async function loadFlows(projectId: string): Promise<void> {
if (!canCall()) return;
try {
const files = await listFiles({ project_id: projectId }, { type: "flow", body: true });
kept.set((all) => [...all.filter((x) => x.projectId !== projectId), ...files.map((f) => fromFile(f, projectId))]);
} catch (e) { log("info", "flows not loaded", { error: e }); }
}

/** A project's flows as this device has them: its own edits over the server's copies, by name. */
export function useFlows(projectId: string | undefined): LocalFlow[] {
const all = kept.use(), edits = pending.use();
if (!projectId) return [];
const mine = all.filter((x) => x.projectId === projectId).map((x) => (edits[x.id] ? { ...x, name: edits[x.id].name, body: edits[x.id].body } : x));
const fresh = Object.values(edits).filter((p) => p.projectId === projectId && !all.some((x) => x.id === p.id)).map((p): LocalFlow => ({ id: p.id, projectId, name: p.name, body: p.body, updatedAt: null }));
return [...mine, ...fresh].sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
}
export const hasUnsent = (id: string) => !!pending.get()[id];
export const usePendingOf = (id: string): Pending | undefined => pending.use()[id];

/** A change to a flow, kept here at once and sent soon; `holder`: the editor holds the edit lock. */
export function saveFlow(f: { id: string; projectId: string; name: string; body: FlowBody }, holder?: string) {
const was = pending.get()[f.id];
const base = was ? was.base : kept.get().find((x) => x.id === f.id)?.updatedAt ?? null;
pending.set((all) => ({ ...all, [f.id]: { ...f, base, rev: ++rev, holder } }));
schedule();
}

/** A new flow, made here (also offline); it goes to the server with the next send. */
export function newFlow(projectId: string, count: number): string {
const id = randomUUID();
saveFlow({ id, projectId, name: `Flow ${count + 1}`, body: emptyFlow() });
return id;
}

let timer: ReturnType<typeof setTimeout> | null = null;
function schedule() { if (timer) clearTimeout(timer); timer = setTimeout(() => { timer = null; void flushFlows(); }, 800); }

let flushing: Promise<void> | null = null;
/** Sends what waits, one flow at a time. A network error stops the run until the next sync. */
export function flushFlows(): Promise<void> {
if (flushing) return flushing;
flushing = (async () => {
for (const p of Object.values(pending.get())) {
if (!canCall()) return;
if (p.error) continue; // refused before and not edited since: shown, not retried in a loop
const bad = flowError(p.body as unknown as Record<string, unknown>);
if (bad) { pending.set((all) => (all[p.id]?.rev === p.rev ? { ...all, [p.id]: { ...p, error: bad } } : all)); continue; }
try {
let server: FileRec | null = null;
if (p.base !== null) { try { server = await getFile(p.id); } catch (e) { if (!(e instanceof ApiError && e.status === 404)) throw e; } }
const changedElsewhere = p.base !== null && (!server || server.deleted_at !== null || server.updated_at !== p.base);
const body = p.body as unknown as Record<string, unknown>;
if (changedElsewhere) {
// Edited in the Studio meanwhile, or deleted: this device's version goes beside it, as a copy.
const copy = randomUUID(), name = `${p.name} (from this device)`.slice(0, 120);
const f = await putFile(copy, { type: "flow", project_id: p.projectId, name, body });
kept.set((all) => upsert(all, fromFile(f, p.projectId)));
toast(`“${p.name}” changed elsewhere while this device was editing: saved as “${name}”`, "neutral");
} else {
const req = { type: "flow", project_id: p.projectId, name: p.name, body };
const f = p.holder ? await putLockedFile(p.id, p.holder, req) : await putFile(p.id, req);
kept.set((all) => upsert(all, fromFile(f, p.projectId)));
// Edited again while it was on its way: that edit now starts from the version just saved.
if (pending.get()[p.id]?.rev !== p.rev) { pending.set((all) => (all[p.id] ? { ...all, [p.id]: { ...all[p.id], base: f.updated_at } } : all)); continue; }
}
pending.set((all) => { if (all[p.id]?.rev !== p.rev) return all; const { [p.id]: _sent, ...rest } = all; void _sent; return rest; });
} catch (e) {
if (e instanceof ApiError && e.status === 423) { log("info", "flow held by someone else; kept here", { flow: p.id }); continue; }
if (!isRejection(e)) { log("info", "flows not sent", { error: e }); return; }
log("warn", "flow refused", { flow: p.id, error: e });
pending.set((all) => (all[p.id]?.rev === p.rev ? { ...all, [p.id]: { ...p, error: (e as Error).message } } : all));
toast(`“${p.name}” could not be saved: ${(e as Error).message}. It stays on this device.`, "danger");
}
}
})().finally(() => { flushing = null; });
return flushing;
}
onSync(flushFlows);
Loading
Loading