Home Reference Source

packages/remote-context/src/actions/ReflectConstructAction.js

  1. /**
  2. * Copyright 2017 Moshe Simantov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. import { trimArgumentsList } from 'remote-instance';
  18. import ReflectAction from './ReflectAction';
  19.  
  20. export default class ReflectConstructAction extends ReflectAction {
  21. static fromProxy(session, target, argumentsList) {
  22. return new this(target, argumentsList.map(arg => session.dispatch(arg)));
  23. }
  24.  
  25. constructor(target, argumentsList = []) {
  26. if (!Array.isArray(argumentsList)) {
  27. throw new TypeError(
  28. `Expect argumentsList to be an Array: ${argumentsList}`
  29. );
  30. }
  31. super(target);
  32.  
  33. this.argumentsList = argumentsList;
  34. }
  35.  
  36. fetch(session) {
  37. const target = this.fetchTarget(session);
  38. const argumentsList = this.argumentsList.map(arg => session.fetch(arg));
  39.  
  40. return Reflect.construct(target, argumentsList);
  41. }
  42.  
  43. exec(session) {
  44. this.fetch(session);
  45. }
  46.  
  47. toArgumentsList() {
  48. const argumentsList = [this.target, this.argumentsList];
  49. const defaults = [undefined, []];
  50.  
  51. return trimArgumentsList(argumentsList, defaults);
  52. }
  53. }