You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While using safe-units in a nuxt project, sometimes nuxt will serialise server-side state and add it to the document as a JSON payload to be deserialised in the client during hydration. Using objects that are class instances (such as Measure) results in an error on the server about non-POJOs (plain old javascript objects):
Cannot stringify arbitrary non-POJOs
Nuxt does provide a capability to add your own serialisation/deserialisation by way of definePayloadPlugin:
// plugins/measurePayload.tsexportdefaultdefinePayloadPlugin(()=>{definePayloadReducer('Measure',(value)=>{if(Measure.isMeasure(value)){// return a plain javascript object with the public properties of a Measure:// value, unit, unitSystem, symbolreturn{
...value,unitSystem: { ...value.unitSystem},};}});definePayloadReviver('Measure',(value)=>{const{ value, unit, unitSystem, symbol }=value;constunitInstance= ...;// does safe-units make this possible?returnMeasure.of(value,unitInstance);});});// this could also possibly be done individually for Unit and UnitSystem to make each part simpler
I'm wondering if it's possible using the current tools provided by the library to construct a Measure class instance from the data available in Measure's public properties (value, unit, unitSystem, symbol)? Since the value is just a number, it seems the hard part is re-constructing an instance of the unit / unit system.
If we were to assume the SIUnitSystem, is there a way to take the unit dimensions (ie { length: 2, mass: 1, ... }) and construct a unit instance from them? Is it safe to use Dimensionless as the unit during de-serialisation? Are the dimensions used in any calculation/conversion, or are they simply there for type checking?
If it's not possible now, but you can see a way to add the capability, is that a useful feature?
The text was updated successfully, but these errors were encountered:
While using
safe-units
in a nuxt project, sometimes nuxt will serialise server-side state and add it to the document as a JSON payload to be deserialised in the client during hydration. Using objects that are class instances (such asMeasure
) results in an error on the server about non-POJOs (plain old javascript objects):Nuxt does provide a capability to add your own serialisation/deserialisation by way of
definePayloadPlugin
:I'm wondering if it's possible using the current tools provided by the library to construct a Measure class instance from the data available in Measure's public properties (
value
,unit
,unitSystem
,symbol
)? Since thevalue
is just anumber
, it seems the hard part is re-constructing an instance of the unit / unit system.If we were to assume the SIUnitSystem, is there a way to take the unit dimensions (ie
{ length: 2, mass: 1, ... }
) and construct a unit instance from them? Is it safe to useDimensionless
as the unit during de-serialisation? Are the dimensions used in any calculation/conversion, or are they simply there for type checking?If it's not possible now, but you can see a way to add the capability, is that a useful feature?
The text was updated successfully, but these errors were encountered: