CodeMirror + Tern worker + addType

I have a webpage with a CodeMirror editor in it, and Tern running as a Worker.
I start two plugins with Tern, one that provides types for “!custom:” def file return values. I’m not sure if what I did is correct, but when the plugin is called to get such types, I create a def file on the fly, load it using “tern.def.load” look up the just defined type in the current context, create an empty AVal, add this new type as type and return it.
Than I issue a call to the web server to build out the def file containing the full type with all properties and methods and whatnot, load the def file using the “addDef” method of the tern server. The original empty definition is combined with this complete definition and the editor is correctly showing completions for my variables.
I am trying to do the same in another plugin that is supposed to handle “addType” messages, but it is not really working. I follow mostly the same strategy, but this time I am not loading def files, but adding source files.
Could someone advise, or does someone have an example of how to handle “addType” in a plugin in an asynchronous way?
That is return a temporary type, than reach out to a web server for more file(s), than load that/those files and have the editor recognize existing variables as having the newly loaded type.

I suppose the problem is that loading the file does not happen as I’d expect it to go. I mean the properties and methods of the objects defined in those files mix and when I create an instance of an object defined in those files, it will have all the methods and all the properties of the files loaded using the “addFile” method of the Tern server.