socket-function 0.8.3 → 0.8.6

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
 
@@ -1,10 +1,12 @@
1
+ module.allowclient = true;
2
+
1
3
  import { SocketFunction } from "../SocketFunction";
2
4
  import { cache, lazy } from "../src/caching";
3
5
  import * as fs from "fs";
4
6
 
5
7
  /** Hot reloads server and client files, just trigger a refresh clientside,
6
8
  * while triggering per file re-evaluation and export updates serverside.
7
- * - Requires HotReloadController to be exposed.
9
+ * - Requires HotReloadController to be exposed both serverside and clientside.
8
10
  */
9
11
  export function watchFilesAndTriggerHotReloading() {
10
12
  setInterval(() => {
@@ -43,6 +45,8 @@ function triggerClientSideReload() {
43
45
  }
44
46
 
45
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!)
46
50
  async watchFiles() {
47
51
  let callerId = HotReloadController.context.caller?.nodeId;
48
52
  if (!callerId) {
@@ -0,0 +1,40 @@
1
+ import { observable } from "mobx";
2
+ import { isNode } from "../src/misc";
3
+
4
+ // TODO: Batch url state changes
5
+ // TODO: Create actual links, that a tag can use
6
+ // - Then after that, support not reloading the page, instead just setting the observables,
7
+ // for faster navigation.
8
+
9
+ export class UrlParam<T> {
10
+ constructor(private key: string, private defaultValue: T) { }
11
+ valueSeqNum = observable({ value: 1 });
12
+ public get(): T {
13
+ urlBackSeqNum.value;
14
+ this.valueSeqNum.value;
15
+ let value = new URL(location.href).searchParams.get(this.key);
16
+ if (value === null) {
17
+ return this.defaultValue;
18
+ }
19
+ return JSON.parse(value) as T;
20
+ }
21
+ public set(value: T) {
22
+ let url = new URL(location.href);
23
+ url.searchParams.set(this.key, JSON.stringify(value));
24
+ history.pushState({}, "", url.toString());
25
+ this.valueSeqNum.value++;
26
+ }
27
+ public reset() {
28
+ let url = new URL(location.href);
29
+ url.searchParams.delete(this.key);
30
+ history.pushState({}, "", url.toString());
31
+ this.valueSeqNum.value++;
32
+ }
33
+ }
34
+
35
+ let urlBackSeqNum = observable({ value: 1 });
36
+ if (!isNode()) {
37
+ window.addEventListener("popstate", () => {
38
+ urlBackSeqNum.value++;
39
+ });
40
+ }
@@ -0,0 +1,50 @@
1
+ import type preact from "preact";
2
+ import { setFlag } from "../require/compileFlags";
3
+
4
+ import { observable, Reaction } from "mobx";
5
+
6
+ setFlag(require, "mobx", "allowclient", true);
7
+ setFlag(require, "preact", "allowclient", true);
8
+
9
+ let globalConstructOrder = 0;
10
+
11
+ export function observer<
12
+ T extends {
13
+ new(...args: any[]): {
14
+ render(): preact.ComponentChild;
15
+ forceUpdate(callback?: () => void): void;
16
+ componentWillUnmount?(): void;
17
+ }
18
+ }
19
+ >(
20
+ Constructor: T
21
+ ) {
22
+ let name = Constructor.name;
23
+ return class extends Constructor {
24
+ // NOTE: This is completely valid javascript. For some reason (https://github.com/microsoft/TypeScript/pull/12065#issuecomment-270205513)
25
+ // the typescript team decided, whatever, just make it an error, even though it isn't in es6 ("we should simplify ES6' semantics").
26
+ // So, instead of simplifying ES6 semantics, lets give ourself better info for debugging...
27
+ // @ts-ignore
28
+ static get name() { return Constructor.name; }
29
+
30
+ // It is always true, that a parent has a constructOrder < a child's constructOrder
31
+ constructOrder = globalConstructOrder++;
32
+
33
+ reaction = new Reaction(`render.${name}.${this.constructOrder}`, () => {
34
+ super.forceUpdate();
35
+ });
36
+
37
+ componentWillUnmount() {
38
+ this.reaction.dispose();
39
+ super.componentWillUnmount?.();
40
+ }
41
+
42
+ render() {
43
+ let output: preact.ComponentChild;
44
+ this.reaction.track(() => {
45
+ output = super.render();
46
+ });
47
+ return output;
48
+ }
49
+ };
50
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.8.3",
3
+ "version": "0.8.6",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -9,6 +9,8 @@
9
9
  "@types/ws": "^8.5.3",
10
10
  "cookie": "^0.5.0",
11
11
  "debugbreak": "^0.6.5",
12
+ "mobx": "^6.6.2",
13
+ "preact": "^10.10.6",
12
14
  "typenode": "^0.5.6",
13
15
  "ws": "^8.8.0"
14
16
  },