diff options
| author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-08 15:19:25 +0900 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-08 15:19:25 +0900 | 
| commit | 0183161145d36cbafb7dbd86ca3a1aac6faca43f (patch) | |
| tree | aed757bfb5f8789156439d1e1fdff4e221376aaa /src/shared | |
| parent | 0f54a203dba38acdd080a928cee95f875fe84706 (diff) | |
| parent | 57f798044d32ba7f9dc10a34ac31ad5dbdbf56ae (diff) | |
Merge pull request #22 from ueokande/separate-domains
Refactor: Separate domains
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/messages.js | 16 | ||||
| -rw-r--r-- | src/shared/store/index.js | 53 | 
2 files changed, 69 insertions, 0 deletions
| diff --git a/src/shared/messages.js b/src/shared/messages.js new file mode 100644 index 0000000..138f0e0 --- /dev/null +++ b/src/shared/messages.js @@ -0,0 +1,16 @@ +export default { +  SETTINGS_QUERY: 'settings.query', + +  BACKGROUND_OPERATION: 'background.operation', + +  CONSOLE_BLURRED: 'console.blured', +  CONSOLE_ENTERED: 'console.entered', +  CONSOLE_QUERY_COMPLETIONS: 'console.query.completions', +  CONSOLE_SHOW_COMMAND: 'console.show.command', +  CONSOLE_SHOW_ERROR: 'console.show.error', +  CONSOLE_HIDE: 'console.hide', + +  OPEN_URL: 'open.url', + +  SETTINGS_RELOAD: 'settings.reload', +}; diff --git a/src/shared/store/index.js b/src/shared/store/index.js new file mode 100644 index 0000000..2fafdf1 --- /dev/null +++ b/src/shared/store/index.js @@ -0,0 +1,53 @@ +class Store { +  constructor(reducer, catcher) { +    this.reducer = reducer; +    this.catcher = catcher; +    this.subscribers = []; +    try { +      this.state = this.reducer(undefined, {}); +    } catch (e) { +      catcher(e); +    } +  } + +  dispatch(action, sender) { +    if (action instanceof Promise) { +      action.then((a) => { +        this.transitNext(a, sender); +      }).catch((e) => { +        this.catcher(e, sender); +      }); +    } else { +      try { +        this.transitNext(action, sender); +      } catch (e) { +        this.catcher(e, sender); +      } +    } +    return action; +  } + +  getState() { +    return this.state; +  } + +  subscribe(callback) { +    this.subscribers.push(callback); +  } + +  transitNext(action, sender) { +    let newState = this.reducer(this.state, action); +    if (JSON.stringify(this.state) !== JSON.stringify(newState)) { +      this.state = newState; +      this.subscribers.forEach(f => f(sender)); +    } +  } +} + +const empty = () => {}; + +const createStore = (reducer, catcher = empty) => { +  return new Store(reducer, catcher); +}; + +export { createStore }; | 
