refactor: implementationChanged

This commit is contained in:
cha0s 2024-02-13 06:17:28 -06:00
parent 52cf407f9e
commit 71d6674de2
3 changed files with 23 additions and 22 deletions

View File

@ -533,6 +533,21 @@ class Flecks {
return get(this.config, path, defaultValue); return get(this.config, path, defaultValue);
} }
/**
* Check whether an updated module changed its hook implementation.
*
* @param {*} fleck The fleck implementing the hook.
* @param {*} hook The hook.
* @param {*} M The updated module.
* @returns {boolean}
*/
implementationChanged(fleck, hook, M) {
return (
(this.fleckImplementation(fleck, hook) || M.hooks?.[hook])
&& this.fleckImplementation(fleck, hook)?.toString() !== M.hooks?.[hook]?.toString()
);
}
/** /**
* Interpolate a string with flecks configuration values. * Interpolate a string with flecks configuration values.
* @param {string} string * @param {string} string

View File

@ -8,19 +8,12 @@ const {
export const hooks = { export const hooks = {
'@flecks/core.hmr': (path, M, flecks) => { '@flecks/core.hmr': (path, M, flecks) => {
if ( if (flecks.implementationChanged(path, '@flecks/server.up', M)) {
flecks.fleckImplementation(path, '@flecks/server.up') if (cluster.isWorker) {
|| M.hooks?.['@flecks/server.up']) { cluster.worker.disconnect();
if ( const error = new Error('@flecks/server.up implementation changed!');
flecks.fleckImplementation(path, '@flecks/server.up')?.toString() error.stack = '';
!== M.hooks?.['@flecks/server.up']?.toString() throw error;
) {
if (cluster.isWorker) {
cluster.worker.disconnect();
const error = new Error('@flecks/server.up implementation changed!');
error.stack = '';
throw error;
}
} }
} }
}, },

View File

@ -1,14 +1,7 @@
export const hooks = { export const hooks = {
'@flecks/core.hmr': (path, M, flecks) => { '@flecks/core.hmr': (path, M, flecks) => {
if ( if (flecks.implementationChanged(path, '@flecks/web/client.up', M)) {
flecks.fleckImplementation(path, '@flecks/web/client.up') throw new Error('@flecks/web/client.up implementation changed!');
|| M.hooks?.['@flecks/web/client.up']) {
if (
flecks.fleckImplementation(path, '@flecks/web/client.up')?.toString()
!== M.hooks?.['@flecks/web/client.up']?.toString()
) {
throw new Error('@flecks/web/client.up implementation changed!');
}
} }
}, },
}; };