1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-14 15:20:06 +02:00
copilot/spikes/stacks/redux-thunk/src/client/worker.js

145 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-10-14 12:45:12 +03:00
const uuid = require('node-uuid');
const Emitter = require('component-emitter');
const crosstab = require('crosstab');
const values = require('lodash.values');
2016-10-17 03:30:20 +03:00
const actions = require('./state/actions');
2016-10-14 12:45:12 +03:00
const emitter = module.exports = new Emitter();
let isMaster = crosstab.util.tabs['MASTER_TAB'].id === crosstab.id;
const bridge = new Map();
const {
2016-10-17 00:35:09 +03:00
updatePrinters,
updateWorkerId
2016-10-14 12:45:12 +03:00
} = actions;
2016-10-17 00:35:09 +03:00
const printers = {
'1': {
id: '1',
name: 'Main printer',
lock: ''
},
'2': {
id: '2',
name: 'Handled printer',
lock: ''
}
};
2016-10-14 12:45:12 +03:00
const handlers = {
2016-10-17 00:35:09 +03:00
'PRINT': (action, fn) => {
fn();
},
2016-10-14 12:45:12 +03:00
'LOCK_PRINTER': (action, fn) => {
2016-10-17 00:35:09 +03:00
const alreadyLocked = values(printers).filter((printer) => {
return printer.lock === action._origin;
});
alreadyLocked.forEach((printer) => {
printers[printer.id] = {
...printers[printer.id],
lock: ''
};
});
printers[action.payload] = {
...printers[action.payload],
lock: action._origin
};
2016-10-14 12:45:12 +03:00
fn();
}
};
crosstab.util.events.on('message', ({
data,
origin
}) => {
if (origin === crosstab.id) {
return;
}
if (!data || !data.type) {
return;
}
if (!data._id) {
return emitter.emit('action', data);
}
const b = bridge.get(data._id);
if (b) {
data.error ? b.reject(new Error(data.error)) : b.resolve(data.payload);
return bridge.delete(data._id);
}
if (!handlers[data.type]) {
return emitter.emit('action', data);
}
2016-10-17 00:35:09 +03:00
handlers[data.type]({
...data,
_origin: origin
}, (err, res) => {
2016-10-14 12:45:12 +03:00
crosstab.broadcast('message', {
...data,
error: err && err.message
}, origin);
});
});
crosstab.util.events.on(crosstab.util.eventTypes.becomeMaster, () => {
isMaster = true;
});
crosstab.util.events.on(crosstab.util.eventTypes.demoteFromMaster, () => {
isMaster = false;
});
const dispatch = module.exports.dispatch = (action, tab) => {
if (isMaster && !tab) {
if (handlers[action.type]) {
return new Promise(function(resolve, reject) {
2016-10-17 00:35:09 +03:00
handlers[action.type]({
...action,
_origin: crosstab.id
}, function(err, res) {
2016-10-14 12:45:12 +03:00
return err ? reject(err) : resolve(res);
});
});
}
}
const id = uuid.v4();
const then = new Promise((resolve, reject) => {
bridge.set(id, {
resolve,
reject
});
});
crosstab.broadcast('message', {
...action,
_id: id
}, tab);
return then;
};
2016-10-17 00:35:09 +03:00
setTimeout(function() {
emitter.emit('action', updateWorkerId(crosstab.id));
}, 450);
2016-10-14 12:45:12 +03:00
setInterval(() => {
if (!isMaster) {
return;
}
const action = updatePrinters(values(printers));
emitter.emit('action', action);
dispatch(action);
}, 500);