Skip to content

Commit

Permalink
Adds Signal.createPropertyDescriptor()
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Sep 24, 2024
1 parent 902f338 commit 4b979eb
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions modules/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,56 @@ export default class Signal {
return new ObserveSignal(signal, fn, initial);
}


/**
Signal.createPropertyDescriptor(descriptor)
Creates a signal-backed get/set property descriptor object from a standard
`descriptor` object.
If `descriptor` has a getter function any signals evaluated by that function
invalidate the property when they become invalid. If `descriptor` has `.value`
the property is invalidated when a new value is assigned to `object.name`.
**/

static createPropertyDescriptor(descriptor) {
const symbol = Symbol();

return assign({}, descriptor, descriptor.get ? {
get: function() {
const signal = this[symbol] || (this[symbol] = Signal.from(descriptor.get, this));
return signal.value;
}
} : {
get: function() {
const signal = this[symbol] || (this[symbol] = Signal.of(descriptor.value));
return signal.value;
},

set: descriptor.writable && function(value) {
const signal = this[symbol];
if (signal) signal.value = value;
else this[symbol] = Signal.of(value);
},

value: undefined,
writable: undefined
}) ;
}

/**
Signal.define(object, name, descriptor)
Apes `Object.defineProperty()` by defining a signal-backed get/set property
`object.name` from a `descriptor` object.
If `descriptor` has a getter function any signals evaluated by that function
invalidate the property when they become invalid. If `descriptor` has `.value`
the property is invalidated when a new value is assigned to `object.name`.
**/

static define(object, name, descriptor) {
return Object.defineProperty(object, name, Signal.createPropertyDescriptor(descriptor)) ;
}

/**
Signal.evaluate(signal, fn[, context])
Expand Down

0 comments on commit 4b979eb

Please sign in to comment.