fluidstate-preact
Version:
Library for using fine-grained reactivity state management library fluidstate in Preact
60 lines (55 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPreactEffectClass = exports.getDisposePropertyName = exports.getCallbackName = void 0;
var _signalsCore = require("@preact/signals-core");
/**
* This file contains a variety of hacky deductions to derive
* 1. `Effect` class provided by preact
* 2. `_callback` property defined on the class (which is not `_callback` in
* reality due to preact's minification)
* 3. `_dispose` property defined on the class (which is also not `_dispose`)
*/
// Effect returns a bound `_dispose` function
let disposePropertyName = _signalsCore.effect.toString().match(/return .+?\.(.+?)\.bind/)?.[1] || "";
let PreactEffect;
let callbackPropertyName = "";
// When the provided effect function is ran, `this` is an instance of
// preact's `Effect` class
let effectFn = function () {
PreactEffect = this.constructor;
let effectPropertyName = "";
// One of the properties on `this` will be the actual effect
// functions. We will need to determine the "_callback" function name
for (let property in this) {
if (this[property] === effectFn) {
effectPropertyName = property;
}
}
for (let property of Object.getOwnPropertyNames(PreactEffect.prototype)) {
const value = PreactEffect.prototype[property];
// The callback function should be the only one that calls `this.[effectFn key]()`
// aside from the constructor
if (property !== "constructor" && typeof value === "function" && value.toString().indexOf(`this.${effectPropertyName}()`)) {
callbackPropertyName = property;
break;
}
}
};
// Creating a mock effect to derive everything we need
const preactEffect = (0, _signalsCore.effect)(effectFn);
preactEffect();
const getDisposePropertyName = () => {
// The returned type is fiction for the purpose of sanity
return disposePropertyName;
};
exports.getDisposePropertyName = getDisposePropertyName;
const getCallbackName = () => {
// The returned type is fiction for the purpose of sanity
return callbackPropertyName;
};
exports.getCallbackName = getCallbackName;
const getPreactEffectClass = () => PreactEffect;
exports.getPreactEffectClass = getPreactEffectClass;
//# sourceMappingURL=reactive-layer-hacky-utils.js.map