Skip to content

Commit

Permalink
Add InitStatus class
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed May 29, 2024
1 parent da3abad commit 1bb835b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
29 changes: 12 additions & 17 deletions srcts/src/shiny/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
mapValues,
pixelRatio,
} from "../utils";
import { promiseWithResolvers } from "../utils/promise";
import {
createInitStatus,
promiseWithResolvers,

Check warning on line 32 in srcts/src/shiny/index.ts

View workflow job for this annotation

GitHub Actions / routine / routine

'promiseWithResolvers' is defined but never used
type InitStatus,
} from "../utils/promise";
import type { BindInputsCtx, BindScope } from "./bind";
import { bindAll, unbindAll, _bindAll } from "./bind";
import type {
Expand Down Expand Up @@ -97,13 +101,9 @@ class ShinyClass {
unbindAll?: typeof shinyUnbindAll;
initializeInputs?: typeof shinyInitializeInputs;

isConnected: PromiseLike<ShinyWebSocket>;
// eslint-disable-next-line @typescript-eslint/naming-convention
_resolveIsConnected: (value: ShinyWebSocket) => void;

isInitialized: PromiseLike<void>;
// eslint-disable-next-line @typescript-eslint/naming-convention
_resolveIsInitialized: (value: void) => void;
// Promise-like objects that are resolved at various stages of initialization.
isConnected: InitStatus<ShinyWebSocket>;
isInitialized: InitStatus<void>;

// Eventually deprecate
// For old-style custom messages - should deprecate and migrate to new
Expand Down Expand Up @@ -142,13 +142,8 @@ class ShinyClass {
this.renderHtmlAsync = renderHtmlAsync;
this.renderHtml = renderHtml;

const connectedPromise = promiseWithResolvers<ShinyWebSocket>();
this.isConnected = connectedPromise.promise;
this._resolveIsConnected = connectedPromise.resolve;

const initPromise = promiseWithResolvers<void>();
this.isInitialized = initPromise.promise;
this._resolveIsInitialized = initPromise.resolve;
this.isConnected = createInitStatus<ShinyWebSocket>();
this.isInitialized = createInitStatus<void>();

$(() => {
// Init Shiny a little later than document ready, so user code can
Expand Down Expand Up @@ -689,11 +684,11 @@ class ShinyClass {
initDeferredIframes();
// @ts-expect-error; .socket property isn't a known property of
// JQuery.TriggeredEvent, but it was added on there anyway.
this._resolveIsConnected(event.socket);
this.isConnected.resolve(event.socket);
});

$(document).one("shiny:sessioninitialized", () => {
this._resolveIsInitialized();
this.isInitialized.resolve();
});
}
}
Expand Down
27 changes: 27 additions & 0 deletions srcts/src/utils/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@ export function promiseWithResolvers<T>(): {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return { promise, resolve: resolve!, reject: reject! };
}

export interface InitStatus<T> extends Promise<T> {
promise: Promise<T>;
resolve(x: T): void;
resolved(): boolean;
}

export function createInitStatus<T>(): InitStatus<T> {
const { promise, resolve } = promiseWithResolvers<T>();
// eslint-disable-next-line @typescript-eslint/naming-convention
let _resolved = false;

return {
promise,
resolve(x: T) {
_resolved = true;
resolve(x);
},
then: promise.then.bind(promise),
catch: promise.catch.bind(promise),
finally: promise.finally.bind(promise),
[Symbol.toStringTag]: "InitStatus",
resolved() {
return _resolved;
},
};
}

0 comments on commit 1bb835b

Please sign in to comment.