UNPKG

killa

Version:

State management for Vanilla and React

102 lines (101 loc) 4.29 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var core_exports = {}; __export(core_exports, { createStore: () => createStore }); module.exports = __toCommonJS(core_exports); var import_just_clone = __toESM(require("just-clone")); var import_deep_equals = require("killa/deep-equals"); var import_constants = require("killa/constants"); function createStore(initializer = Object.assign({}), options) { let state; const subscribers = /* @__PURE__ */ new Set(); const compare = typeof (options == null ? void 0 : options.compare) === "function" ? options.compare : import_deep_equals.deepEquals; const getState = () => (0, import_just_clone.default)(state); const setState = (fn, force = false) => { const newState = fn(getState()) || {}; if (!compare(state, newState)) { const prevState = state; state = force ? Object.assign(newState) : Object.assign(getState(), (0, import_just_clone.default)(newState)); subscribers.forEach((subscriber) => { const _prevState = (0, import_just_clone.default)(prevState); const _newState = getState(); if (subscriber.$$subscriber && subscriber.$$selector) { const selectorState = subscriber.$$selectorState; const nextselectorState = subscriber.$$selector(state); if (!compare(selectorState, nextselectorState)) { subscriber.$$selectorState = nextselectorState; subscriber(_newState, _prevState); } return; } subscriber(_newState, _prevState); }); } }; const initialState = typeof initializer === "function" ? initializer(getState, setState) : initializer; const subscribe = (subscriber, selector) => { if (typeof selector === "function") { subscriber.$$subscriber = import_constants.SYMBOL_SUBSCRIBER; subscriber.$$selectorState = selector(state); subscriber.$$selector = selector; } subscribers.add(subscriber); return () => subscribers.delete(subscriber); }; if (!(0, import_deep_equals.isObject)(initialState)) throw new Error("Store must be an object."); state = (0, import_just_clone.default)(initialState); const resetState = (state2 = null) => { const newState = state2 && (0, import_deep_equals.isObject)(state2) ? state2 : (0, import_just_clone.default)(initialState); store.setState(() => newState, true); }; const destroy = () => subscribers.clear(); const store = { $$store: import_constants.SYMBOL_STORE, getState, setState, subscribe, getServerState: () => initialState, resetState, destroy }; if ((options == null ? void 0 : options.use) && Array.isArray(options.use)) { options.use.forEach((middleware) => middleware(store)); } if (process.env.NODE_ENV !== "test") { return Object.freeze(store); } return store; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createStore });