@Martin

I have noticed this risk. So:

1) This will be just a limited feature used in my framework, it might go
open source, but I will never try to make this a part of node.js API. So
the risk will  affect very little area.

2) I will implement it in this way:
   a) Get the old exports object from the cache
   b) Remove the old module from the cache
   c) Use require() to load the new module to the cache
   d) Use new module's code_changed function to convert module state
   e) Delete all fields of the old exports object
   f) Copy all fields of the new exports to the old exports object
   g) Update the status field of old exports object to the status object
created at step d
   h) Update the new modules exports field to the reference to the old
exports object, to make sure the exports object of the module is unique in
the system

Thank you for your reply and expecting more comments & advices.


On Sat, Aug 17, 2013 at 11:55 PM, Martin Cooper <mfncoo...@gmail.com> wrote:

> There are a couple of issues with that.
>
> First, if 'mod' loaded other modules to do its job, those other modules
> are not removed from the cache. For example, suppose package 'foo' has main
> module a.js, and that loads module b.js to do some of its work. Now if you
> delete 'foo', or even a.js, from the cache, b.js is still cached, so when a
> new version of a.js is loaded, it will still use that old b.js. Hence for
> this to work reliably, you need to know all of the implementation files
> that will need to be removed from the cache, not just your entry point.
> That's pretty fragile.
>
> Also, depending on the nature of the modules being removed, and their
> usage patterns, you may end up with retained references to the now-removed
> (but possibly still extant) module, or objects created by it, that may
> result in weird behaviour later. In the OP's case, with a framework
> designed for this, that may not be so much of an issue, but it's still a
> risk, especially if the modules being removed from the cache are
> non-trivial and / or not written by the framework designer.
>
> --
> Martin Cooper
>
>
> On Sat, Aug 17, 2013 at 2:42 AM, George Stagas <gsta...@gmail.com> wrote:
>
>> function removeCached(mod) {
>>   delete require.cache[require.resolve(mod)];
>> }
>>
>> // then
>>
>> require('foo');
>>
>> // ... later
>>
>> removeCached('foo');
>>
>>
>>
>> 2013/8/17 Tony Huang <cnwz...@gmail.com>
>>
>>> Hi all,
>>>
>>> I'm creating a framework for my game server, and developing the module
>>> hot update feature.
>>>
>>> My design is very similar to the Erlang hot update feature. Modules will
>>> be hot updated should meet the interface:
>>> module.exports = {
>>>   'status': "data",
>>>   'version': "1.0",
>>>   'init': function() {
>>>      return initialStatus;
>>>   },
>>>   'code_changed': function(oldVersion, newVersion, oldStatus) {
>>>      return newStatus;
>>>   }
>>> };
>>>
>>> So, when the module source file changes, the framework will get the
>>> status of original version of module, and remove it from the module cache,
>>> and import the new module, and invoke the code_changed method of the new
>>> version of module to generate the new status and assign it to the new
>>> module to finish the update process.
>>>
>>> As a result, I need an API to remove the module from the module cache,
>>> will it be possible or  will this API be added in the future version of
>>> node?
>>>
>>> Best regards.
>>>
>>> --
>>> ------------------------------------------------------
>>> Tony Huang    cnwz...@gmail.com
>>>                      wz...@hotmail.com
>>>                      wz...@vip.sina.com
>>>
>>> --
>>> --
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines:
>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to nodejs@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> nodejs+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to nodejs+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>  --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to nodejs+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>



-- 
------------------------------------------------------
Tony Huang    cnwz...@gmail.com
                     wz...@hotmail.com
                     wz...@vip.sina.com

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to