Class: Mbeditor::JsGlobalsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/js_globals_service.rb

Overview

Enumerates every top-level JavaScript declaration in the workspace so the editor can declare them as ambient globals in one shot. This models the Sprockets concatenation world: files share one global script scope, so an unindented var/function/class (or a window.X = assignment) in any JS-family file is visible everywhere with no import statement.

Backed by CodeSearchService (rg > git grep > grep over JS_GLOBS, which includes *.js.jsx / *.js.erb), with a short cache invalidated whenever mbeditor mutates a file.

Constant Summary collapse

MAX_SYMBOLS =
3000
CACHE_TTL =

seconds

30
PATTERN =

Column-0 anchored so only true top-level declarations match (Sprockets globals are by definition unindented); window assignments may be indented (IIFE bodies). BSD-grep-safe ERE: no \s, no non-greedy ops.

'^((var|let|const|function|class|async[ \t]+function)[ \t]+[A-Za-z_$][A-Za-z0-9_$]*' \
'|[ \t]*window\.[A-Za-z_$][A-Za-z0-9_$]*[ \t]*=)'
IDENTIFIER =
/[A-Za-z_$][A-Za-z0-9_$]*/

Class Method Summary collapse

Class Method Details

.call(workspace_root) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/mbeditor/js_globals_service.rb', line 29

def call(workspace_root)
  root = workspace_root.to_s
  now = monotonic
  MUTEX.synchronize do
    entry = (@cache ||= {})[root]
    return entry[:data] if entry && (now - entry[:ts]) < CACHE_TTL
  end

  data = compute(root)
  MUTEX.synchronize { (@cache ||= {})[root] = { ts: monotonic, data: data } }
  data
end

.invalidate(workspace_root) ⇒ Object



42
43
44
# File 'app/services/mbeditor/js_globals_service.rb', line 42

def invalidate(workspace_root)
  MUTEX.synchronize { (@cache ||= {}).delete(workspace_root.to_s) }
end