0.0.1 • Published 2 years ago
context-engine v0.0.1
context-engine
Usage: Example:
Initialize context server once:
// index.js
const http = require("http");
const expressApp = require("./app");
const httpServer = http.createServer(expressApp);
const { ContextServer } = require("context-engine");
const contextServer = new ContextServer();
contextServer.attach(httpServer);
httpServer.listen(5000, () => console.log("listening on 5000"));
Anywhere in your code:
// app.js
const express = require("express");
const app = express();
const { ContextPublisher } = require("context-engine");
const cp = new ContextPublisher();
app.get("/reject", (req, res) => {
cp.publish("loveyou", "hateyou");
res.sendStatus(200);
});
app.get("/accept", (req, res) => {
cp.publish("loveyou", "loveyoutoo");
res.sendStatus(200);
});
module.exports = app;
In frontend code (on client side)
// app.js
import { useContextData } from "context-client";
function App() {
const { data } = useContextData("loveyou", {
initialData: "waitforyou",
});
return <div>{data}</div>;
}
Motivation
Use useContextData anywhere, any components you like.
The topic in useContextData
(topic
, option
) is same as in cp.publish
(topic
, message
), the message will be delivered.
message
is a string. For sending and receiving object, use JSON.stringify
and JSON.parse
, respectively.
data
is last message string received.
option
is an object, all options available are:
initialData
: Initial value ofdata
, when no new message delivered yet.
Now there is only one. More options (throttling,...) will be supported in future version.
0.0.1
2 years ago