Mobtime SDK
A mobtime NodeJS/browser SDK
Example
(You can also check out example.js for some "tested" code)
import { Mobtime, Message, nodeWebsocket } from "@mobtime/sdk";
const onMobUpdate = (message, mobtime) => {
const mob = mobtime.mob();
const toString = m => m.name;
console.log("mob updated", {
changed: mob.changedItems().map(toString),
removed: mob.removedItems().map(toString),
all: mob.items().map(toString),
});
};
const onGoalsUpdate = (message, mobtime) => {
const goals = mobtime.goals();
const toString = g => `[${g.completed ? "X" : " "}] ${g.text}`;
console.log("goals updated", {
changed: goals.changedItems().map(toString),
removed: goals.removedItems().map(toString),
all: goals.items().map(toString),
});
};
const main = mobtime =>
new Promise((resolve, reject) => {
let intervalHandle = null;
const onClose = closeFn => () => {
clearInterval(intervalHandle);
closeFn();
};
mobtime.on(Message.MOB_UPDATE, onMobUpdate);
mobtime.on(Message.GOALS_UPDATE, onGoalsUpdate);
mobtime.on("close", onClose(resolve));
mobtime.on("error", onClose(reject));
mobtime
.mob()
.add("Alex", "gh:mrozbarry")
.add("Debbie")
.commit();
mobtime
.goals()
.add("Write great code", "good-day")
.add("Be the best team")
.commit();
mobtime
.settings()
.change("duration", 5 * 60 * 1000)
.change("mobOrder", "driver,navigator")
.change("mobOrder", ["driver", "navigator"])
.commit();
mobtime
.timer()
.start(4 * 60 * 1000)
.start()
.commit();
mobtime
.timer()
.pause()
.resume()
.commit();
setInterval(() => {
if (!mobtimer.timer().isRunning()) return;
const remaining = mobtime.timer().remainingMilliseconds();
console.log("Timer countdown", remaining);
if (remaining === 0) {
mobtime
.timer()
.complete()
.commit();
}
}, 250);
});
new Mobtime()
.withSocket(
nodeWebsocket(
"your-timer-id",
{
domain: "mobti.me",
secure: true,
},
),
)
.then(main)
.catch(error => {
console.error("mobtime connection failed", error);
process.exit(1);
})
.then(() => process.exit(0));
A note on immutability
The mob, goals, settings, and timer classes are immutable, meaning that when you cue up changes, those changes all exist in isolation.
What this means is this will not work as expected:
mobtime.mob().add("Joe");
mobtime.mob().add("Jane");
mobtime.mob().commit();
In reality, this will behave as if you didn't make those changes.
Instead, you should either track the updated Mob
reference, or use chaining.
let mob = mobtime.mob();
mob = mob.add("Joe");
mob = mob.add("Jane");
mob.commit();
mobtime
.mob()
.add("Joe")
.add("Jane")
.commit();
Testing
Mobtime is essential an input/output blackbox, which means you can safely test outputs given a set of inputs.
The only kicker is that inputs are a combination of user input (or calling functions) and responding to socket events.
Luckily, you can make a custom test socket.
Test sockets
Use @mobtime/sdk/support/socket.js
import { Mobtime, Message } from "@mobtime/sdk";
import { Socket } from "@mobtime/sdk/support/socket.js";
new Mobtime().usingSocket(Socket.use()).then(mobtime => {
const { socket } = mobtime;
});
Roll your own
You can also extend BaseSocket
if you do not like how the default support socket is set up.
import { BaseSocket } from "@mobtime/sdk";
export class TestSocket extends BaseSocket {
constructor(timerId) {
super(timerId);
}
}
TestSocket.use = timerId => Promise.resolve(new TestSocket(timerId));