@magnetarjs/plugin-vue3
Version:
Magnetar plugin vue3
59 lines (58 loc) • 2.49 kB
JavaScript
import { isFullString, isString } from 'is-what';
import { deleteActionFactory } from './delete.js';
import { insertActionFactory } from './insert.js';
export function streamActionFactory(data, vue3StoreOptions) {
return function ({ payload, collectionPath, docId, actionConfig, pluginModuleConfig, mustExecuteOnRead, }) {
// hover over the prop names below to see more info on when they are triggered:
const doOnStream = {
added: (payload, meta) => {
// abort updating local cache state if the payload was set to undefined
if (payload === undefined)
return undefined;
const _docId = docId || `${meta.id}`;
insertActionFactory(data, vue3StoreOptions)({
payload,
collectionPath,
docId: _docId,
actionConfig,
pluginModuleConfig,
});
return undefined;
},
modified: (payload, meta) => {
// abort updating local cache state if the payload was set to undefined
if (payload === undefined)
return undefined;
const _docId = docId || `${meta.id}`;
insertActionFactory(data, vue3StoreOptions)({
payload,
collectionPath,
docId: _docId,
actionConfig,
pluginModuleConfig,
});
return undefined;
},
removed: (payload, meta) => {
// abort updating local cache state if the payload was set to undefined
if (payload === undefined)
return undefined;
const collectionPathDocIdToDelete = isFullString(docId)
? [collectionPath, docId]
: isString(payload)
? [collectionPath, payload]
: [collectionPath, meta.id];
const [_cPath, _dId] = collectionPathDocIdToDelete;
deleteActionFactory(data, vue3StoreOptions)({
payload: undefined,
collectionPath: _cPath,
docId: _dId,
actionConfig,
pluginModuleConfig,
});
return undefined;
},
};
return doOnStream;
};
}