Skip to content

Commit

Permalink
chore(meteor/packages): apply code style (#32145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Apr 8, 2024
1 parent 67be331 commit 474589f
Show file tree
Hide file tree
Showing 20 changed files with 1,552 additions and 1,604 deletions.
89 changes: 40 additions & 49 deletions apps/meteor/packages/autoupdate/autoupdate_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,27 @@
// The client version of the client code currently running in the
// browser.

import { ClientVersions } from "./client_versions.js";
import { ClientVersions } from './client_versions.js';

const clientArch = Meteor.isCordova ? "web.cordova" :
Meteor.isModern ? "web.browser" : "web.browser.legacy";
const clientArch = Meteor.isCordova ? 'web.cordova' : Meteor.isModern ? 'web.browser' : 'web.browser.legacy';

const autoupdateVersions =
((__meteor_runtime_config__.autoupdate || {}).versions || {})[clientArch] || {
version: "unknown",
versionRefreshable: "unknown",
versionNonRefreshable: "unknown",
assets: [],
};
const autoupdateVersions = ((__meteor_runtime_config__.autoupdate || {}).versions || {})[clientArch] || {
version: 'unknown',
versionRefreshable: 'unknown',
versionNonRefreshable: 'unknown',
assets: [],
};

export const Autoupdate = {};

// Stores acceptable client versions.
const clientVersions =
Autoupdate._clientVersions = // Used by a self-test and hot-module-replacement
new ClientVersions();
const clientVersions = (Autoupdate._clientVersions = // Used by a self-test and hot-module-replacement
new ClientVersions());

Meteor.connection.registerStore(
"meteor_autoupdate_clientVersions",
clientVersions.createStore()
);
Meteor.connection.registerStore('meteor_autoupdate_clientVersions', clientVersions.createStore());

Autoupdate.newClientAvailable = function () {
return clientVersions.newClientAvailable(
clientArch,
["versionRefreshable", "versionNonRefreshable"],
autoupdateVersions
);
return clientVersions.newClientAvailable(clientArch, ['versionRefreshable', 'versionNonRefreshable'], autoupdateVersions);
};

// Set to true if the link.onload callback ever fires for any <link> node.
Expand All @@ -71,15 +61,15 @@ const retry = new Retry({
// server fixing code will result in a restart and reconnect, but
// potentially the subscription could have a transient error.
minCount: 0, // don't do any immediate retries
baseTimeout: 30*1000 // start with 30s
baseTimeout: 30 * 1000, // start with 30s
});

let failures = 0;

Autoupdate._retrySubscription = () => {
Meteor.subscribe("meteor_autoupdate_clientVersions", {
Meteor.subscribe('meteor_autoupdate_clientVersions', {
onError(error) {
Meteor._debug("autoupdate subscription failed", error);
Meteor._debug('autoupdate subscription failed', error);
failures++;
retry.retryLater(failures, function () {
// Just retry making the subscription, don't reload the whole
Expand Down Expand Up @@ -111,8 +101,7 @@ Autoupdate._retrySubscription = () => {
return;
}

if (doc.versionNonRefreshable !==
autoupdateVersions.versionNonRefreshable) {
if (doc.versionNonRefreshable !== autoupdateVersions.versionNonRefreshable) {
// Non-refreshable assets have changed, so we have to reload the
// whole page rather than just replacing <link> tags.
if (stop) stop();
Expand All @@ -121,7 +110,13 @@ Autoupdate._retrySubscription = () => {
// is provided by the ddp package that autoupdate depends on.

// Delay reload in 60 seconds
console.warn('Client version changed from', autoupdateVersions.versionNonRefreshable, 'to', doc.versionNonRefreshable, `Page will reload in ${reloadDelayInSeconds} seconds`);
console.warn(
'Client version changed from',
autoupdateVersions.versionNonRefreshable,
'to',
doc.versionNonRefreshable,
`Page will reload in ${reloadDelayInSeconds} seconds`,
);
setTimeout(() => {
Package.reload.Reload._reload();
}, reloadDelayInSeconds * 1000);
Expand All @@ -137,30 +132,27 @@ Autoupdate._retrySubscription = () => {
var newCss = doc.assets || [];
var oldLinks = [];

Array.prototype.forEach.call(
document.getElementsByTagName('link'),
function (link) {
if (link.className === '__meteor-css__') {
oldLinks.push(link);
}
Array.prototype.forEach.call(document.getElementsByTagName('link'), function (link) {
if (link.className === '__meteor-css__') {
oldLinks.push(link);
}
);
});

function waitUntilCssLoads(link, callback) {
var called;

link.onload = function () {
knownToSupportCssOnLoad = true;
if (! called) {
if (!called) {
called = true;
callback();
}
};

if (! knownToSupportCssOnLoad) {
if (!knownToSupportCssOnLoad) {
var id = Meteor.setInterval(function () {
if (link.sheet) {
if (! called) {
if (!called) {
called = true;
callback();
}
Expand All @@ -172,35 +164,34 @@ Autoupdate._retrySubscription = () => {

let newLinksLeftToLoad = newCss.length;
function removeOldLinks() {
if (oldLinks.length > 0 &&
--newLinksLeftToLoad < 1) {
oldLinks.splice(0).forEach(link => {
if (oldLinks.length > 0 && --newLinksLeftToLoad < 1) {
oldLinks.splice(0).forEach((link) => {
link.parentNode.removeChild(link);
});
}
}

if (newCss.length > 0) {
newCss.forEach(css => {
const newLink = document.createElement("link");
newLink.setAttribute("rel", "stylesheet");
newLink.setAttribute("type", "text/css");
newLink.setAttribute("class", "__meteor-css__");
newLink.setAttribute("href", css.url);
newCss.forEach((css) => {
const newLink = document.createElement('link');
newLink.setAttribute('rel', 'stylesheet');
newLink.setAttribute('type', 'text/css');
newLink.setAttribute('class', '__meteor-css__');
newLink.setAttribute('href', css.url);

waitUntilCssLoads(newLink, function () {
Meteor.setTimeout(removeOldLinks, 200);
});

const head = document.getElementsByTagName("head").item(0);
const head = document.getElementsByTagName('head').item(0);
head.appendChild(newLink);
});
} else {
removeOldLinks();
}
}
}
}
},
});
};

Expand Down
60 changes: 27 additions & 33 deletions apps/meteor/packages/autoupdate/autoupdate_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
// The ID of each document is the client architecture, and the fields of
// the document are the versions described above.

import { ClientVersions } from "./client_versions.js";
var Future = Npm.require("fibers/future");
import { ClientVersions } from './client_versions.js';
var Future = Npm.require('fibers/future');

export const Autoupdate = __meteor_runtime_config__.autoupdate = {
export const Autoupdate = (__meteor_runtime_config__.autoupdate = {
// Map from client architectures (web.browser, web.browser.legacy,
// web.cordova) to version fields { version, versionRefreshable,
// versionNonRefreshable, refreshable } that will be stored in
// ClientVersions documents (whose IDs are client architectures). This
// data gets serialized into the boilerplate because it's stored in
// __meteor_runtime_config__.autoupdate.versions.
versions: {}
};
versions: {},
});

// Stores acceptable client versions.
const clientVersions = new ClientVersions();
Expand Down Expand Up @@ -65,22 +65,18 @@ function updateVersions(shouldReloadClientProgram) {
// If the AUTOUPDATE_VERSION environment variable is defined, it takes
// precedence, but Autoupdate.autoupdateVersion is still supported as
// a fallback. In most cases neither of these values will be defined.
AUTOUPDATE_VERSION = Autoupdate.autoupdateVersion
AUTOUPDATE_VERSION = Autoupdate.autoupdateVersion,
} = process.env;

// Step 2: update __meteor_runtime_config__.autoupdate.versions.
const clientArchs = Object.keys(WebApp.clientPrograms);
clientArchs.forEach(arch => {
clientArchs.forEach((arch) => {
Autoupdate.versions[arch] = {
version: AUTOUPDATE_VERSION ||
WebApp.calculateClientHash(arch),
versionRefreshable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashRefreshable(arch),
versionNonRefreshable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashNonRefreshable(arch),
versionReplaceable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashReplaceable(arch),
versionHmr: WebApp.clientPrograms[arch].hmrVersion
version: AUTOUPDATE_VERSION || WebApp.calculateClientHash(arch),
versionRefreshable: AUTOUPDATE_VERSION || WebApp.calculateClientHashRefreshable(arch),
versionNonRefreshable: AUTOUPDATE_VERSION || WebApp.calculateClientHashNonRefreshable(arch),
versionReplaceable: AUTOUPDATE_VERSION || WebApp.calculateClientHashReplaceable(arch),
versionHmr: WebApp.clientPrograms[arch].hmrVersion,
};
});

Expand All @@ -95,7 +91,7 @@ function updateVersions(shouldReloadClientProgram) {
// `WebApp.getRefreshableAssets`, which is only set after
// `WebApp.generateBoilerplate` is called by `main` in webapp.
WebApp.onListening(() => {
clientArchs.forEach(arch => {
clientArchs.forEach((arch) => {
const payload = {
...Autoupdate.versions[arch],
assets: WebApp.getRefreshableAssets(arch),
Expand All @@ -107,7 +103,7 @@ function updateVersions(shouldReloadClientProgram) {
}

Meteor.publish(
"meteor_autoupdate_clientVersions",
'meteor_autoupdate_clientVersions',
function (appId) {
// `null` happens when a client doesn't have an appId and passes
// `undefined` to `Meteor.subscribe`. `undefined` is translated to
Expand All @@ -116,36 +112,31 @@ Meteor.publish(

// Don't notify clients using wrong appId such as mobile apps built with a
// different server but pointing at the same local url
if (Autoupdate.appId && appId && Autoupdate.appId !== appId)
return [];
if (Autoupdate.appId && appId && Autoupdate.appId !== appId) return [];

// Random value to delay the updates for 2-10 minutes
const randomInterval = Meteor.isProduction ? (Math.floor(Math.random() * 8) + 2) * 1000 * 60 : 0;

const stop = clientVersions.watch((version, isNew) => {
setTimeout(() => {
(isNew ? this.added : this.changed)
.call(this, "meteor_autoupdate_clientVersions", version._id, version)
(isNew ? this.added : this.changed).call(this, 'meteor_autoupdate_clientVersions', version._id, version);
}, randomInterval);
});

this.onStop(() => stop());
this.ready();
},
{is_auto: true}
{ is_auto: true },
);

Meteor.startup(function () {
updateVersions(false);

// Force any connected clients that are still looking for these older
// document IDs to reload.
["version",
"version-refreshable",
"version-cordova",
].forEach(_id => {
['version', 'version-refreshable', 'version-cordova'].forEach((_id) => {
clientVersions.set(_id, {
version: "outdated"
version: 'outdated',
});
});
});
Expand Down Expand Up @@ -173,10 +164,13 @@ function enqueueVersionsRefresh() {
}

// Listen for messages pertaining to the client-refresh topic.
import { onMessage } from "meteor/inter-process-messaging";
onMessage("client-refresh", enqueueVersionsRefresh);
import { onMessage } from 'meteor/inter-process-messaging';
onMessage('client-refresh', enqueueVersionsRefresh);

// Another way to tell the process to refresh: send SIGHUP signal
process.on('SIGHUP', Meteor.bindEnvironment(function () {
enqueueVersionsRefresh();
}, "handling SIGHUP signal for refresh"));
process.on(
'SIGHUP',
Meteor.bindEnvironment(function () {
enqueueVersionsRefresh();
}, 'handling SIGHUP signal for refresh'),
);
23 changes: 10 additions & 13 deletions apps/meteor/packages/autoupdate/client_versions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracker } from "meteor/tracker";
import { Tracker } from 'meteor/tracker';

export class ClientVersions {
constructor() {
Expand All @@ -12,10 +12,10 @@ export class ClientVersions {
createStore() {
return {
update: ({ id, msg, fields }) => {
if (msg === "added" || msg === "changed") {
if (msg === 'added' || msg === 'changed') {
this.set(id, fields);
}
}
},
};
}

Expand All @@ -39,15 +39,15 @@ export class ClientVersions {
} else {
version = {
_id: id,
...fields
...fields,
};

isNew = true;
this._versions.set(id, version);
}

this._watchCallbacks.forEach(({ fn, filter }) => {
if (! filter || filter === version._id) {
if (!filter || filter === version._id) {
fn(version, isNew);
}
});
Expand All @@ -59,11 +59,11 @@ export class ClientVersions {
// documents. If `filter` is set, the callback is only invoked for documents
// with ID `filter`.
watch(fn, { skipInitial, filter } = {}) {
if (! skipInitial) {
if (!skipInitial) {
const resolved = Promise.resolve();

this._versions.forEach((version) => {
if (! filter || filter === version._id) {
if (!filter || filter === version._id) {
resolved.then(() => fn(version, true));
}
});
Expand All @@ -78,10 +78,7 @@ export class ClientVersions {
// A reactive data source for `Autoupdate.newClientAvailable`.
newClientAvailable(id, fields, currentVersion) {
function isNewVersion(version) {
return (
version._id === id &&
fields.some((field) => version[field] !== currentVersion[field])
);
return version._id === id && fields.some((field) => version[field] !== currentVersion[field]);
}

const dependency = new Tracker.Dependency();
Expand All @@ -96,9 +93,9 @@ export class ClientVersions {
stop();
}
},
{ skipInitial: true }
{ skipInitial: true },
);

return !! version && isNewVersion(version);
return !!version && isNewVersion(version);
}
}
2 changes: 1 addition & 1 deletion apps/meteor/packages/autoupdate/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Package.describe({
version: '1.8.0',
});

Package.onUse(function(api) {
Package.onUse(function (api) {
api.use(['webapp', 'check', 'inter-process-messaging'], 'server');

api.use(['tracker', 'retry'], 'client');
Expand Down
Loading

0 comments on commit 474589f

Please sign in to comment.