summaryrefslogtreecommitdiff
path: root/script/vm/globals.lua
blob: 6a481bd119dca731f6cf4789b42564a7c2000dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local files = require 'files'
local await = require 'await'
local noder = require 'core.noder'

local globalsMap    = {}
local subscribeMap  = {}

local function popGlobals(uri)
    if not subscribeMap[uri] then
        return
    end
    for id in pairs(subscribeMap[uri]) do
        if globalsMap[id] then
            globalsMap[id][uri] = nil
        end
    end
    subscribeMap[uri] = nil
end

local function pushGlobals(uri)
    subscribeMap[uri] = {}
    local state = files.getState(uri)
    if not state then
        return
    end
    local nodes = noder.compileNodes(state.ast)
    for id in pairs(nodes) do
        if id:sub(1, 2) == 'g:' then
            if not globalsMap[id] then
                globalsMap[id] = {}
            end
            globalsMap[id][uri] = true
            subscribeMap[uri][id] = true
        end
    end
end

files.watch(function (ev, uri)
    if ev == 'update' then
        popGlobals(uri)
        pushGlobals(uri)
    end
end)

local m = {}

function m.getUrisByID(id)
    return globalsMap[id]
end

return m