Skip to content

Commit

Permalink
apply styles
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Apr 6, 2024
1 parent a7823c1 commit 2482079
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 176 deletions.
1 change: 0 additions & 1 deletion apps/meteor/packages/accounts-linkedin/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Package.describe({
});

Package.onUse((api) => {
api.versionsFrom('2.5');
api.use('ecmascript');
api.use('accounts-base', ['client', 'server']);
api.imply('accounts-base', ['client', 'server']);
Expand Down
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.registerStoreClient('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
117 changes: 61 additions & 56 deletions apps/meteor/packages/autoupdate/autoupdate_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@
// 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';

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 All @@ -53,49 +52,45 @@ Autoupdate.autoupdateVersionRefreshable = null;
Autoupdate.autoupdateVersionCordova = null;
Autoupdate.appId = __meteor_runtime_config__.appId = process.env.APP_ID;

var syncQueue = new Meteor._SynchronousQueue();
var syncQueue = new Meteor._AsynchronousQueue();

function updateVersions(shouldReloadClientProgram) {
async function updateVersions(shouldReloadClientProgram) {
// Step 1: load the current client program on the server
if (shouldReloadClientProgram) {
WebAppInternals.reloadClientPrograms();
await WebAppInternals.reloadClientPrograms();
}

const {
// 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,
};
});

// Step 3: form the new client boilerplate which contains the updated
// assets and __meteor_runtime_config__.
if (shouldReloadClientProgram) {
WebAppInternals.generateBoilerplate();
await WebAppInternals.generateBoilerplate();
}

// Step 4: update the ClientVersions collection.
// We use `onListening` here because we need to use
// `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 +102,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,67 +111,77 @@ 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);
Meteor.startup(async function () {
await 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',
});
});
});

var fut = new Future();
function enqueueVersionsRefresh() {
syncQueue.queueTask(async function () {
await updateVersions(true);
});
}

// We only want 'refresh' to trigger 'updateVersions' AFTER onListen,
// so we add a queued task that waits for onListen before 'refresh' can queue
// tasks. Note that the `onListening` callbacks do not fire until after
// Meteor.startup, so there is no concern that the 'updateVersions' calls from
// 'refresh' will overlap with the `updateVersions` call from Meteor.startup.
const setupListeners = () => {
// Listen for messages pertaining to the client-refresh topic.
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'),
);
};

syncQueue.queueTask(function () {
fut.wait();
});
if (Meteor._isFibersEnabled) {
var Future = Npm.require('fibers/future');

WebApp.onListening(function () {
fut.return();
});
var fut = new Future();

// We only want 'refresh' to trigger 'updateVersions' AFTER onListen,
// so we add a queued task that waits for onListen before 'refresh' can queue
// tasks. Note that the `onListening` callbacks do not fire until after
// Meteor.startup, so there is no concern that the 'updateVersions' calls from
// 'refresh' will overlap with the `updateVersions` call from Meteor.startup.

function enqueueVersionsRefresh() {
syncQueue.queueTask(function () {
updateVersions(true);
fut.wait();
});
}

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

// Another way to tell the process to refresh: send SIGHUP signal
process.on('SIGHUP', Meteor.bindEnvironment(function () {
enqueueVersionsRefresh();
}, "handling SIGHUP signal for refresh"));
setupListeners();
} else {
WebApp.onListening(function () {
Promise.resolve(setupListeners());
});
}
Loading

0 comments on commit 2482079

Please sign in to comment.