UNPKG

houdini-svelte

Version:
101 lines (100 loc) 2.98 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var base_exports = {}; __export(base_exports, { BaseStore: () => BaseStore }); module.exports = __toCommonJS(base_exports); var import_client = require("$houdini/runtime/client"); var import_store = require("svelte/store"); var import_adapter = require("../adapter"); var import_client2 = require("../client"); class BaseStore { #params; get artifact() { return this.#params.artifact; } get name() { return this.artifact.name; } #store; #unsubscribe = null; constructor(params) { if (typeof params.initialize === "undefined") { params.initialize = true; } this.#store = new import_client.DocumentStore({ artifact: params.artifact, client: null, fetching: params.fetching, initialValue: params.initialValue }); this.#params = params; } #observer = null; get observer() { if (this.#observer) { return this.#observer; } this.#observer = (0, import_client2.getClient)().observe(this.#params); return this.#observer; } subscribe(...args) { const bubbleUp = this.#store.subscribe(...args); if (import_adapter.isBrowser && (this.#subscriberCount === 0 || !this.#unsubscribe)) { this.setup(); } this.#subscriberCount = (this.#subscriberCount ?? 0) + 1; return () => { this.#subscriberCount--; if (this.#subscriberCount <= 0) { this.#unsubscribe?.(); this.#unsubscribe = null; bubbleUp(); } }; } #subscriberCount = 0; setup(init = true) { let initPromise = Promise.resolve(); try { (0, import_client2.getClient)(); } catch { initPromise = (0, import_client2.initClient)(); } initPromise.then(() => { if (this.#unsubscribe) { return; } this.#unsubscribe = this.observer.subscribe((value) => { this.#store.set(value); }); if (init && this.#params.initialize) { return this.observer.send({ setup: true, variables: (0, import_store.get)(this.observer).variables }); } }); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BaseStore });