Trame client library for plain JS
This library aims to simplify integration of any web client with a trame server.
Examples
Usage
const trame = new Trame()
await trame.connect({ application: 'trame' });
trame.state.set("a", 5);
console.log(trame.state.get("b"));
trame.state.update({
a: 1,
b: 2,
});
trame.state.watch(["a"], (a) => {
console.log(`a changed to ${a}`);
})
const result = await trame.trigger("name", [arg_0, arg_1], { kwarg_0: 1, kwarg_1: 2 });
trame.refs["name", js_object];
More API examples
await trame.connect({ application: "trame" });
trame.registerDecorator()
const unsubscribeOnClose = trame.onClose((info) => {
console.log("connection closed", info);
});
unsubscribeOnClose();
const unsubscribeOnError = trame.onError((info) => {
console.log("connection error", info);
});
unsubscribeOnError();
const unsubscribeOnDisconnect = trame.onDisconnect(() => {
console.log("Client is disconnecting");
});
unsubscribeOnDisconnect();
trame.exit(timeout);
trame.disconnect();
await trame.reconnect();
console.log(trame.client);
console.log(trame.state);
trame.state.set("a", 2);
trame.state.set('b', 3);
trame.state.update({
a: 2.5,
b: 3.5,
c: 4.5,
})
console.log(trame.state.get("c"));
console.log(trame.state.get('a'));
trame.state.flush('a', 'b');
const unsubscribe = trame.state.onChange(({ type, keys }) => {
if (type === "dirty-state") {
console.log(`${keys} have changed`);
} else if (type === "new-keys") {
console.log(`${keys} have been added`);
} else {
console.log(`Unkown type(${type}) of message`)
}
});
unsubscribe();
const unsubscribe2 = trame.state.watch(
["a", "b", "c"],
(a, b, c) => {
console.log(`a(${a}) or b(${b}) or c(${c}) have changed`);
}
);
unsubscribe2();
trame.trigger("name", ['arg_0', 'arg_1'], { kwarg_0: 1, kwarg_1: 2 });
trame.refs["ref_name"] = console;