piral-core
Version:
The core library for creating a Piral instance.
46 lines • 1.32 kB
JavaScript
import { updateKey } from '../utils';
export function resetData(ctx) {
ctx.dispatch((state) => ({
...state,
data: {},
}));
}
export function readDataItem(ctx, key) {
return ctx.readState((state) => state.data[key]);
}
export function readDataValue(ctx, key) {
const item = readDataItem(ctx, key);
return item && item.value;
}
export function writeDataItem(ctx, key, value, owner, target, expires) {
const isNull = !value && typeof value === 'object';
const data = isNull
? value
: {
value,
owner,
target,
expires,
};
ctx.dispatch((state) => ({
...state,
data: updateKey(state.data, key, data),
}));
ctx.emit('store-data', {
name: key,
target,
value,
owner,
expires,
});
}
export function tryWriteDataItem(ctx, key, value, owner, target, expires) {
const item = readDataItem(ctx, key);
if (item && item.owner !== owner) {
console.error(`Invalid data write to '${key}'. This item currently belongs to '${item.owner}' (write attempted from '${owner}'). The action has been ignored.`);
return false;
}
writeDataItem(ctx, key, value, owner, target, expires);
return true;
}
//# sourceMappingURL=data.js.map