Skip to content

Commit

Permalink
Merge pull request #556 from siefkenj/webpack2
Browse files Browse the repository at this point in the history
Global jQuery object and return of `cellInfo`
  • Loading branch information
novoselt authored Mar 6, 2022
2 parents b8471e7 + f004887 commit 62db3a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
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

0 comments on commit 62db3a2

Please sign in to comment.