Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global jQuery object and return of cellInfo #556

Merged
merged 2 commits into from
Mar 6, 2022
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
7 changes: 7 additions & 0 deletions js/jquery-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import _jquery from "jquery";

// Some users depend on jQuery being globally set by sage_cell.
// We take care to initialize the jQuery global variable only if
// another jQuery is not set.
window.jQuery = window.jQuery || window.$ || _jquery;
window.$ = window.$ || window.jQuery || _jquery;
33 changes: 24 additions & 9 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sagecell from "./sagecell";
import cell from "./cell";
import "./jquery-global";

// TODO: put this tracking code in a site-specific file.
// TODO: finish implementing our own stats service that handles,
Expand All @@ -26,9 +27,12 @@ _gaq.push(["sagecell._trackPageview"]);
* it can be called externally.
*/
function makeResolvablePromise() {
const ret = { promise: null, resolve: null };
const ret = { promise: null, resolve: null, state: "pending" };
ret.promise = new Promise((resolve) => {
ret.resolve = resolve;
ret.resolve = (...args) => {
ret.state = "fulfilled";
return resolve(...args);
};
});
return ret;
}
Expand Down Expand Up @@ -64,13 +68,24 @@ Object.assign(sagecell, {
// but we may not be ready to process data right away, so we
// provide a wrapper that will poll until sagecell is ready.
makeSagecell: function (args) {
sagecell._initPromise.promise
.then(() => {
window.sagecell._makeSagecell(args);
})
.catch((e) => {
console.warn("Encountered error in makeSagecell", e);
});
// Clients expect to receive a `cellInfo` object right away.
// However, this object cannot be made available until the page loads.
// If we're not ready, we return a stub object that gets updated with
// the proper data when it becomes available.
if (sagecell._initPromise.state === "pending") {
const ret = {};
sagecell._initPromise.promise
.then(() => {
const cellInfo = window.sagecell._makeSagecell(args);
Object.assign(ret, cellInfo);
})
.catch((e) => {
console.warn("Encountered error in makeSagecell", e);
});
return ret;
} else {
return window.sagecell._makeSagecell(args);
}
},
_initPromise: makeResolvablePromise(),
});
Expand Down
7 changes: 5 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ module.exports = {
new webpack.ProvidePlugin({
jQuery: "jquery",
$: "jquery",
"window.jQuery": "jquery",
"window.$": "jquery",
// Normally the following lines are used to make sure that jQuery
// cannot "leak" into the outside environment. However, since
// we *want* to initialize the global jQuery object, we omit them.
// "window.jQuery": "jquery",
// "window.$": "jquery",
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
Expand Down