@shopware-ag/meteor-admin-sdk
Version:
The Meteor SDK for the Shopware Administration.
62 lines • 2.47 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { get, subscribe, update } from '../index';
export function useDataset(id, options) {
const data = ref(null);
const isReady = ref(false);
let isUpdatingFromRemote = false;
// eslint-disable-next-line @typescript-eslint/no-empty-function
let unsubscribe = () => { };
const ready = get({
id,
selectors: options === null || options === void 0 ? void 0 : options.selectors,
})
.then((initialData) => __awaiter(this, void 0, void 0, function* () {
if (initialData !== null && initialData !== undefined) {
isUpdatingFromRemote = true;
data.value = initialData;
yield nextTick();
isUpdatingFromRemote = false;
}
}))
// eslint-disable-next-line @typescript-eslint/no-empty-function
.catch(() => {
// A catch is needed because get() rejects if no data is available yet
})
.finally(() => {
isReady.value = true;
});
const stopWatch = watch(data, (newValue) => {
if (isUpdatingFromRemote || newValue === null) {
return;
}
void update({
id,
data: newValue,
});
}, { deep: true });
unsubscribe = subscribe(id, (payload) => __awaiter(this, void 0, void 0, function* () {
isUpdatingFromRemote = true;
data.value = payload.data;
yield nextTick();
isUpdatingFromRemote = false;
}), { selectors: options === null || options === void 0 ? void 0 : options.selectors });
onBeforeUnmount(() => {
stopWatch();
unsubscribe();
});
return {
data,
isReady,
ready,
};
}
//# sourceMappingURL=useDataset.js.map