socket-function 0.8.5 → 0.8.8
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.
- package/mobx/UrlParam.ts +40 -0
- package/mobx/allowclient.flag +0 -0
- package/mobx/observer.tsx +50 -0
- package/mobx/promiseToObservable.tsx +32 -0
- package/package.json +3 -1
package/mobx/UrlParam.ts
ADDED
|
@@ -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
|
+
}
|
|
File without changes
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { observable, Reaction, IObservable } from "mobx";
|
|
2
|
+
|
|
3
|
+
export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefined } {
|
|
4
|
+
let isDone = false;
|
|
5
|
+
let error: unknown;
|
|
6
|
+
let result: T | undefined;
|
|
7
|
+
|
|
8
|
+
let isDoneTrigger = observable({ value: false });
|
|
9
|
+
promise.then(
|
|
10
|
+
r => {
|
|
11
|
+
result = r;
|
|
12
|
+
isDoneTrigger.value = true;
|
|
13
|
+
isDone = true;
|
|
14
|
+
},
|
|
15
|
+
e => {
|
|
16
|
+
error = e;
|
|
17
|
+
isDoneTrigger.value = true;
|
|
18
|
+
isDone = true;
|
|
19
|
+
}
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
get value() {
|
|
24
|
+
if (isDone) {
|
|
25
|
+
if (error) throw error;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
isDoneTrigger.value;
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "socket-function",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.8",
|
|
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
|
},
|