diff options
| author | Stephen Blott | 2016-03-21 12:30:57 +0000 | 
|---|---|---|
| committer | Stephen Blott | 2016-03-21 12:31:00 +0000 | 
| commit | 741bc131353eb856022d66252309de23a0873fc7 (patch) | |
| tree | dc5f2453155f4ef87c4eeb71247623997fb621cc | |
| parent | 8783569983d8b3634b1b1eed9b6560dbea5698ab (diff) | |
| download | vimium-741bc131353eb856022d66252309de23a0873fc7.tar.bz2 | |
Simplify singleton handling.
While working on the visual-mode code, it became apparent that our
current "singleton" implementation is unnecessarily complicated.
This simplifies it.  The keys are now required to be strings.
(Previously, they could be any object; which meant we needed to gove
objects an identity.  All of which was complicated.)
| -rw-r--r-- | content_scripts/mode.coffee | 18 | ||||
| -rw-r--r-- | content_scripts/mode_find.coffee | 4 | ||||
| -rw-r--r-- | content_scripts/mode_visual.coffee | 5 | ||||
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 6 | ||||
| -rw-r--r-- | lib/utils.coffee | 11 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 2 | 
6 files changed, 12 insertions, 34 deletions
| diff --git a/content_scripts/mode.coffee b/content_scripts/mode.coffee index 205b8288..d5775ad7 100644 --- a/content_scripts/mode.coffee +++ b/content_scripts/mode.coffee @@ -109,15 +109,14 @@ class Mode          "scroll": (event) => @alwaysContinueBubbling => @exit event      # Some modes are singletons: there may be at most one instance active at any time.  A mode is a singleton -    # if @options.singleton is truthy.  The value of @options.singleton should be the key which is intended to -    # be unique.  New instances deactivate existing instances with the same key. +    # if @options.singleton is set.  The value of @options.singleton should be the key which is intended to be +    # unique.  New instances deactivate existing instances with the same key.      if @options.singleton -      do => -        singletons = Mode.singletons ||= {} -        key = Utils.getIdentity @options.singleton -        @onExit -> delete singletons[key] -        @deactivateSingleton @options.singleton -        singletons[key] = this +      singletons = Mode.singletons ||= {} +      key = @options.singleton +      @onExit -> delete singletons[key] +      singletons[key]?.exit() +      singletons[key] = this      # If @options.passInitialKeyupEvents is set, then we pass initial non-printable keyup events to the page      # or to other extensions (because the corresponding keydown events were passed).  This is used when @@ -182,9 +181,6 @@ class Mode        @modeIsActive = false        @setIndicator() -  deactivateSingleton: (singleton) -> -    Mode.singletons?[Utils.getIdentity singleton]?.exit() -    # Shorthand for an otherwise long name.  This wraps a handler with an arbitrary return value, and always    # yields @continueBubbling instead.  This simplifies handlers if they always continue bubbling (a common    # case), because they do not need to be concerned with the value they yield. diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index e863b553..ad7cc136 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -33,8 +33,8 @@ class PostFindMode extends SuppressPrintable      super        name: "post-find" -      # PostFindMode shares a singleton with the modes launched by focusInput; each displaces the other. -      singleton: element +      # PostFindMode shares a singleton with focusInput; each displaces the other. +      singleton: "post-find-mode/focus-input"        exitOnBlur: element        exitOnClick: true        keydown: (event) -> InsertMode.suppressEvent event # Always truthy, so always continues bubbling. diff --git a/content_scripts/mode_visual.coffee b/content_scripts/mode_visual.coffee index 20560298..a6102987 100644 --- a/content_scripts/mode_visual.coffee +++ b/content_scripts/mode_visual.coffee @@ -212,9 +212,6 @@ class VisualMode extends KeyHandlerMode      "c": -> @movement.collapseSelectionToAnchor(); new CaretMode      "o": -> @movement.reverseSelection() -    # TODO(smblott): "aw", etc. -    # TODO(smblott): simplify singletons. -    constructor: (options = {}) ->      @movement = new Movement options.alterMethod ? "extend"      @selection = @movement.selection @@ -236,7 +233,7 @@ class VisualMode extends KeyHandlerMode      super extend options,        name: options.name ? "visual"        indicator: options.indicator ? "Visual mode" -      singleton: VisualMode +      singleton: "visual-mode-group" # Visual mode, visual-line mode and caret mode each displace each other.        exitOnEscape: true        suppressAllKeyboardEvents: true        keyMapping: keyMapping diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index ade14029..1801a3fd 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -392,8 +392,6 @@ extend window,                  selectedInputIndex += hints.length + (if event.shiftKey then -1 else 1)                  selectedInputIndex %= hints.length                  hints[selectedInputIndex].classList.add 'internalVimiumSelectedInputHint' -                # Deactivate any active modes on this element (PostFindMode, or a suspended edit mode). -                @deactivateSingleton visibleInputs[selectedInputIndex].element                  DomUtils.simulateSelect visibleInputs[selectedInputIndex].element                  @suppressEvent                else unless event.keyCode == KeyboardUtils.keyCodes.shiftKey @@ -405,8 +403,6 @@ extend window,              id: "vimiumInputMarkerContainer"              className: "vimiumReset" -          # Deactivate any active modes on this element (PostFindMode, or a suspended edit mode). -          @deactivateSingleton visibleInputs[selectedInputIndex].element            DomUtils.simulateSelect visibleInputs[selectedInputIndex].element            if visibleInputs.length == 1              @exit() @@ -419,7 +415,7 @@ extend window,            DomUtils.removeElement @hintContainingDiv            if mode and document.activeElement and DomUtils.isEditable document.activeElement              new mode -              singleton: document.activeElement +              singleton: "post-find-mode/focus-input"                targetElement: document.activeElement                indicator: false diff --git a/lib/utils.coffee b/lib/utils.coffee index 31f4bec6..60d29998 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -197,17 +197,6 @@ Utils =    # locale-sensitive uppercase detection    hasUpperCase: (s) -> s.toLowerCase() != s -  # Give objects (including elements) distinct identities. -  getIdentity: do -> -    identities = [] - -    (obj) -> -      index = identities.indexOf obj -      if index < 0 -        index = identities.length -        identities.push obj -      "identity-" + index -    # Return a copy of object, but with some of its properties omitted.    copyObjectOmittingProperties: (obj, properties...) ->      obj = extend {}, obj diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index dad4def9..979500f5 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -744,7 +744,7 @@ context "Mode utilities",      count = 0      class Test extends Mode -      constructor: -> count += 1; super singleton: Test +      constructor: -> count += 1; super singleton: "test"        exit: -> count -= 1; super()      assert.isTrue count == 0 | 
