socket-function 0.8.2 → 0.8.5

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