Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Conditionally load realtime APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-r-rose committed Dec 1, 2017
1 parent 2c55a92 commit 74fae73
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"mocha": "~3.5.3",
"rimraf": "~2.6.2",
"style-loader": "~0.13.2",
"typescript": "~2.4.2",
"typescript": "~2.6.2",
"url-loader": "~0.5.9",
"webpack": "~2.7.0"
},
Expand Down
3 changes: 3 additions & 0 deletions schema/drive.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"properties": {
"clientId": {
"type": "string", "title": "Client ID", "default": ""
},
"realtime": {
"type": "boolean", "title": "Realtime", "default": true
}
},
"type": "object"
Expand Down
4 changes: 0 additions & 4 deletions src/drive/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ class GoogleDriveFileBrowser extends Widget {

// Keep references to the createFileBrowser arguments for
// when we need to construct it.
this._registry = registry;
this._commands = commands;
this._manager = manager;
this._factory = factory;
this._driveName = driveName;

Expand Down Expand Up @@ -182,9 +180,7 @@ class GoogleDriveFileBrowser extends Widget {
private _browser: FileBrowser;
private _loginScreen: GoogleDriveLogin;
private _logoutButton: ToolbarButton;
private _registry: DocumentRegistry;
private _commands: CommandRegistry;
private _manager: IDocumentManager;
private _factory: IFileBrowserFactory;
private _driveName: string;
private _hasOpenDocuments: () => boolean;
Expand Down
27 changes: 23 additions & 4 deletions src/drive/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import * as drive from './drive';

import {
makeError
makeError, realtimeLoaded
} from '../gapi';


Expand Down Expand Up @@ -57,18 +57,37 @@ class GoogleDrive implements Contents.IDrive {
}
}

/**
* The name of the drive.
*/
get name(): 'GDrive' {
return 'GDrive';
}

/**
* Getter for the IModelDB factory.
*/
get modelDBFactory(): ModelDB.IFactory {
return {
createNew: (path: string) => {
return new GoogleModelDB( {filePath: path} );
if (realtimeLoaded) {
return {
createNew: (path: string) => {
return new GoogleModelDB( {filePath: path} );
}
}
} else {
return {
// If the realtime APIs have not been loaded,
// make a new in-memory ModelDB.
createNew: (path: string) => {
return new ModelDB();
}
}
}
}

/**
* Server settings (unused for interfacing with Google Drive).
*/
readonly serverSettings: ServerConnection.ISettings;

/**
Expand Down
17 changes: 15 additions & 2 deletions src/gapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,22 @@ const gapiInitialized = new PromiseDelegate<void>();
export
let gapiAuthorized = new PromiseDelegate<void>();

/**
* A boolean that is set if the deprecated realtime APIs
* have been loaded onto the page.
*/
export
let realtimeLoaded = false;

/**
* Load the gapi scripts onto the page.
*
* @param realtime - whether to load the (deprecated) realtime libraries.
*
* @returns a promise that resolves when the gapi scripts are loaded.
*/
export
function loadGapi(): Promise<void> {
function loadGapi(realtime: boolean): Promise<void> {
return new Promise<void>( (resolve, reject) => {
// Get the gapi script from Google.
const gapiScript = document.createElement('script');
Expand All @@ -76,7 +85,11 @@ function loadGapi(): Promise<void> {
// Load overall API scripts onto the page.
gapiScript.onload = () => {
// Load the specific client libraries we need.
gapi.load('client:auth2,drive-realtime,drive-share', () => {
const libs = realtime ?
'client:auth2,drive-realtime,drive-share' :
'client:auth2';
gapi.load(libs, () => {
if (realtime) { realtimeLoaded = true };
gapiLoaded.resolve(void 0);
resolve(void 0);
});
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ function activateFileBrowser(app: JupyterLab, palette: ICommandPalette, manager:
const id = fileBrowserPlugin.id;

// Load the gapi libraries onto the page.
loadGapi();
settingRegistry.load(id).then(settings => {
const realtime = settings.get('realtime').composite as boolean;
if (realtime === true) {
console.warn('Warning: Google Realtime has been deprecated. ' +
'No new realtime applications may be registered, ' +
'and existing ones will cease to work in December 2018');
loadGapi(true);
} else {
loadGapi(false);
}
});

// Add the Google Drive backend to the contents manager.
const drive = new GoogleDrive(app.docRegistry);
Expand Down

0 comments on commit 74fae73

Please sign in to comment.