WebWorker.js 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const WorkerState = require('./WorkerState');
  2. //server states:
  3. const ssNormal = 'normal';
  4. const ssDbLoading = 'db_loading';
  5. //singleton
  6. let instance = null;
  7. class WebWorker {
  8. constructor(config) {
  9. if (!instance) {
  10. this.config = config;
  11. this.workerState = new WorkerState();
  12. this.wState = this.workerState.getControl('server_state');
  13. this.myState = '';
  14. this.loadOrCreateDb();//no await
  15. instance = this;
  16. }
  17. return instance;
  18. }
  19. checkMyState() {
  20. if (this.myState != ssNormal)
  21. throw new Error('server_busy');
  22. }
  23. setMyState(newState) {
  24. this.myState = newState;
  25. this.wState.set({state: newState});
  26. }
  27. async loadOrCreateDb() {
  28. this.setMyState(ssDbLoading);
  29. try {
  30. //
  31. } catch (e) {
  32. //
  33. } finally {
  34. this.setMyState(ssNormal);
  35. }
  36. }
  37. }
  38. module.exports = WebWorker;