Skip to content

Commit

Permalink
refactor: implementationChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Feb 13, 2024
1 parent 52cf407 commit 71d6674
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
15 changes: 15 additions & 0 deletions packages/core/build/flecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,21 @@ class Flecks {
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.
* @param {string} string
Expand Down
19 changes: 6 additions & 13 deletions packages/server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ const {

export const hooks = {
'@flecks/core.hmr': (path, M, flecks) => {
if (
flecks.fleckImplementation(path, '@flecks/server.up')
|| M.hooks?.['@flecks/server.up']) {
if (
flecks.fleckImplementation(path, '@flecks/server.up')?.toString()
!== M.hooks?.['@flecks/server.up']?.toString()
) {
if (cluster.isWorker) {
cluster.worker.disconnect();
const error = new Error('@flecks/server.up implementation changed!');
error.stack = '';
throw error;
}
if (flecks.implementationChanged(path, '@flecks/server.up', M)) {
if (cluster.isWorker) {
cluster.worker.disconnect();
const error = new Error('@flecks/server.up implementation changed!');
error.stack = '';
throw error;
}
}
},
Expand Down
11 changes: 2 additions & 9 deletions packages/web/src/client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
export const hooks = {
'@flecks/core.hmr': (path, M, flecks) => {
if (
flecks.fleckImplementation(path, '@flecks/web/client.up')
|| 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!');
}
if (flecks.implementationChanged(path, '@flecks/web/client.up', M)) {
throw new Error('@flecks/web/client.up implementation changed!');
}
},
};
Expand Down

0 comments on commit 71d6674

Please sign in to comment.