socket-function 0.8.18 → 0.8.20

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.
@@ -3,7 +3,7 @@ module.allowclient = true;
3
3
  import debugbreak from "debugbreak";
4
4
  import * as tls from "tls";
5
5
  import { getCallObj } from "./src/nodeProxy";
6
- import { Args } from "./src/types";
6
+ import { Args, MaybePromise } from "./src/types";
7
7
 
8
8
  export const socket = Symbol("socket");
9
9
 
@@ -45,7 +45,7 @@ export interface FullCallType extends CallType {
45
45
  }
46
46
 
47
47
  export interface SocketFunctionHook<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> {
48
- (config: HookContext<ExposedType, CallContext>): Promise<void>;
48
+ (config: HookContext<ExposedType, CallContext>): MaybePromise<void>;
49
49
  }
50
50
  export type HookContext<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> = {
51
51
  call: CallType;
@@ -60,7 +60,7 @@ export type ClientHookContext<ExposedType extends SocketExposedInterface = Socke
60
60
  overrideResult?: unknown;
61
61
  };
62
62
  export interface SocketFunctionClientHook<ExposedType extends SocketExposedInterface = SocketExposedInterface, CallContext extends CallContextType = CallContextType> {
63
- (config: ClientHookContext<ExposedType, CallContext>): Promise<void>;
63
+ (config: ClientHookContext<ExposedType, CallContext>): MaybePromise<void>;
64
64
  }
65
65
 
66
66
  export type CallContextType = {
@@ -1,16 +1,22 @@
1
1
  import { observable, Reaction, IObservable } from "mobx";
2
2
  import { cacheLimited } from "../src/caching";
3
3
 
4
- export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefined } {
4
+ interface InternalResult {
5
+ result: { value: unknown } | undefined;
6
+ }
7
+ export function promiseToObservable<T>(promise: Promise<T>, staleValue?: T): { value: T | undefined } {
5
8
  let isDone = false;
6
9
  let error: unknown;
7
10
  let result: T | undefined;
8
11
 
12
+ let internalResult: InternalResult = { result: undefined };
13
+
9
14
  let isDoneTrigger = observable({
10
15
  value: false
11
16
  });
12
17
  promise.then(
13
18
  r => {
19
+ internalResult.result = { value: r };
14
20
  result = r;
15
21
  isDoneTrigger.value = true;
16
22
  isDone = true;
@@ -22,16 +28,16 @@ export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefi
22
28
  }
23
29
  );
24
30
 
25
- return {
31
+ return Object.assign({
26
32
  get value() {
27
33
  if (isDone) {
28
34
  if (error) throw error;
29
35
  return result;
30
36
  }
31
37
  isDoneTrigger.value;
32
- return undefined;
38
+ return staleValue;
33
39
  }
34
- };
40
+ }, { internalResult });
35
41
  }
36
42
 
37
43
  export function asyncObservable<Output, Args extends any[]>(maxCount: number, getValue: (...args: Args) => Promise<Output>): {
@@ -45,22 +51,37 @@ export function asyncObservable<Output, Args extends any[]>(maxCount: number, ge
45
51
  valueObs: { value: Output | undefined };
46
52
  seqNum: { value: number };
47
53
  }>();
54
+ let staleValues = new Map<string, Output>();
48
55
 
49
56
  get["invalidate"] = (...args: Args) => {
50
57
  let hash = JSON.stringify(args);
51
58
  let value = values.get(hash);
52
59
  if (!value) return;
60
+ // Access the value specially, to prevent causing a mobx subscription
61
+ let internalValue = (value.valueObs as any as { internalResult: InternalResult }).internalResult;
62
+ if (internalValue.result) {
63
+ staleValues.set(hash, internalValue.result.value as any);
64
+ }
53
65
  values.delete(hash);
54
66
  startingCalculating.delete(hash);
55
67
  value.seqNum.value++;
56
68
  };
57
69
  get["invalidateAll"] = () => {
70
+ // HACK: It sucks to have to iterate over everything, but... our maxCount is limited anyway, so... hopefully it isn't too slow?
71
+ // TODO: If we are iterating anyway... we should probably just store a LRU queue for cache eviction...
72
+ for (let [hash, value] of values) {
73
+ // Access the value specially, to prevent causing a mobx subscription
74
+ let internalValue = (value.valueObs as any as { internalResult: InternalResult }).internalResult;
75
+ if (internalValue.result) {
76
+ staleValues.set(hash, internalValue.result.value as any);
77
+ }
78
+ }
79
+
58
80
  startingCalculating.clear();
59
81
  values.clear();
60
82
  invalidateAllSeqNum.seqNum++;
61
83
  };
62
84
  function get(...args: Args) {
63
-
64
85
  let hash = JSON.stringify(args);
65
86
  let value = values.get(hash);
66
87
  if (!value) {
@@ -73,13 +94,14 @@ export function asyncObservable<Output, Args extends any[]>(maxCount: number, ge
73
94
  // Not very efficient, but clearing the entire state is a lot easier to do then
74
95
  // keep track of the order they are accessed (and it does make MOST accesses
75
96
  // MUCH faster).
76
- if (values.size >= maxCount) {
97
+ if (values.size >= maxCount || staleValues.size > maxCount) {
77
98
  values.clear();
78
99
  startingCalculating.clear();
100
+ staleValues.clear();
79
101
  }
80
102
 
81
103
  // We call inside another function so that synchronous errors still get wrapped in the observable
82
- let valueObs = promiseToObservable((async () => getValue(...args))());
104
+ let valueObs = promiseToObservable((async () => getValue(...args))(), staleValues.get(hash));
83
105
  value = {
84
106
  valueObs,
85
107
  seqNum: observable({ value: 1 }, undefined, { deep: false, proxy: false }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.8.18",
3
+ "version": "0.8.20",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {