mutableProxy.js 5.3KB

5 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // vValueHelper Tests
  2. var proxiedDescriptor = (()=> {
  3. var __values = [];
  4. class proxiedDescriptorClass {
  5. get [Symbol.toStringTag]() { return 'proxiedDescriptor';}
  6. valueOf(){return 'pdc'}
  7. }
  8. var __composite = { a : '__composite'};
  9. console.dir(__composite)
  10. function composite(){
  11. var o = js.isUndefined(__values[0]) ? __values[0] : js.assign(js.getTypeInfo(__values[0]).create(), ...__values)
  12. return o;
  13. }
  14. let mutableTarget;
  15. let mutableHandler;
  16. function setHandler(handler) {
  17. Object.keys(handler).forEach(key => {
  18. const value = handler[key];
  19. if (typeof value !== 'function') {
  20. // handler[key] = Reflect[key];
  21. throw new Error(`Trap "${key}: ${value}" is not a function`);
  22. }
  23. if (!Reflect[key]) {
  24. throw new Error(`Trap "${key}: ${value}" is not a valid trap`);
  25. }
  26. });
  27. // mutableHandler = js.assign({}, Reflect, mutableHandler || {}, handler);
  28. mutableHandler = js.assign(mutableHandler || {}, handler);
  29. // mutableHandler = handler
  30. }
  31. function setTarget(target) {
  32. // if (!(target instanceof Object)) {
  33. // throw new Error(`Target "${target}" is not an object`);
  34. // }
  35. mutableTarget = target;
  36. }
  37. setTarget(() => {});
  38. // setHandler(Reflect);
  39. console.log('-----MT---------')
  40. console.dir(mutableTarget)
  41. console.log('-----MT---------')
  42. // Dynamically forward all the traps to the associated methods on the mutable handler
  43. const handler = new Proxy({}, {
  44. get(target, property) {
  45. console.log('-----marker---------')
  46. setTarget(composite())
  47. // console.dir(target)
  48. // console.dir(Reflect.ownKeys)
  49. console.log(property)
  50. if(mutableHandler[property]) return (...args) => mutableHandler[property].apply(null, [mutableTarget, ...args.slice(1)]);
  51. // else return (...args) => Reflect[property].apply(null, [mutableTarget, ...args.slice(1)]);
  52. }
  53. });
  54. var mutableProxyInstance = {
  55. setHandler,
  56. getTarget() {
  57. return mutableTarget;
  58. },
  59. getHandler() {
  60. return mutableHandler;
  61. },
  62. proxy: new Proxy(mutableTarget, handler)
  63. };
  64. setHandler({
  65. getOwnPropertyDescriptor: function(target, prop) {
  66. console.log('accessing prop : ')
  67. console.dir(prop)
  68. // console.dir(this)
  69. o = composite()
  70. console.dir(target)
  71. var a = {value : undefined, configurable : true};
  72. if(!js.isUndefined(o)) a= Object.getOwnPropertyDescriptor(o, prop);
  73. // if (a) a.value = this.get(o, prop);
  74. return a;
  75. // return function(){
  76. // console.log('in toString ....')
  77. // var o = composite();
  78. // console.dir(o)
  79. // return o
  80. // }
  81. }
  82. ,
  83. // ownKeys : function(){
  84. // return Reflect.ownKeys(composite())
  85. // }
  86. // ,
  87. get: function(target, name) {
  88. console.log('accessing name : ')
  89. console.dir(name)
  90. console.log('target is now')
  91. console.dir(target)
  92. // console.log('' + (name == Symbol.toStringTag))
  93. // if(name == 'ownKeys') {
  94. // return composite().ownKeys;
  95. // }
  96. if(js.isUndefined(target)) return Reflect[name];
  97. if(
  98. name == util.inspect.custom
  99. // || name == 'inspect'
  100. // || name == Symbol.toStringTag
  101. // || name == Symbol.iterator /*|| name == 'constructor' || name == 'toString' || name == 'valueOf'*/
  102. // || name == 'valueOf'
  103. // && name != 'name'
  104. ) {
  105. return function(){
  106. console.log('in toString ....')
  107. var o = composite();
  108. console.dir(o)
  109. return o
  110. }
  111. }
  112. // else if(
  113. // name == Symbol.toStringTag
  114. // // ||
  115. // // name == 'toString'
  116. // || name == 'valueOf'
  117. // || name == Symbol.iterator /*|| name == 'constructor' || name == 'toString' || name == 'valueOf'*/
  118. // )
  119. // return JSON.stringify((function(){
  120. // console.log('in toString ....')
  121. // var o = composite();
  122. // // console.dir(o)
  123. // return o
  124. // })()
  125. // )
  126. // else
  127. return (name == 'composite' ? composite()
  128. // : name == 'toString' || name == 'valueOf' ? (function(){
  129. // console.log('in toString')
  130. // // console.dir((js.isUndefined(__values[0]) ? __values[0] : js.assign(js.getTypeInfo(__values[0]).create(), ...__values)))
  131. // return composite()
  132. // })
  133. // : name == 'toValue' ? js.assign(js.getTypeInfo(__values[0]).create(), ...__values)
  134. : name == 'current' ? Ref.isRef(__values[0]) ? __values[0].getValue() : __values[0]
  135. : name == 'previous' ? Ref.isRef(__values[1]) ? __values[1].getValue() : __values[1]
  136. : composite()[name] //Ref.isRef(__values[name]) ? __values[name].getValue() : __values[name]
  137. )
  138. }
  139. , set: function(target, name, value) {
  140. mutabletarget = __composite = composite();
  141. name == 'revise' ? (__values.splice(0, 0, value), value)
  142. : name == 'precede' ? (__values.push(value), value)
  143. : name == 'current' ? __values[0] = value
  144. : __values[name] = value;
  145. return __composite;
  146. }
  147. })
  148. var p = new Proxy(__composite, handler);
  149. p.toString = function() { return 'ps tostring' }
  150. return {
  151. get : function(){ return p}
  152. , set : function(value) { p.revise = value}
  153. }
  154. })()