2020-05-01 05:54:12 +03:00
|
|
|
import Share from './components/lib/sole';
|
2020-05-09 06:51:18 +03:00
|
|
|
export default class Store {
|
2020-05-01 05:54:12 +03:00
|
|
|
constructor() {
|
2020-05-08 00:15:59 +03:00
|
|
|
this.state = this.initialState();
|
|
|
|
this.sync = this.sync.bind(this);
|
|
|
|
this.print = this.print.bind(this);
|
2020-05-09 06:51:18 +03:00
|
|
|
this.buffer = new Share();
|
2020-05-08 00:15:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
initialState() {
|
|
|
|
return {
|
2020-05-01 05:54:12 +03:00
|
|
|
txt: [],
|
|
|
|
prompt: '',
|
|
|
|
cursor: 0,
|
|
|
|
input: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:58:13 +03:00
|
|
|
clear() {
|
|
|
|
this.handleEvent({
|
|
|
|
data: { clear: true }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:54:12 +03:00
|
|
|
handleEvent(data) {
|
|
|
|
// recursive handler
|
|
|
|
if (data.data) {
|
|
|
|
var dojoReply = data.data;
|
|
|
|
} else {
|
|
|
|
var dojoReply = data;
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:15:59 +03:00
|
|
|
if (dojoReply.clear) {
|
|
|
|
this.setState(this.initialState(), (() => {
|
|
|
|
return;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:54:12 +03:00
|
|
|
// %mor sole-effects are nested, so throw back to handler
|
|
|
|
if (dojoReply.map) {
|
|
|
|
return dojoReply.map(reply => this.handleEvent(reply));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Object.keys(dojoReply)[0]) {
|
|
|
|
case 'txt':
|
|
|
|
return this.print(dojoReply.txt);
|
|
|
|
case 'tab':
|
|
|
|
this.print(dojoReply.tab.match + ' ' + dojoReply.tab.info);
|
|
|
|
return;
|
|
|
|
case 'tan':
|
|
|
|
return dojoReply.tan.split('\n').map(this.print);
|
|
|
|
case 'pro':
|
|
|
|
return this.setState({ prompt: dojoReply.pro.cad });
|
|
|
|
case 'hop':
|
|
|
|
return this.setState({ cursor: dojoReply.hop });
|
|
|
|
case 'det':
|
2020-05-09 06:51:18 +03:00
|
|
|
this.buffer.receive(dojoReply.det);
|
2020-05-01 05:54:12 +03:00
|
|
|
return this.sync(dojoReply.det.ted);
|
|
|
|
case 'act':
|
|
|
|
switch (dojoReply.act) {
|
|
|
|
case 'clr': return this.setState({ txt: [] });
|
|
|
|
case 'nex': return this.setState({
|
|
|
|
input: '',
|
|
|
|
cursor: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: console.log(dojoReply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doEdit(ted) {
|
2020-05-09 06:51:18 +03:00
|
|
|
const detSend = this.buffer.transmit(ted);
|
2020-05-01 05:54:12 +03:00
|
|
|
this.sync(ted);
|
2020-05-12 00:58:13 +03:00
|
|
|
return this.api.soto({ det: detSend });
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
print(txt) {
|
|
|
|
const textLog = this.state.txt;
|
|
|
|
textLog.push(txt);
|
|
|
|
return this.setState({ txt: textLog });
|
|
|
|
}
|
|
|
|
|
|
|
|
sync(ted) {
|
|
|
|
return this.setState({
|
2020-05-09 06:51:18 +03:00
|
|
|
input: this.buffer.buf,
|
|
|
|
cursor: this.buffer.transpose(ted, this.state.cursor)
|
2020-05-01 05:54:12 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setStateHandler(setState) {
|
|
|
|
this.setState = setState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|