UNPKG

react-async-states

Version:

A low-level multi paradigm state management library

118 lines (115 loc) 4.79 kB
import { isFunction, __DEV__, emptyArray } from '../../shared/index.js'; let isCurrentlyRunningOnRender = false; function startRenderPhaseRun() { let prev = isCurrentlyRunningOnRender; isCurrentlyRunningOnRender = true; return prev; } function endRenderPhaseRun(nextValue) { isCurrentlyRunningOnRender = nextValue; } function isRenderPhaseRun() { return isCurrentlyRunningOnRender; } function shouldRunSubscription(subscription, subscriptionConfig) { let { instance, alternate } = subscription; let config = alternate?.config || subscriptionConfig; let { lazy, condition, autoRunArgs } = config; return shouldSubscriptionRun(instance.state, instance.actions.getPayload(), lazy, condition, (autoRunArgs || emptyArray)); } function shouldSubscriptionRun(state, payload, lazy, condition, args) { if (lazy === false) { if (condition === undefined || condition === true) { return true; } else if (isFunction(condition)) { return condition(state, args, payload); } } return false; } function reconcileInstance(instance, currentConfig) { let instanceActions = instance.actions; // 📝 We can call this part the instance reconciliation // patch the given config and the new producer if provided and different // we might be able to iterate over properties and re-assign only the ones // that changed and are supported. let configToPatch = removeHookConfigToPatchToSource(currentConfig); instanceActions.patchConfig(configToPatch); if (currentConfig.payload) { instanceActions.mergePayload(currentConfig.payload); } let currentProducer = instance.fn; let pendingProducer = currentConfig.producer; if (pendingProducer !== undefined && pendingProducer !== currentProducer) { instanceActions.replaceProducer(pendingProducer); } } function removeHookConfigToPatchToSource(currentConfig) { // the patched config may contain the following properties // - source // - payload // - events // and other properties that can be retrieved from hooks usage and others // so we are tearing them apart before merging let { lazy, events, source, payload, concurrent, autoRunArgs, subscriptionKey, ...output } = currentConfig; return output; } function forceComponentUpdate(prev) { return prev + 1; } // dev mode helpers let currentlyRenderingComponentName = null; function __DEV__setHookCallerName(name) { if (name && !currentlyRenderingComponentName) { currentlyRenderingComponentName = name; } } function __DEV__getCurrentlyRenderingComponentName() { return currentlyRenderingComponentName; } function __DEV__unsetHookCallerName() { currentlyRenderingComponentName = null; } let __DEV__betterSourceConfigProperties; if (__DEV__) { __DEV__betterSourceConfigProperties = { runEffect: true, retryConfig: true, cacheConfig: true, initialValue: true, hideFromDevtools: true, skipPendingStatus: true, skipPendingDelayMs: true, resetStateOnDispose: true, runEffectDurationMs: true, }; } function __DEV__warnInDevAboutIncompatibleConfig(subscription) { let { config } = subscription; let { source, key, producer } = config; if (source) { if (key) { console.error(`[Warning][async-states] Subscription in component ${subscription.at} ` + `has a 'source' and 'key' as the same time, 'key' has no effect.`); return; } if (producer) { console.error(`[Warning][async-states] Subscription in component ${subscription.at} ` + `has a 'source' (${source.key}) and 'producer' as the same time,` + ` the source's producer will be replaced.`); return; } let configuredProps = Object.keys(config).filter((t) => config[t] !== undefined && __DEV__betterSourceConfigProperties[t]); if (configuredProps.length) { console.error(`[Warning][async-states] Subscription in component ${subscription.at} ` + `has a 'source' and the following properties '` + configuredProps.join(", ") + "' at the same time. All these props will be flushed into the " + "source config, better move them to the source creation."); return; } } } export { __DEV__getCurrentlyRenderingComponentName, __DEV__setHookCallerName, __DEV__unsetHookCallerName, __DEV__warnInDevAboutIncompatibleConfig, endRenderPhaseRun, forceComponentUpdate, isRenderPhaseRun, reconcileInstance, shouldRunSubscription, shouldSubscriptionRun, startRenderPhaseRun }; //# sourceMappingURL=HookSubscriptionUtils.js.map