|
1 |
| -(provide 'rabbit-core) |
| 1 | +;;; rabbit-core.el --- core.rabbitmacs -*- lexical-binding:t -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2017 Rabbit |
| 4 | + |
| 5 | +;; Maintainer: Rabbit <yfhj1990@hotmail.com> |
| 6 | +;; Package: rabbitmacs |
| 7 | + |
| 8 | +;; MIT License |
| 9 | + |
| 10 | +;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +;; of this software and associated documentation files (the "Software"), to deal |
| 12 | +;; in the Software without restriction, including without limitation the rights |
| 13 | +;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +;; copies of the Software, and to permit persons to whom the Software is |
| 15 | +;; furnished to do so, subject to the following conditions: |
| 16 | + |
| 17 | +;; The above copyright notice and this permission notice shall be included in all |
| 18 | +;; copies or substantial portions of the Software. |
| 19 | + |
| 20 | +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | +;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | +;; SOFTWARE. |
| 27 | + |
| 28 | +;;; Code: |
2 | 29 |
|
3 | 30 | (require 'rabbit-util)
|
4 | 31 |
|
5 |
| -(import 'rabbit-default) |
| 32 | +(import rabbit-default) |
6 | 33 | (import 'rabbit-face)
|
7 | 34 | (import 'rabbit-edit)
|
| 35 | + |
| 36 | +(defvar rabbit/store '() "rabbitmacs store.") |
| 37 | + |
| 38 | + |
| 39 | +(defun rabbit/store-creator (update) |
| 40 | + "" |
| 41 | + |
| 42 | + (let* ((curr-listener '()) |
| 43 | + (curr-modal '()) |
| 44 | + (curr-update update) |
| 45 | + (next-listener curr-listener) |
| 46 | + (is-dispatching nil) |
| 47 | + |
| 48 | + (make-imuttable-listener (lambda (fn) |
| 49 | + (when (eq next-listener curr-listener) |
| 50 | + (setq next-listener (-slice curr-listener 0))) |
| 51 | + (funcall fn) |
| 52 | + )) |
| 53 | + (get-modal (lambda () curr-modal)) |
| 54 | + |
| 55 | + (subscribe (lambda (listener) |
| 56 | + (let ((is-subscribe t)) |
| 57 | + |
| 58 | + (make-imuttable-listener |
| 59 | + (lambda () |
| 60 | + (push listener curr-listener))) |
| 61 | + |
| 62 | + (lambda () |
| 63 | + "Unsubscribe." |
| 64 | + (when is-subscribe |
| 65 | + (setq is-subscribe nil) |
| 66 | + (make-imuttable-listener |
| 67 | + (lambda () |
| 68 | + (let (idx (-elem-index listener curr-listener)) |
| 69 | + (-remove-at idx curr-listener))))))))) |
| 70 | + |
| 71 | + (dispatch (lambda (msg) |
| 72 | + "Dispatch msg" |
| 73 | + |
| 74 | + (print curr-update) |
| 75 | + |
| 76 | + (unless is-dispatching |
| 77 | + (setq is-dispatching (not is-dispatching)) |
| 78 | + (setq curr-modal (curr-update curr-modal msg)) |
| 79 | + (setq is-dispatching (not is-dispatching)) |
| 80 | + |
| 81 | + ;; fire listeners |
| 82 | + (-each curr-listener |
| 83 | + (lambda (listener) |
| 84 | + (funcall listener)))))) |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | + (setq rabbit/store (list :type :store |
| 89 | + :dispatch 'dispatch |
| 90 | + :subscribe 'subscribe |
| 91 | + :get-modal 'get-modal |
| 92 | + )))) |
| 93 | + |
| 94 | +(rabbit/store-creator (lambda (state action) |
| 95 | + (print action) |
| 96 | + state)) |
| 97 | + |
| 98 | +(funcall (plist-get rabbit/store :dispatch) 123) |
| 99 | + |
| 100 | +(defun rabbit/listener-creator (fn) |
| 101 | + "Create listener." |
| 102 | + (list :type :listener |
| 103 | + :call fn)) |
| 104 | + |
| 105 | + |
| 106 | +(defun defmodal (body) |
| 107 | + "" |
| 108 | + (let* ((model rabbit/modal) |
| 109 | + (key (intern (buffer-name)))) |
| 110 | + (plist-put rabbit/modal key body))) |
| 111 | + |
| 112 | +(defmodal '((a . 1) (b . 2))) |
| 113 | + |
| 114 | +(defun rabbit/dispatch (store) |
| 115 | + "dispatch a action." |
| 116 | + (lambda (action) |
| 117 | + |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | +(rabbit/listener-creator |
| 122 | + (lambda (x) (print x) )) |
| 123 | + |
| 124 | + |
| 125 | +(defun rabbit/subscription () |
| 126 | + "" |
| 127 | + |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +(provide 'rabbit-core) |
| 135 | + |
| 136 | +;; Local Variables: |
| 137 | +;; coding: utf-8 |
| 138 | +;; End: |
| 139 | +;;; rabbit-core.el ends here |
0 commit comments