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/frontend/client/query.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import * as message from "@cocalc/util/message";6import { is_array } from "@cocalc/util/misc";7import { validate_client_query } from "@cocalc/util/schema-validate";8import { CB } from "@cocalc/util/types/database";910declare const $: any; // jQuery1112export class QueryClient {13private client: any;1415constructor(client: any) {16this.client = client;17}1819private async call(message: object, timeout: number): Promise<any> {20return await this.client.async_call({21message,22timeout,23allow_post: false, // since that would happen via this.post_query24});25}2627// This works like a normal async function when28// opts.cb is NOT specified. When opts.cb is specified,29// it works like a cb and returns nothing. For changefeeds30// you MUST specify opts.cb, but can always optionally do so.31public async query(opts: {32query: object;33changes?: boolean;34options?: object[]; // if given must be an array of objects, e.g., [{limit:5}]35standby?: boolean; // if true and use HTTP post, then will use standby server (so must be read only)36timeout?: number; // default: 3037no_post?: boolean; // DEPRECATED -- didn't turn out to be worth it.38ignore_response?: boolean; // if true, be slightly efficient by not waiting for any response or39// error (just assume it worked; don't care about response)40cb?: CB; // used for changefeed outputs if changes is true41}): Promise<any> {42if (opts.options != null && !is_array(opts.options)) {43// should never happen...44throw Error("options must be an array");45}46if (opts.changes && opts.cb == null) {47throw Error("for changefeed, must specify opts.cb");48}4950const err = validate_client_query(opts.query, this.client.account_id);51if (err) {52throw Error(err);53}54const mesg = message.query({55query: opts.query,56options: opts.options,57changes: opts.changes,58multi_response: !!opts.changes,59});60if (opts.timeout == null) {61opts.timeout = 30;62}63if (mesg.multi_response) {64if (opts.cb == null) {65throw Error("changefeed requires cb callback");66}67this.client.call({68allow_post: false,69message: mesg,70error_event: true,71timeout: opts.timeout,72cb: opts.cb,73});74} else {75if (opts.cb != null) {76try {77const result = await this.call(mesg, opts.timeout);78opts.cb(undefined, result);79} catch (err) {80opts.cb(typeof err == "string" ? err : err.message ?? err);81}82} else {83return await this.call(mesg, opts.timeout);84}85}86}8788public async cancel(id: string): Promise<void> {89await this.call(message.query_cancel({ id }), 30);90}91}929394