Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/blobs.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Saving blobs to hub7CoCalc: Collaborative web-based SageMath, Jupyter, LaTeX and Terminals.8Copyright 2015, SageMath, Inc., GPL v3.9*/1011import { getLogger } from "@cocalc/backend/logger";12import * as message from "@cocalc/util/message";13import { defaults, required, uuid } from "@cocalc/util/misc";14import { CB } from "@cocalc/util/types/database";1516const winston = getLogger("blobs");1718type BlobCB = CB<any, { sha1: string; error: string }>;1920type CBEntry = [BlobCB, string];2122const _save_blob_callbacks: { [key: string]: CBEntry[] } = {};2324interface Opts {25sha1: string;26cb: BlobCB;27timeout?: number;28}2930export function receive_save_blob_message(opts: Opts): void {31// temporarily used by file_session_manager32opts = defaults(opts, {33sha1: required,34cb: required,35timeout: 30, // seconds; maximum time in seconds to wait for response message36});37winston.debug(`receive_save_blob_message: ${opts.sha1}`);38const { sha1 } = opts;39const id = uuid();40_save_blob_callbacks[sha1] ??= [];41_save_blob_callbacks[sha1].push([opts.cb, id]);4243// Timeout functionality -- send a response after opts.timeout seconds,44// in case no hub responded.45if (!opts.timeout) {46return;47}4849const f = function (): void {50const v = _save_blob_callbacks[sha1];51if (v != null) {52const mesg = message.save_blob({53sha1,54error: `timed out after local hub waited for ${opts.timeout} seconds`,55});5657const w: CBEntry[] = [];58for (let x of v) {59// this is O(n) instead of O(1), but who cares since n is usually 1.60if (x[1] === id) {61x[0](mesg);62} else {63w.push(x);64}65}6667if (w.length === 0) {68delete _save_blob_callbacks[sha1];69} else {70_save_blob_callbacks[sha1] = w;71}72}73};7475setTimeout(f, opts.timeout * 1000);76}7778export function handle_save_blob_message(mesg): void {79winston.debug(`handle_save_blob_message: ${mesg.sha1}`);80const v = _save_blob_callbacks[mesg.sha1];81if (v != null) {82for (let x of v) {83x[0](mesg);84}85delete _save_blob_callbacks[mesg.sha1];86}87}888990