socket-function 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,13 +33,13 @@ export interface SocketFunctionHook<ExposedType extends SocketExposedInterface =
33
33
  export type HookContext<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> = {
34
34
  call: CallType;
35
35
  context: SocketRegistered["context"];
36
- // If the result is overriden, we continue evaluating hooks and perform the final call
36
+ // If the result is overriden, we continue evaluating hooks BUT NOT perform the final call
37
37
  overrideResult?: unknown;
38
38
  };
39
39
 
40
40
  export type ClientHookContext<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> = {
41
41
  call: CallType;
42
- // If the result is overriden, we continue evaluating hooks and perform the final call
42
+ // If the result is overriden, we continue evaluating hooks BUT NOT perform the final call
43
43
  overrideResult?: unknown;
44
44
  };
45
45
  export interface SocketFunctionClientHook<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> {
@@ -0,0 +1,65 @@
1
+ import { SocketFunction } from "../SocketFunction";
2
+ import { cache, lazy } from "../src/caching";
3
+ import * as fs from "fs";
4
+
5
+ /** Hot reloads server and client files, just trigger a refresh clientside,
6
+ * while triggering per file re-evaluation and export updates serverside.
7
+ * - Requires HotReloadController to be exposed.
8
+ */
9
+ export function watchFilesAndTriggerHotReloading() {
10
+ setInterval(() => {
11
+ for (let module of Object.values(require.cache)) {
12
+ if (!module) continue;
13
+ hotReloadModule(module);
14
+ }
15
+ }, 5000);
16
+ }
17
+
18
+
19
+ const hotReloadModule = cache((module: NodeJS.Module) => {
20
+ if (!module.updateContents) return;
21
+ fs.watchFile(module.filename, { persistent: false, interval: 1000 }, (curr, prev) => {
22
+ if (curr.mtime.getTime() === prev.mtime.getTime()) return;
23
+ console.log(`Hot reloading due to change: ${module.filename}`);
24
+ module.updateContents?.();
25
+ triggerClientSideReload();
26
+ });
27
+ });
28
+ let reloadTriggering = false;
29
+ let clientWatcherNodes = new Set<string>();
30
+ function triggerClientSideReload() {
31
+ if (reloadTriggering) return;
32
+ reloadTriggering = true;
33
+ setTimeout(async () => {
34
+ reloadTriggering = false;
35
+ for (let clientNodeId of clientWatcherNodes) {
36
+ console.log(`Notifying client of hot reload: ${clientNodeId}`);
37
+ HotReloadController.nodes[clientNodeId].fileUpdated().catch(() => {
38
+ console.log(`Removing erroring client: ${clientNodeId}`);
39
+ clientWatcherNodes.delete(clientNodeId);
40
+ });
41
+ }
42
+ }, 300);
43
+ }
44
+
45
+ class HotReloadControllerBase {
46
+ async watchFiles() {
47
+ let callerId = HotReloadController.context.caller?.nodeId;
48
+ if (!callerId) {
49
+ throw new Error("No nodeId?");
50
+ }
51
+ clientWatcherNodes.add(callerId);
52
+ }
53
+ async fileUpdated() {
54
+ document.location.reload();
55
+ }
56
+ }
57
+
58
+ export const HotReloadController = SocketFunction.register(
59
+ "HotReloadController-032b2250-3aac-4187-8c95-75412742b8f5",
60
+ new HotReloadControllerBase(),
61
+ {
62
+ watchFiles: {},
63
+ fileUpdated: {}
64
+ }
65
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {