UNPKG

547 BJavaScriptView Raw
1'use strict';
2const clone = require('lodash/clone');
3
4class ContextRef {
5 constructor() {
6 this.value = {};
7 }
8
9 get() {
10 return this.value;
11 }
12
13 set(newValue) {
14 this.value = newValue;
15 }
16
17 copy() {
18 return new LateBinding(this);
19 }
20}
21module.exports = ContextRef;
22
23class LateBinding extends ContextRef {
24 constructor(ref) {
25 super();
26 this.ref = ref;
27 this.bound = false;
28 }
29
30 get() {
31 if (!this.bound) {
32 this.set(clone(this.ref.get()));
33 }
34
35 return super.get();
36 }
37
38 set(newValue) {
39 this.bound = true;
40 super.set(newValue);
41 }
42}