Guest User

Untitled

a guest
Sep 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. # Ready to Assemble Design Pattern #
  2. _IKEA Furniture Design Pattern (Assembly Service Included)_
  3.  
  4. ## Motivation:
  5. In pdf.js we need to do as much processing as possible in the worker thread. The result of this work is then often used in the main thread to rebuild an object. There is currently no consistent way to do this.
  6.  
  7. ## Ready to Assemble Furniture Process:
  8.  
  9. **Producer**
  10.  
  11. - Build - Take pieces and do as much work and assembly on the product as possible.
  12. - Package - Take the partially assembled pieces and put them in packaging for transport.
  13. - Send - Send the product to the consumer.
  14.  
  15. **Consumer**
  16.  
  17. - Receive - Receive the package from the producer.
  18. - Assemble - Create the final product from the partially assembled pieces ready for use.
  19.  
  20. ## Relating this to pdf.js using how PostScript functions work as an example:
  21.  
  22. **Producer (Worker Thread)**
  23.  
  24. - Build - Take raw PDF objects(strings, numbers, dicts, ...) and do as much processing as possible. For PS functions we parse the function.
  25. - Package - Create a JSON representation with enough information to build the final object. This is very similar to our current getIR.
  26. - Send - Automatically handled by messaging e.g. mainThread.postMessage(package)
  27.  
  28. **Consumer (Main Thread)**
  29.  
  30. - Receive - Automatically handled by messaging e.g. worker.onMessage(package)
  31. - Assemble - Create the final object that is usable by the main thread. This could be considered our fromIR.
  32.  
  33. ##How this differs from just serialize/deserialize:
  34. With searlize/deserialize the object you use in the main thread would be exactly the same. In this model we aren't trying to re-create the object exactly since most of the time we don't need everything that was in the worker thread.
  35.  
  36. ## How this could make things easier:
  37.  
  38. - creates an easy to understand analogy (I think)
  39. - breaks chunks of work up into defined tasks
  40. - assembly could be made semi-automatic
  41.  
  42. ## In Code
  43. This really isn't much code, it's more a method to organize things.
  44.  
  45. ```javascript
  46.  
  47. /**** Approach 1) Object for each, separate consumer/producer ****/
  48. function PostScriptProducer(data) {
  49. this.data = data;
  50. }
  51. PostScriptProducer.prototype = {
  52. build: function() {
  53. parseTheData();
  54. return aPackage /* a JSON object or maybe Package Object*/;
  55. }
  56. }
  57.  
  58. function PostScriptConsumer(package) {
  59. this.package = package;
  60. }
  61. PostScriptConsumer.prototype = {
  62. assemble: function() {
  63. var postScriptEvaluator = createPostScriptEvalutor();
  64. return postScriptEvaluator;
  65. }
  66. }
  67.  
  68. function AssemblyService() {
  69. this.assemblers = {};
  70. }
  71. AssemblyService.prototype = {
  72. addAssembler: function(name, assembler) {
  73. this.assemblers[name] = assembler;
  74. },
  75. assemble: function(package) {
  76. var assembler = new this.assemblers[package.name]();
  77. return assembler.assemble(package);
  78. }
  79. }
  80.  
  81. /*** Approach 2) Module(ish), separate consumer/producer ****/
  82. var PostScriptConsumer = (function() {
  83. return {
  84. build: function(args) {
  85. parseTheData();
  86. return aPackage /* a JSON object or maybe Package Object*/;
  87. }
  88. }
  89. });
  90. var PostScriptProducer = (function() {
  91. return {
  92. assemble: function(package) {
  93. var postScriptEvaluator = createPostScriptEvalutor();
  94. return postScriptEvaluator;
  95. }
  96. }
  97. });
  98. ```
Add Comment
Please, Sign In to add comment