Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/client/lib/Output.ts
1028 views
1
import { inspect } from 'util';
2
import CoreSession from './CoreSession';
3
import ObjectObserver from './ObjectObserver';
4
5
export default class Output<T = any> extends Array<T> {
6
[key: string]: any;
7
8
toJSON(): any {
9
if (this.length && Object.keys(this).every(x => !Number.isNaN(x))) {
10
return [...this];
11
}
12
const result: any = {};
13
for (const [key, value] of Object.entries(this)) {
14
result[key] = value;
15
}
16
return result;
17
}
18
19
[inspect.custom](): any {
20
return this.toJSON();
21
}
22
}
23
24
export function createObservableOutput<T>(coreSession: Promise<CoreSession>): Output<T> {
25
const observable = new ObjectObserver(new Output());
26
observable.onChanges = changes => {
27
const changesToRecord = changes.map(change => ({
28
type: change.type,
29
value: change.value,
30
path: JSON.stringify(change.path),
31
timestamp: new Date(),
32
}));
33
coreSession.then(x => x.recordOutput(changesToRecord)).catch(() => null);
34
};
35
return observable.proxy;
36
}
37
38