-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathsimple.js
93 lines (84 loc) · 3.18 KB
/
simple.js
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
86
87
88
89
90
91
92
93
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const WebSocket = require('ws');
require('dotenv').config();
let ConfiguredPromise = null;
const docMixin = {
/**
* An array of strings specifying which api types this mixin applies to. It works with a single
* string as well.
*/
types: ['Doc'], // "Doc" type in QIX Engine is the document, also called app.
/**
* Initialization function. Called when an instance of the specified API(s) is created
* before applying the mixins.
* @param {Object} args - Object containing init parameters.
* @param {Configuration} args.config - The enigma.js configuration.
* @param {Object} args.api - The object instance that was just created.
*/
init(args) {
// Any initialization code goes here.
console.log(`My mixin is being initialized on type ${args.api.type} with id ${args.api.id}`);
// Store the Promise constructor so we can use it later:
ConfiguredPromise = args.config.Promise;
},
/**
* Object literal containing methods that will be added to the API.
* Already existing functions with the same name cannot be overridden.
*/
extend: {
/**
* Simple tweeting function.
* @returns {Promise} A promise that when resolved means this mixin method
* has completed.
*/
tweet() {
console.log('This document is tweeting');
return ConfiguredPromise.resolve();
},
/**
* This function already exist on the doc API and will therefore cause an exception when creating
* the API.
*
* Uncomment to see the error enigma.js will throw if you try to overwrite an existing method.
*/
/* getObject: () => {
console.log('trying to override but it will not work');
} */
},
/**
* Object literal containing methods that will be overwritten to already existing API methods.
* An error is thrown if any of the specified methods does not exist.
*/
override: {
/**
* Overriding the createObject function.
* @param {Function} base This is the original function that is being overridden.
* @param {*} params The parameter list. When parameters are passed by name, enigma.js
* will add default values for parameters not supplied by the caller.
* @returns {Promise} A promise that when resolved contains the newly created
* object, or rejected if object couldn't be created.
*/
createObject(base, ...params) {
console.log('Creating object with params:', params);
return base(...params);
},
},
};
const session = enigma.create({
schema,
mixins: [docMixin],
url: `wss://${process.env.QCS_HOST}/app/SessionApp_1234`,
createSocket: (url) => new WebSocket(url, {
headers: { Authorization: `Bearer ${process.env.QCS_API_KEY}` },
}),
});
session.open()
.then((global) => global.getActiveDoc())
.then((doc) => doc.tweet().then(() => doc.createObject({ qInfo: { qType: 'custom-type' } })))
.then((object) => console.log(`Created object with type ${object.genericType} and id ${object.id}`))
.catch((error) => {
console.log('Something went wrong:', error);
process.exit(1);
})
.then(() => session.close());