Serialized Function(since 2.0)
Introduction
You often needs to provide a custom function if you are working with DCF. Some of them will be serialized and transfer to master/worker, and run on a different process. It will be ok if you don't use any upvalues(variables/constants from enclosing scopes), but if you needs to use closure, you should read this article first.
Functions will be serialized as source string, then deserialized with new Function
.
Here come's a problem: how does worker know the upvalue min
?
Manually capture env
One solution is to capture upvalues by calling captureEnv
API manually:
If you want some API from other libraries, you can use requireModule
to require them for serialized function:
Please be sure that module is also available on master/worker.
If you serialize a function inside a serialized function(you should rarely meet this if you do not use DCF HTTP/2 API), please be careful by using captureEnv, they should be required by requireModule:
Register auto capturing
if manually capture upvalue is too complex, you can require a helper module registerCaptureEnv
, which will capture all upvalues automaticly for every function, with very low cost, and does not effect things if you didn't serialized functions.
Hint: You should require
@dcfjs/common/registerCaptureEnv
before any your modules was loaded. You can either write a loader module, or require it from command line:
You should not destruct third-party modules before using them in serailized function:
But if you use ECMAScript 6 Modules
via typescript or babel, it's safe to import and use function:
Last updated