@ddd-qc/cell-proxy 0.19.19 → 0.20.0

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/dist/CellProxy.js CHANGED
@@ -1,209 +1,209 @@
1
- import { encodeHashToBase64 } from "@holochain/client";
2
- import { anyToB64, prettyDate, prettyDuration } from "./utils";
3
- import { CellMixin, Empty } from "./mixins";
4
- import { Mutex, withTimeout } from "async-mutex";
5
- /**
6
- * Proxy for a running DNA.
7
- * It logs and queues ZomeCalls.
8
- * It holds a reference to its AppProxy and its Cell.
9
- * This class is expected to be used by ZomeProxies.
10
- */
11
- export class CellProxy extends CellMixin(Empty) {
12
- /** Ctor */
13
- constructor(_appProxy, cell,
14
- //public readonly dnaDef: MyDnaDef,
15
- defaultTimeout) {
16
- super();
17
- this._appProxy = _appProxy;
18
- /** append only logs */
19
- this._requestLog = [];
20
- this._responseLog = [];
21
- this._cell = cell;
22
- console.log(`CellProxy.ctor`, cell);
23
- this.defaultTimeout = defaultTimeout ? defaultTimeout : 10 * 1000;
24
- this._callMutex = withTimeout(new Mutex(), this.defaultTimeout);
25
- }
26
- /** -- Methods -- */
27
- /** */
28
- addSignalHandler(handler) {
29
- return this._appProxy.addSignalHandler(handler, this.cell.hcl().toString());
30
- }
31
- /** */
32
- dumpSignals() {
33
- this._appProxy.dumpSignals(this.cell.id);
34
- }
35
- /** Pass call request to conductor proxy and log it */
36
- async executeZomeCall(reqLog) {
37
- reqLog.executionTimestamp = Date.now();
38
- const requestIndex = this._requestLog.length;
39
- this._requestLog.push(reqLog);
40
- try {
41
- const response = await this._appProxy.callZome(reqLog.request, reqLog.timeout);
42
- const respLog = { requestIndex, success: response, timestamp: Date.now() };
43
- this._responseLog.push(respLog);
44
- return respLog;
45
- }
46
- catch (e) {
47
- const respLog = { requestIndex, failure: e, timestamp: Date.now() };
48
- this._responseLog.push(respLog);
49
- return respLog;
50
- }
51
- }
52
- /** Pass call request to conductor proxy and log it */
53
- logCallTimedout(reqLog) {
54
- reqLog.executionTimestamp = Date.now();
55
- const requestIndex = this._requestLog.length;
56
- this._requestLog.push(reqLog);
57
- const respLog = { requestIndex, failure: "Waiting for Mutex timed-out", timestamp: Date.now() };
58
- this._responseLog.push(respLog);
59
- return respLog;
60
- }
61
- /**
62
- * callZome() with Mutex (for calls that writes to source-chain)
63
- * TODO: Implement call queue instead of mutex?
64
- */
65
- async callZomeBlocking(zome_name, fn_name, payload, cap_secret, timeout) {
66
- timeout = timeout ? timeout : this.defaultTimeout;
67
- const req = {
68
- cap_secret, zome_name, fn_name, payload,
69
- cell_id: this.cell.id,
70
- provenance: this.cell.id[1],
71
- };
72
- const log = { request: req, timeout, requestTimestamp: Date.now() };
73
- /** Acquire lock */
74
- let release;
75
- try {
76
- release = await this._callMutex.acquire();
77
- }
78
- catch (e) {
79
- console.warn("Waiting for callZomeBlocking mutex timed-out", e);
80
- this.logCallTimedout(log);
81
- return Promise.reject("Waiting for callZomeBlocking mutex timed-out");
82
- }
83
- /** Execute */
84
- const respLog = await this.executeZomeCall(log);
85
- /** Release */
86
- release();
87
- if (respLog.failure) {
88
- this.dumpSignals();
89
- this.dumpLogs(zome_name);
90
- return Promise.reject(respLog.failure);
91
- }
92
- return respLog.success;
93
- }
94
- /** */
95
- async callZome(zome_name, fn_name, payload, cap_secret, timeout) {
96
- timeout = timeout ? timeout : this.defaultTimeout;
97
- const req = {
98
- cap_secret, zome_name, fn_name, payload,
99
- cell_id: this.cell.id,
100
- provenance: this.cell.id[1],
101
- };
102
- const log = { request: req, timeout, requestTimestamp: Date.now() };
103
- try {
104
- await this._callMutex.waitForUnlock();
105
- }
106
- catch (e) {
107
- console.warn("Waiting for callZome mutex timed-out", e);
108
- this.logCallTimedout(log);
109
- return Promise.reject("Waiting for callZome mutex timed-out");
110
- }
111
- const respLog = await this.executeZomeCall(log);
112
- if (respLog.failure) {
113
- this.dumpSignals();
114
- this.dumpLogs(zome_name);
115
- return Promise.reject(respLog.failure);
116
- }
117
- return respLog.success;
118
- }
119
- /**
120
- * Calls the `entry_defs()` zome function and
121
- * Returns an array of all the zome's AppEntryNames and Visibility, i.e. (AppEntryName, isPublic)[]
122
- */
123
- async callEntryDefs(zomeName) {
124
- //console.log("callEntryDefs()", zomeName)
125
- try {
126
- const entryDefs = await this.callZome(zomeName, "entry_defs", null, null, 2 * 1000);
127
- //console.debug("getEntryDefs() for " + this.zomeName + " result:")
128
- //console.log({entryDefs})
129
- let result = [];
130
- for (const def of entryDefs.Defs) {
131
- const name = def.id.App;
132
- result.push([name, def.visibility.hasOwnProperty('Public')]);
133
- }
134
- //console.log({result})
135
- return result;
136
- }
137
- catch (e) {
138
- console.error("Calling getEntryDefs() on " + zomeName + " failed: ");
139
- console.error({ e });
140
- return Promise.reject(e);
141
- }
142
- }
143
- /**
144
- * Calls the `zome_info()` zome function
145
- */
146
- async callZomeInfo(zomeName) {
147
- console.log("callZomeInfo()", zomeName);
148
- try {
149
- const zome_info = await this.callZome(zomeName, "get_zome_info", null, null, 10 * 100);
150
- //console.debug("callZomeInfo() for " + zomeName + " result:")
151
- //console.log({zome_info})
152
- return zome_info;
153
- }
154
- catch (e) {
155
- console.error("Calling callZomeInfo() on " + zomeName + " failed. Make sure `get_zome_info()` is implemented in your zome code. Error: ");
156
- console.error({ e });
157
- return Promise.reject(e);
158
- }
159
- }
160
- /**
161
- * Calls the `dna_info()` zome function
162
- */
163
- async callDnaInfo(zomeName) {
164
- console.log("callDnaInfo()", zomeName);
165
- try {
166
- const dna_info = await this.callZome(zomeName, "get_dna_info", null, null, 10 * 100);
167
- //console.debug("callDnaInfo() for " + zomeName + " result:")
168
- //console.log({dna_info})
169
- return dna_info;
170
- }
171
- catch (e) {
172
- console.error("Calling callDnaInfo() on " + zomeName + " failed. Make sure `get_dna_info()` is implemented in your zome code. Error: ");
173
- console.error({ e });
174
- return Promise.reject(e);
175
- }
176
- }
177
- // /** TODO once we have getDnaDefinition() api */
178
- // dumpAllZomes() {
179
- // // FIXME get DNA DEF
180
- // for (const zomeName of dnaDef) {
181
- // this.dumpLogs(zomeName)
182
- // }
183
- // }
184
- /** */
185
- dumpLogs(zomeName) {
186
- let result = [];
187
- for (const response of this._responseLog) {
188
- const requestLog = this._requestLog[response.requestIndex];
189
- if (zomeName && requestLog.request.zome_name != zomeName) {
190
- continue;
191
- }
192
- const startTime = prettyDate(new Date(requestLog.requestTimestamp));
193
- const waitTime = prettyDuration(new Date(requestLog.executionTimestamp - requestLog.requestTimestamp));
194
- const duration = prettyDuration(new Date(response.timestamp - requestLog.requestTimestamp));
195
- const input = requestLog.request.payload instanceof Uint8Array ? encodeHashToBase64(requestLog.request.payload) : requestLog.request.payload;
196
- const output = anyToB64(response.failure ? response.failure : response.success);
197
- const log = zomeName
198
- ? { startTime, fnName: requestLog.request.fn_name, input, output, duration, waitTime }
199
- : { startTime, zomeName: requestLog.request.zome_name, fnName: requestLog.request.fn_name, input, output, duration, waitTime };
200
- result.push(log);
201
- }
202
- console.warn(`Dumping logs for cell "${this._appProxy.getLocations(this.cell.id)}"`);
203
- if (zomeName) {
204
- console.warn(` - For zome "${zomeName}"`);
205
- }
206
- console.table(result);
207
- }
208
- }
1
+ import { encodeHashToBase64 } from "@holochain/client";
2
+ import { anyToB64, prettyDate, prettyDuration } from "./utils";
3
+ import { CellMixin, Empty } from "./mixins";
4
+ import { Mutex, withTimeout } from "async-mutex";
5
+ /**
6
+ * Proxy for a running DNA.
7
+ * It logs and queues ZomeCalls.
8
+ * It holds a reference to its AppProxy and its Cell.
9
+ * This class is expected to be used by ZomeProxies.
10
+ */
11
+ export class CellProxy extends CellMixin(Empty) {
12
+ /** Ctor */
13
+ constructor(_appProxy, cell,
14
+ //public readonly dnaDef: MyDnaDef,
15
+ defaultTimeout) {
16
+ super();
17
+ this._appProxy = _appProxy;
18
+ /** append only logs */
19
+ this._requestLog = [];
20
+ this._responseLog = [];
21
+ this._cell = cell;
22
+ console.log(`CellProxy.ctor`, cell);
23
+ this.defaultTimeout = defaultTimeout ? defaultTimeout : 10 * 1000;
24
+ this._callMutex = withTimeout(new Mutex(), this.defaultTimeout);
25
+ }
26
+ /** -- Methods -- */
27
+ /** */
28
+ addSignalHandler(handler) {
29
+ return this._appProxy.addSignalHandler(handler, this.cell.hcl().toString());
30
+ }
31
+ /** */
32
+ dumpSignals() {
33
+ this._appProxy.dumpSignals(this.cell.id);
34
+ }
35
+ /** Pass call request to conductor proxy and log it */
36
+ async executeZomeCall(reqLog) {
37
+ reqLog.executionTimestamp = Date.now();
38
+ const requestIndex = this._requestLog.length;
39
+ this._requestLog.push(reqLog);
40
+ try {
41
+ const response = await this._appProxy.callZome(reqLog.request, reqLog.timeout);
42
+ const respLog = { requestIndex, success: response, timestamp: Date.now() };
43
+ this._responseLog.push(respLog);
44
+ return respLog;
45
+ }
46
+ catch (e) {
47
+ const respLog = { requestIndex, failure: e, timestamp: Date.now() };
48
+ this._responseLog.push(respLog);
49
+ return respLog;
50
+ }
51
+ }
52
+ /** Pass call request to conductor proxy and log it */
53
+ logCallTimedout(reqLog) {
54
+ reqLog.executionTimestamp = Date.now();
55
+ const requestIndex = this._requestLog.length;
56
+ this._requestLog.push(reqLog);
57
+ const respLog = { requestIndex, failure: "Waiting for Mutex timed-out", timestamp: Date.now() };
58
+ this._responseLog.push(respLog);
59
+ return respLog;
60
+ }
61
+ /**
62
+ * callZome() with Mutex (for calls that writes to source-chain)
63
+ * TODO: Implement call queue instead of mutex?
64
+ */
65
+ async callZomeBlocking(zome_name, fn_name, payload, cap_secret, timeout) {
66
+ timeout = timeout ? timeout : this.defaultTimeout;
67
+ const req = {
68
+ cap_secret, zome_name, fn_name, payload,
69
+ cell_id: this.cell.id,
70
+ provenance: this.cell.id[1],
71
+ };
72
+ const log = { request: req, timeout, requestTimestamp: Date.now() };
73
+ /** Acquire lock */
74
+ let release;
75
+ try {
76
+ release = await this._callMutex.acquire();
77
+ }
78
+ catch (e) {
79
+ console.warn("Waiting for callZomeBlocking mutex timed-out", e);
80
+ this.logCallTimedout(log);
81
+ return Promise.reject("Waiting for callZomeBlocking mutex timed-out");
82
+ }
83
+ /** Execute */
84
+ const respLog = await this.executeZomeCall(log);
85
+ /** Release */
86
+ release();
87
+ if (respLog.failure) {
88
+ this.dumpSignals();
89
+ this.dumpLogs(zome_name);
90
+ return Promise.reject(respLog.failure);
91
+ }
92
+ return respLog.success;
93
+ }
94
+ /** */
95
+ async callZome(zome_name, fn_name, payload, cap_secret, timeout) {
96
+ timeout = timeout ? timeout : this.defaultTimeout;
97
+ const req = {
98
+ cap_secret, zome_name, fn_name, payload,
99
+ cell_id: this.cell.id,
100
+ provenance: this.cell.id[1],
101
+ };
102
+ const log = { request: req, timeout, requestTimestamp: Date.now() };
103
+ try {
104
+ await this._callMutex.waitForUnlock();
105
+ }
106
+ catch (e) {
107
+ console.warn("Waiting for callZome mutex timed-out", e);
108
+ this.logCallTimedout(log);
109
+ return Promise.reject("Waiting for callZome mutex timed-out");
110
+ }
111
+ const respLog = await this.executeZomeCall(log);
112
+ if (respLog.failure) {
113
+ this.dumpSignals();
114
+ this.dumpLogs(zome_name);
115
+ return Promise.reject(respLog.failure);
116
+ }
117
+ return respLog.success;
118
+ }
119
+ /**
120
+ * Calls the `entry_defs()` zome function and
121
+ * Returns an array of all the zome's AppEntryNames and Visibility, i.e. (AppEntryName, isPublic)[]
122
+ */
123
+ async callEntryDefs(zomeName) {
124
+ //console.log("callEntryDefs()", zomeName)
125
+ try {
126
+ const entryDefs = await this.callZome(zomeName, "entry_defs", null, null, 2 * 1000);
127
+ //console.debug("getEntryDefs() for " + this.zomeName + " result:")
128
+ //console.log({entryDefs})
129
+ let result = [];
130
+ for (const def of entryDefs.Defs) {
131
+ const name = def.id.App;
132
+ result.push([name, def.visibility.hasOwnProperty('Public')]);
133
+ }
134
+ //console.log({result})
135
+ return result;
136
+ }
137
+ catch (e) {
138
+ console.error("Calling getEntryDefs() on " + zomeName + " failed: ");
139
+ console.error({ e });
140
+ return Promise.reject(e);
141
+ }
142
+ }
143
+ /**
144
+ * Calls the `zome_info()` zome function
145
+ */
146
+ async callZomeInfo(zomeName) {
147
+ console.log("callZomeInfo()", zomeName);
148
+ try {
149
+ const zome_info = await this.callZome(zomeName, "get_zome_info", null, null, 10 * 100);
150
+ //console.debug("callZomeInfo() for " + zomeName + " result:")
151
+ //console.log({zome_info})
152
+ return zome_info;
153
+ }
154
+ catch (e) {
155
+ console.error("Calling callZomeInfo() on " + zomeName + " failed. Make sure `get_zome_info()` is implemented in your zome code. Error: ");
156
+ console.error({ e });
157
+ return Promise.reject(e);
158
+ }
159
+ }
160
+ /**
161
+ * Calls the `dna_info()` zome function
162
+ */
163
+ async callDnaInfo(zomeName) {
164
+ console.log("callDnaInfo()", zomeName);
165
+ try {
166
+ const dna_info = await this.callZome(zomeName, "get_dna_info", null, null, 10 * 100);
167
+ //console.debug("callDnaInfo() for " + zomeName + " result:")
168
+ //console.log({dna_info})
169
+ return dna_info;
170
+ }
171
+ catch (e) {
172
+ console.error("Calling callDnaInfo() on " + zomeName + " failed. Make sure `get_dna_info()` is implemented in your zome code. Error: ");
173
+ console.error({ e });
174
+ return Promise.reject(e);
175
+ }
176
+ }
177
+ // /** TODO once we have getDnaDefinition() api */
178
+ // dumpAllZomes() {
179
+ // // FIXME get DNA DEF
180
+ // for (const zomeName of dnaDef) {
181
+ // this.dumpLogs(zomeName)
182
+ // }
183
+ // }
184
+ /** */
185
+ dumpLogs(zomeName) {
186
+ let result = [];
187
+ for (const response of this._responseLog) {
188
+ const requestLog = this._requestLog[response.requestIndex];
189
+ if (zomeName && requestLog.request.zome_name != zomeName) {
190
+ continue;
191
+ }
192
+ const startTime = prettyDate(new Date(requestLog.requestTimestamp));
193
+ const waitTime = prettyDuration(new Date(requestLog.executionTimestamp - requestLog.requestTimestamp));
194
+ const duration = prettyDuration(new Date(response.timestamp - requestLog.requestTimestamp));
195
+ const input = requestLog.request.payload instanceof Uint8Array ? encodeHashToBase64(requestLog.request.payload) : requestLog.request.payload;
196
+ const output = anyToB64(response.failure ? response.failure : response.success);
197
+ const log = zomeName
198
+ ? { startTime, fnName: requestLog.request.fn_name, input, output, duration, waitTime }
199
+ : { startTime, zomeName: requestLog.request.zome_name, fnName: requestLog.request.fn_name, input, output, duration, waitTime };
200
+ result.push(log);
201
+ }
202
+ console.warn(`Dumping logs for cell "${this._appProxy.getLocations(this.cell.id)}"`);
203
+ if (zomeName) {
204
+ console.warn(` - For zome "${zomeName}"`);
205
+ }
206
+ console.table(result);
207
+ }
208
+ }
209
209
  //# sourceMappingURL=CellProxy.js.map
@@ -1,31 +1,31 @@
1
- import { AppApi, AppInfoRequest, AppInfoResponse, AppWebsocket, CallZomeRequest, InstalledAppId, CreateCloneCellRequest, DisableCloneCellRequest, EnableCloneCellRequest, ClonedCell } from "@holochain/client";
2
- import { AppProxy } from "./AppProxy";
3
- /**
4
- * Creates, connects and holds an appWebsocket.
5
- * Creates and holds Cell proxies for this appWebsocket.
6
- * Maintains a mapping between CellIds and HCLs
7
- * Handles SignalHandlers per HCL
8
- * Stores appSignal logs
9
- * TODO Implement Singleton per App port?
10
- */
11
- export declare class ConductorAppProxy extends AppProxy implements AppApi {
12
- defaultTimeout: number;
13
- /** -- Fields -- */
14
- private _appWs;
15
- /** -- Getters -- */
16
- /** Check this after connecting since AppWebsocket can shamelessly override the provided args. */
17
- get appIdOfShame(): InstalledAppId | undefined;
18
- createCloneCell(request: CreateCloneCellRequest): Promise<ClonedCell>;
19
- /** -- AppApi (Passthrough to appWebsocket) -- */
20
- enableCloneCell(request: EnableCloneCellRequest): Promise<ClonedCell>;
21
- disableCloneCell(request: DisableCloneCellRequest): Promise<void>;
22
- appInfo(args: AppInfoRequest): Promise<AppInfoResponse>;
23
- callZome(req: CallZomeRequest, timeout?: number): Promise<unknown>;
24
- /** -- Creation -- */
25
- /** async Factory */
26
- static new(port_or_socket: number | AppWebsocket, defaultTimeout?: number): Promise<ConductorAppProxy>;
27
- /** */
28
- private static fromSocket;
29
- /** Ctor */
30
- constructor(defaultTimeout: number);
31
- }
1
+ import { AppApi, AppInfoRequest, AppInfoResponse, AppWebsocket, CallZomeRequest, InstalledAppId, CreateCloneCellRequest, DisableCloneCellRequest, EnableCloneCellRequest, ClonedCell } from "@holochain/client";
2
+ import { AppProxy } from "./AppProxy";
3
+ /**
4
+ * Creates, connects and holds an appWebsocket.
5
+ * Creates and holds Cell proxies for this appWebsocket.
6
+ * Maintains a mapping between CellIds and HCLs
7
+ * Handles SignalHandlers per HCL
8
+ * Stores appSignal logs
9
+ * TODO Implement Singleton per App port?
10
+ */
11
+ export declare class ConductorAppProxy extends AppProxy implements AppApi {
12
+ defaultTimeout: number;
13
+ /** -- Fields -- */
14
+ private _appWs;
15
+ /** -- Getters -- */
16
+ /** Check this after connecting since AppWebsocket can shamelessly override the provided args. */
17
+ get appIdOfShame(): InstalledAppId | undefined;
18
+ createCloneCell(request: CreateCloneCellRequest): Promise<ClonedCell>;
19
+ /** -- AppApi (Passthrough to appWebsocket) -- */
20
+ enableCloneCell(request: EnableCloneCellRequest): Promise<ClonedCell>;
21
+ disableCloneCell(request: DisableCloneCellRequest): Promise<void>;
22
+ appInfo(args: AppInfoRequest): Promise<AppInfoResponse>;
23
+ callZome(req: CallZomeRequest, timeout?: number): Promise<unknown>;
24
+ /** -- Creation -- */
25
+ /** async Factory */
26
+ static new(port_or_socket: number | AppWebsocket, defaultTimeout?: number): Promise<ConductorAppProxy>;
27
+ /** */
28
+ private static fromSocket;
29
+ /** Ctor */
30
+ constructor(defaultTimeout: number);
31
+ }
@@ -1,81 +1,81 @@
1
- import { AppWebsocket, } from "@holochain/client";
2
- import { AppProxy } from "./AppProxy";
3
- /**
4
- * Creates, connects and holds an appWebsocket.
5
- * Creates and holds Cell proxies for this appWebsocket.
6
- * Maintains a mapping between CellIds and HCLs
7
- * Handles SignalHandlers per HCL
8
- * Stores appSignal logs
9
- * TODO Implement Singleton per App port?
10
- */
11
- export class ConductorAppProxy extends AppProxy {
12
- /** -- Getters -- */
13
- /** Check this after connecting since AppWebsocket can shamelessly override the provided args. */
14
- get appIdOfShame() { return this._appWs.overrideInstalledAppId; }
15
- async createCloneCell(request) {
16
- //console.log("createCloneCell() called:", request)
17
- return this._appWs.createCloneCell(request);
18
- }
19
- /** -- AppApi (Passthrough to appWebsocket) -- */
20
- async enableCloneCell(request) {
21
- //console.log("enableCloneCell() called:", request)
22
- return this._appWs.enableCloneCell(request);
23
- }
24
- async disableCloneCell(request) {
25
- //console.log("disableCloneCell() called:", request)
26
- return this._appWs.disableCloneCell(request);
27
- }
28
- async appInfo(args) {
29
- return this._appWs.appInfo(args);
30
- }
31
- async callZome(req, timeout) {
32
- timeout = timeout ? timeout : this.defaultTimeout;
33
- return this._appWs.callZome(req, timeout);
34
- }
35
- /** -- Creation -- */
36
- /** async Factory */
37
- static async new(port_or_socket, defaultTimeout) {
38
- if (typeof port_or_socket == 'object') {
39
- return ConductorAppProxy.fromSocket(port_or_socket);
40
- }
41
- else {
42
- const timeout = defaultTimeout ? defaultTimeout : 10 * 1000;
43
- let wsUrl = new URL(`ws://localhost:${port_or_socket}`);
44
- try {
45
- let conductor = new ConductorAppProxy(timeout);
46
- /** AppWebsocket */
47
- const appWs = await AppWebsocket.connect(wsUrl, timeout);
48
- /** AppAgentWebsocket */
49
- // const appAgentWs = await AppAgentWebsocket.connect(`ws://localhost:${process.env.HC_APP_PORT}`, "playground");
50
- // console.log(appAgentWs.appWebsocket);
51
- // const appWs = await appAgentWs.appWebsocket;
52
- conductor._appWs = appWs;
53
- conductor._appWs.on('signal', (sig) => { conductor.onSignal(sig); });
54
- return conductor;
55
- }
56
- catch (e) {
57
- console.error("ConductorAppProxy initialization failed", e);
58
- return Promise.reject("ConductorAppProxy initialization failed");
59
- }
60
- }
61
- }
62
- /** */
63
- static async fromSocket(appWebsocket) {
64
- try {
65
- let conductor = new ConductorAppProxy(appWebsocket.defaultTimeout);
66
- conductor._appWs = appWebsocket;
67
- conductor._appWs.on('signal', (sig) => { conductor.onSignal(sig); });
68
- return conductor;
69
- }
70
- catch (e) {
71
- console.error("ConductorAppProxy initialization failed", e);
72
- return Promise.reject("ConductorAppProxy initialization failed");
73
- }
74
- }
75
- /** Ctor */
76
- /*protected*/ constructor(defaultTimeout) {
77
- super(defaultTimeout);
78
- this.defaultTimeout = defaultTimeout;
79
- }
80
- }
1
+ import { AppWebsocket, } from "@holochain/client";
2
+ import { AppProxy } from "./AppProxy";
3
+ /**
4
+ * Creates, connects and holds an appWebsocket.
5
+ * Creates and holds Cell proxies for this appWebsocket.
6
+ * Maintains a mapping between CellIds and HCLs
7
+ * Handles SignalHandlers per HCL
8
+ * Stores appSignal logs
9
+ * TODO Implement Singleton per App port?
10
+ */
11
+ export class ConductorAppProxy extends AppProxy {
12
+ /** -- Getters -- */
13
+ /** Check this after connecting since AppWebsocket can shamelessly override the provided args. */
14
+ get appIdOfShame() { return this._appWs.overrideInstalledAppId; }
15
+ async createCloneCell(request) {
16
+ //console.log("createCloneCell() called:", request)
17
+ return this._appWs.createCloneCell(request);
18
+ }
19
+ /** -- AppApi (Passthrough to appWebsocket) -- */
20
+ async enableCloneCell(request) {
21
+ //console.log("enableCloneCell() called:", request)
22
+ return this._appWs.enableCloneCell(request);
23
+ }
24
+ async disableCloneCell(request) {
25
+ //console.log("disableCloneCell() called:", request)
26
+ return this._appWs.disableCloneCell(request);
27
+ }
28
+ async appInfo(args) {
29
+ return this._appWs.appInfo(args);
30
+ }
31
+ async callZome(req, timeout) {
32
+ timeout = timeout ? timeout : this.defaultTimeout;
33
+ return this._appWs.callZome(req, timeout);
34
+ }
35
+ /** -- Creation -- */
36
+ /** async Factory */
37
+ static async new(port_or_socket, defaultTimeout) {
38
+ if (typeof port_or_socket == 'object') {
39
+ return ConductorAppProxy.fromSocket(port_or_socket);
40
+ }
41
+ else {
42
+ const timeout = defaultTimeout ? defaultTimeout : 10 * 1000;
43
+ let wsUrl = new URL(`ws://localhost:${port_or_socket}`);
44
+ try {
45
+ let conductor = new ConductorAppProxy(timeout);
46
+ /** AppWebsocket */
47
+ const appWs = await AppWebsocket.connect(wsUrl, timeout);
48
+ /** AppAgentWebsocket */
49
+ // const appAgentWs = await AppAgentWebsocket.connect(`ws://localhost:${process.env.HC_APP_PORT}`, "playground");
50
+ // console.log(appAgentWs.appWebsocket);
51
+ // const appWs = await appAgentWs.appWebsocket;
52
+ conductor._appWs = appWs;
53
+ conductor._appWs.on('signal', (sig) => { conductor.onSignal(sig); });
54
+ return conductor;
55
+ }
56
+ catch (e) {
57
+ console.error("ConductorAppProxy initialization failed", e);
58
+ return Promise.reject("ConductorAppProxy initialization failed");
59
+ }
60
+ }
61
+ }
62
+ /** */
63
+ static async fromSocket(appWebsocket) {
64
+ try {
65
+ let conductor = new ConductorAppProxy(appWebsocket.defaultTimeout);
66
+ conductor._appWs = appWebsocket;
67
+ conductor._appWs.on('signal', (sig) => { conductor.onSignal(sig); });
68
+ return conductor;
69
+ }
70
+ catch (e) {
71
+ console.error("ConductorAppProxy initialization failed", e);
72
+ return Promise.reject("ConductorAppProxy initialization failed");
73
+ }
74
+ }
75
+ /** Ctor */
76
+ /*protected*/ constructor(defaultTimeout) {
77
+ super(defaultTimeout);
78
+ this.defaultTimeout = defaultTimeout;
79
+ }
80
+ }
81
81
  //# sourceMappingURL=ConductorAppProxy.js.map