File size: 2,599 Bytes
31e8457
 
 
e391660
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391660
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as Effect from "../../Effect.js";
import * as Semaphore from "../../Semaphore.js";
/**
 * Builds a service with a `run` method that buffers writes until `run` installs
 * a writer, replays buffered writes with their original contexts, and restores
 * the previous writer when the run ends.
 *
 * @category services
 * @since 4.0.0
 */
export const withRun = () => f => Effect.suspend(() => {
  const semaphore = Semaphore.makeUnsafe(1);
  let buffer = [];
  let write = (...args) => Effect.contextWith(context => {
    buffer.push([args, context]);
    return Effect.void;
  });
  return Effect.map(f((...args) => write(...args)), a => ({
    ...a,
    run(f) {
      return semaphore.withPermits(1)(Effect.gen(function* () {
        const prev = write;
        write = f;
        for (const [args, context] of buffer) {
          yield* Effect.provideContext(Effect.suspend(() => f(...args)), context);
        }
        buffer = [];
        return yield* Effect.onExit(Effect.never, () => {
          write = prev;
          return Effect.void;
        });
      }));
    }
  }));
});
/**
 * Builds an RPC client protocol service that tracks active client IDs and
 * buffers server responses per client until that client's `run` handler is
 * installed.
 *
 * @category services
 * @since 4.0.0
 */
export const withRunClient = f => Effect.suspend(() => {
  const clientIds = new Set();
  const clientBuffers = new Map();
  const clientWrites = new Map();
  let write = (clientId, data) => Effect.contextWith(context => {
    let buffer = clientBuffers.get(clientId);
    if (!buffer) {
      buffer = [];
      clientBuffers.set(clientId, buffer);
    }
    buffer.push([data, context]);
    return Effect.void;
  });
  return Effect.map(f((clientId, data) => {
    const clientWrite = clientWrites.get(clientId);
    if (clientWrite) {
      return clientWrite(data);
    }
    return write(clientId, data);
  }, clientIds), a => ({
    ...a,
    run(clientId, f) {
      return Effect.gen(function* () {
        clientIds.add(clientId);
        clientWrites.set(clientId, f);
        const buffer = clientBuffers.get(clientId);
        if (buffer) {
          clientBuffers.delete(clientId);
          for (const [args, context] of buffer) {
            yield* Effect.provideContext(Effect.suspend(() => f(args)), context);
          }
        }
        return yield* Effect.onExit(Effect.never, () => {
          clientIds.delete(clientId);
          clientWrites.delete(clientId);
          return Effect.void;
        });
      });
    }
  }));
});
//# sourceMappingURL=Utils.js.map