context-ref.js 590 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const clone = require('lodash.clone');
  3. class ContextRef {
  4. constructor() {
  5. this.value = {};
  6. }
  7. get() {
  8. return this.value;
  9. }
  10. set(newValue) {
  11. this.value = newValue;
  12. }
  13. copy() {
  14. return new LateBinding(this); // eslint-disable-line no-use-before-define
  15. }
  16. }
  17. module.exports = ContextRef;
  18. class LateBinding extends ContextRef {
  19. constructor(ref) {
  20. super();
  21. this.ref = ref;
  22. this.bound = false;
  23. }
  24. get() {
  25. if (!this.bound) {
  26. this.set(clone(this.ref.get()));
  27. }
  28. return super.get();
  29. }
  30. set(newValue) {
  31. this.bound = true;
  32. super.set(newValue);
  33. }
  34. }