-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure context is always provided as React state.
Prefer `:as` over `:refer` for uix.
- Loading branch information
Showing
9 changed files
with
49 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
(ns ogres.app.provider.events | ||
(:require [uix.core :refer [$ defui create-context use-callback use-context use-effect]])) | ||
(:require [uix.core :as uix :refer [defui $]])) | ||
|
||
(def ^:private context (create-context (js/EventTarget.))) | ||
(def ^:private context (uix/create-context (js/EventTarget.))) | ||
|
||
(defn use-subscribe [topic f] | ||
(let [target (use-context context) | ||
(let [target (uix/use-context context) | ||
topic (str topic)] | ||
(use-effect | ||
(uix/use-effect | ||
(fn [] | ||
(let [handler (fn [event] (apply f (.-detail event)))] | ||
(.addEventListener target topic handler) | ||
(fn [] | ||
(.removeEventListener target topic handler)))) [target topic f]))) | ||
|
||
(defn use-publish [] | ||
(let [target (use-context context)] | ||
(use-callback | ||
(let [target (uix/use-context context)] | ||
(uix/use-callback | ||
(fn [topic & args] | ||
(.dispatchEvent target (js/CustomEvent. (str topic) #js {"detail" args}))) [target]))) | ||
|
||
(defui provider [props] | ||
($ context {:value (js/EventTarget.)} | ||
(:children props))) | ||
(let [[target] (uix/use-state (js/EventTarget.))] | ||
($ context {:value target} | ||
(:children props)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters