Skip to content

Instantly share code, notes, and snippets.

@kofno
Created December 14, 2018 19:46
Show Gist options
  • Select an option

  • Save kofno/f3cafc0a677ead3ed38b51be3e2e21fc to your computer and use it in GitHub Desktop.

Select an option

Save kofno/f3cafc0a677ead3ed38b51be3e2e21fc to your computer and use it in GitHub Desktop.

Revisions

  1. kofno created this gist Dec 14, 2018.
    41 changes: 41 additions & 0 deletions store.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // - 1 -
    import { just, Maybe, nothing } from 'maybeasy';
    import { action, computed, observable } from 'mobx';
    import { Alert } from './types';

    class AlertsStore {
    // - 2 -
    @observable
    alerts: Alert[] = [];

    // - 3 -
    @computed
    get current(): Maybe<Alert> {
    return this.alerts.length === 0 ? nothing() : just(this.alerts[0]);
    }

    // - 4 -
    @action
    hide = () => {
    if (this.alerts.length > 0) {
    this.alerts[0].display = false;
    }
    };

    // - 5 -
    @action
    process = () => {
    this.alerts = this.alerts.slice(1);
    };

    // - 6 -
    @action
    push = (alert: Alert) => {
    this.hide();
    this.alerts.push({ ...alert, display: true });
    };
    }

    // - 7 -
    const alertsStore = new AlertsStore();
    export default alertsStore;