UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

85 lines (84 loc) 2.32 kB
/*! * NENT 2022 */ import { Component, Element, Prop } from '@stencil/core'; import { actionBus, eventBus, } from '../../services/actions'; import { addDataProvider, removeDataProvider, } from '../../services/data/factory'; import { DATA_COMMANDS } from '../n-data/services/interfaces'; import { StorageService } from './services/storage'; /** * This element enables the **Storage Data Provider**, that * leverages the browsers 'long-term' data storage. * * @system data * @extension actions * @extension provider */ export class DataStorage { constructor() { /** * Provider name to use in nent expressions. */ this.name = 'storage'; } registerProvider() { addDataProvider(this.name, this.provider); this.actionSubscription = actionBus.on(this.name, async (action) => { if (action.command == DATA_COMMANDS.SetData) { const { data } = action; await Promise.all(Object.keys(action.data).map(key => this.provider.set(key, data[key]))); } }); } componentWillLoad() { this.provider = new StorageService(window, eventBus, this.name, this.keyPrefix); this.registerProvider(); } disconnectedCallback() { var _a; removeDataProvider(this.name); (_a = this.actionSubscription) === null || _a === void 0 ? void 0 : _a.call(this); } render() { return null; } static get is() { return "n-data-storage"; } static get properties() { return { "keyPrefix": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The key prefix to use in storage" }, "attribute": "key-prefix", "reflect": false }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Provider name to use in nent expressions." }, "attribute": "name", "reflect": false, "defaultValue": "'storage'" } }; } static get elementRef() { return "el"; } }