@shopware-ag/meteor-admin-sdk
Version:
The Meteor SDK for the Shopware Administration.
76 lines • 3.25 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());
});
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "vue", "../index"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDataset = void 0;
const vue_1 = require("vue");
const index_1 = require("../index");
function useDataset(id, options) {
const data = (0, vue_1.ref)(null);
const isReady = (0, vue_1.ref)(false);
let isUpdatingFromRemote = false;
// eslint-disable-next-line @typescript-eslint/no-empty-function
let unsubscribe = () => { };
const ready = (0, index_1.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 (0, vue_1.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 = (0, vue_1.watch)(data, (newValue) => {
if (isUpdatingFromRemote || newValue === null) {
return;
}
void (0, index_1.update)({
id,
data: newValue,
});
}, { deep: true });
unsubscribe = (0, index_1.subscribe)(id, (payload) => __awaiter(this, void 0, void 0, function* () {
isUpdatingFromRemote = true;
data.value = payload.data;
yield (0, vue_1.nextTick)();
isUpdatingFromRemote = false;
}), { selectors: options === null || options === void 0 ? void 0 : options.selectors });
(0, vue_1.onBeforeUnmount)(() => {
stopWatch();
unsubscribe();
});
return {
data,
isReady,
ready,
};
}
exports.useDataset = useDataset;
});
//# sourceMappingURL=useDataset.js.map