name system tracks a map of entry names

This commit is contained in:
2021-09-05 16:00:54 -06:00
parent 96f7da5c15
commit 795f813cfc
5 changed files with 41 additions and 7 deletions

View File

@@ -139,13 +139,18 @@
(var :Array<String> commandNames [])
(defNew [&prop :Archive archive
&prop :ArchiveUI ui]
[&mut :Array<Entry> selectedEntries []
&mut :ChangeSet lastChangeSet []
:Map<String,Command> commands (new Map)]
:Map<String,Command> commands (new Map)
:NameSystem nameSystem (new NameSystem)]
(ui.setController this)
// Add systems!
(archive.addSystem nameSystem)
(archive.addSystem (new WikipediaImageSystem))
(archive.processSystems)

View File

@@ -3,6 +3,11 @@ package nat;
import nat.ArchiveController;
interface ArchiveUI {
/**
* Accept and store a reference to the controller
*/
function setController(controller:ArchiveController):Void;
/**
* Prompt the user to enter text
*/

View File

@@ -12,6 +12,10 @@
(.trim (.toString (.readLine (Sys.stdin))))]
(controller.tryRunCommand command))))
(prop &mut :ArchiveController controller)
(method :Void setController [controller] (set this.controller controller))
(defNew [])
(method :Void enterText [prompt resolve maxLength]
@@ -44,12 +48,7 @@
->name {
(if !name
(onEmptyString)
(let [matchingEntries []]
(.process (archive.addSystem
(stringComponentSystem archive Name name
(lambda [archive e]
(matchingEntries.push e)))) archive)
(let [matchingEntries (controller.nameSystem.getEntries name)]
(case (the Array<Entry> matchingEntries)
([e] (resolve e))
([] (chooseEntry "name $name doesn't match any entries. Try again?" archive resolve))

View File

@@ -0,0 +1,8 @@
package nat.systems;
import kiss.Prelude;
import kiss.List;
import nat.System;
@:build(kiss.Kiss.build())
class NameSystem extends System {}

View File

@@ -0,0 +1,17 @@
(load "../Lib.kiss")
(prop :Map<String,Array<Entry>> entriesByName (new Map))
// Stores a map of named entries, for instant lookup by name
(defNew []
(super
->[archive e] (hasComponent e Name)
->[archive e] (let [name (readComponent archive e Name)]
(if (entriesByName.exists name)
(.push (dictGet entriesByName name) e)
(dictSet entriesByName name [e]))
// Because the if statement doesn't unify by type :(
null)))
(method :Array<Entry> getEntries [name]
(or (dictGet entriesByName name) []))