{
  "version": 3,
  "sources": ["../src/constants.ts", "../src/create-id.ts", "../src/subscribe.ts", "../src/middleware.ts", "../src/utils/wrapper.ts", "../src/constructor.ts", "../src/on-action.ts", "../src/utils/id.ts"],
  "sourcesContent": ["export const exomeId: unique symbol = Symbol();\nexport const exomeName: unique symbol = Symbol();\nexport const FUNCTION = \"function\";\nexport const CONSTRUCTOR = \"constructor\";\n", "export const createID = (): string =>\n\t(\n\t\tDate.now().toString(36) + ((Math.random() * 1e5) ^ 1).toString(36)\n\t).toUpperCase();\n", "import { exomeId } from \"./constants.ts\";\nimport type { Exome } from \"./constructor.ts\";\n\nexport const subscriptions: Record<string, Set<() => any>> = {};\n\n/**\n * Subscribe to store instance update events.\n */\nexport const subscribe = <T extends Exome>(\n\tstore: T | null | undefined,\n\tfn: (store: T) => void,\n): (() => void) => {\n\tif (store == null) {\n\t\treturn () => {};\n\t}\n\n\tconst set = (subscriptions[store[exomeId]] ??= new Set());\n\tconst update = () => fn(store);\n\n\tset.add(update);\n\n\treturn () => {\n\t\tset.delete(update);\n\t};\n};\n\n/**\n * Sends update event to specific store instance.\n */\nexport const update = (store: Exome): void => {\n\tfor (const fn of subscriptions[store[exomeId]]?.values?.() || []) {\n\t\tfn();\n\t}\n};\n\n/**\n * Sends update event to all existing store instances.\n */\nexport const updateAll = (): void => {\n\tObject.values(subscriptions).map((set) => {\n\t\tfor (const fn of set.values()) {\n\t\t\tfn();\n\t\t}\n\t});\n};\n", "import { FUNCTION } from \"./constants.ts\";\nimport type { Exome } from \"./constructor.ts\";\nimport { update } from \"./subscribe.ts\";\n\nexport type Middleware = (\n\tinstance: Exome,\n\taction: string,\n\tpayload: any[],\n) => void | ((error?: Error, response?: any) => void);\n\nexport const middleware: Middleware[] = [];\n\n/**\n * Listens to middleware calls for any store instance.\n */\nexport const addMiddleware = (fn: Middleware): (() => void) => {\n\tmiddleware.push(fn);\n\n\treturn () => {\n\t\tmiddleware.splice(middleware.indexOf(fn), 1);\n\t};\n};\n\n/**\n * Triggers middleware for particular store instance to be called.\n * When return function gets called, it maks that the middleware action\n * was completed with or without errors.\n */\nexport const runMiddleware = (\n\tparent: Parameters<Middleware>[0],\n\tkey: Parameters<Middleware>[1],\n\targs: Parameters<Middleware>[2],\n): ((error?: Error, response?: any) => void) => {\n\tconst after = middleware.map((middleware) => middleware(parent, key, args));\n\n\treturn (error?: Error, response?: any) => {\n\t\tif (key !== \"NEW\") update(parent);\n\n\t\tlet x = 0;\n\t\tconst l = after.length;\n\t\twhile (x < l) {\n\t\t\ttypeof after[x] === FUNCTION &&\n\t\t\t\t(after[x] as (error?: Error, response?: any) => void)(error, response);\n\t\t\t++x;\n\t\t}\n\t};\n};\n", "import { CONSTRUCTOR, FUNCTION } from \"../constants.ts\";\nimport type { Exome } from \"../constructor.ts\";\nimport { runMiddleware } from \"../middleware.ts\";\n\nexport function getAllPropertyNames(obj: any) {\n\tconst props = [];\n\n\t// biome-ignore lint/style/noParameterAssign:\n\twhile ((obj = Object.getPrototypeOf(obj)) && obj !== Object.prototype) {\n\t\tprops.push(\n\t\t\t...Object.getOwnPropertyNames(obj).filter(\n\t\t\t\t(key) =>\n\t\t\t\t\tkey !== CONSTRUCTOR &&\n\t\t\t\t\tobj.hasOwnProperty(key) &&\n\t\t\t\t\ttypeof Object.getOwnPropertyDescriptor(obj, key)?.get !== FUNCTION,\n\t\t\t),\n\t\t);\n\t}\n\n\treturn props;\n}\n\nexport const wrapper = <T extends Exome>(parent: T): T => {\n\tconst properties = getAllPropertyNames(parent);\n\n\tfor (const key of properties) {\n\t\tconst value = (parent as any)[key];\n\n\t\tif (typeof value === FUNCTION) {\n\t\t\t(parent as any)[key] = (...args: any) => {\n\t\t\t\tconst middleware = runMiddleware(parent, key, args);\n\t\t\t\ttry {\n\t\t\t\t\tconst output = value.apply(parent, args);\n\n\t\t\t\t\tif (output instanceof Promise) {\n\t\t\t\t\t\treturn new Promise<any>((resolve, reject) => {\n\t\t\t\t\t\t\toutput\n\t\t\t\t\t\t\t\t.then(\n\t\t\t\t\t\t\t\t\t(result) => (middleware(undefined, result), resolve(result)),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t.catch((error) => (reject(error), middleware(error)));\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn middleware(undefined, output), output;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tmiddleware(error as Error);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}\n\n\treturn parent;\n};\n", "import { CONSTRUCTOR, exomeId, exomeName } from \"./constants.ts\";\nimport { createID } from \"./create-id.ts\";\nimport { runMiddleware } from \"./middleware.ts\";\nimport { subscriptions } from \"./subscribe.ts\";\nimport { wrapper } from \"./utils/wrapper.ts\";\n\n/**\n * Class that every store extends from.\n */\nexport class Exome {\n\tprivate [exomeId]: string;\n\tprivate [exomeName]!: string;\n\n\tconstructor() {\n\t\tconst name = this[exomeName] || this[CONSTRUCTOR].name;\n\t\tconst id = (this[exomeId] = name + \"-\" + createID());\n\t\tconst after = runMiddleware(this, \"NEW\", []);\n\n\t\tsubscriptions[id] = new Set();\n\n\t\t// Run this code after child constructor to get all the parameters right.\n\t\tPromise.resolve().then(after as any);\n\n\t\treturn wrapper(this);\n\t}\n}\n", "import type { Exome } from \"./constructor.ts\";\nimport { addMiddleware } from \"./middleware.ts\";\n\ntype Unsubscribe = () => void;\n\n/**\n * Listens to specific actions for all instances of particular store.\n */\nexport const onAction = <\n\tT extends Exome,\n\tA extends null | \"NEW\" | \"LOAD_STATE\" | keyof T,\n>(\n\tParent: new (...args: any[]) => T,\n\taction: A,\n\tcallback: <\n\t\tP extends A extends keyof T\n\t\t\t? T[A] extends (...args: infer P) => any\n\t\t\t\t? P\n\t\t\t\t: any[]\n\t\t\t: any[],\n\t>(\n\t\tinstance: T,\n\t\taction: Exclude<A, null>,\n\t\tpayload: P,\n\t\terror?: Error,\n\t\tresponse?: any,\n\t) => void,\n\ttype: \"before\" | \"after\" = \"after\",\n): Unsubscribe => {\n\treturn addMiddleware((instance, targetAction, payload) => {\n\t\tif (\n\t\t\t!(\n\t\t\t\tinstance instanceof Parent &&\n\t\t\t\t(targetAction === action || action === null)\n\t\t\t)\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (type === \"before\") {\n\t\t\tcallback(instance, targetAction as any, payload as any);\n\t\t\treturn;\n\t\t}\n\n\t\treturn (error, response) =>\n\t\t\tcallback(instance, targetAction as any, payload as any, error, response);\n\t});\n};\n", "import { exomeId } from \"../constants.ts\";\nimport type { Exome } from \"../constructor.ts\";\n\n/**\n * Gets unique id of specific store instance.\n */\nexport const getExomeId = (store: Exome): string => {\n\treturn store[exomeId];\n};\n\n/**\n * Sets custom id to specific store instance.\n */\nexport const setExomeId = (store: Exome, id: string): void => {\n\tconst [name] = getExomeId(store).split(\"-\");\n\tstore[exomeId] = `${name}-${id}`;\n};\n"],
  "mappings": ";AAAO,IAAMA,IAAyB,OAAO,GAChCC,IAA2B,OAAO,GAClCC,IAAW,YACXC,IAAc;;;ACHpB,IAAMC,IAAW,OAEtB,KAAK,IAAI,EAAE,SAAS,EAAE,KAAM,KAAK,OAAO,IAAI,MAAO,GAAG,SAAS,EAAE,GAChE,YAAY;;;ACAR,IAAMC,IAAgD,CAAC,GAKjDC,IAAY,CACxBC,GACAC,MACkB;AAXnB,MAAAC,GAAAC;AAYC,MAAIH,KAAS;AACZ,WAAO,MAAM;AAAA,IAAC;AAGf,MAAMI,KAAOD,IAAAL,EAAAI,IAAcF,EAAMK,CAAO,OAA3B,OAAAF,IAAAL,EAAAI,KAAkC,oBAAI,IAAI,GACjDI,IAAS,MAAML,EAAGD,CAAK;AAE7B,SAAAI,EAAI,IAAIE,CAAM,GAEP,MAAM;AACZ,IAAAF,EAAI,OAAOE,CAAM;AAAA,EAClB;AACD,GAKaA,IAAS,CAACN,MAAuB;AA7B9C,MAAAE,GAAAC;AA8BC,WAAWF,OAAME,KAAAD,IAAAJ,EAAcE,EAAM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P,EAASG,GAAUC,GAAqBC,GAAgBC,GAAOC,CAAQ;AAAA;AACzE,CAAC;;;ACxCK,IAAMC,IAAa,CAACC,MACnBA,EAAMC,CAAO,GAMRC,IAAa,CAACF,GAAcG,MAAqB;AAC7D,MAAM,CAACC,CAAI,IAAIL,EAAWC,CAAK,EAAE,MAAM,GAAG;AAC1C,EAAAA,EAAMC,CAAO,IAAI,GAAGG,CAAI,IAAID,CAAE;AAC/B;",
  "names": ["exomeId", "exomeName", "FUNCTION", "CONSTRUCTOR", "createID", "subscriptions", "subscribe", "store", "fn", "_a", "_b", "set", "exomeId", "update", "updateAll", "middleware", "addMiddleware", "fn", "runMiddleware", "parent", "key", "args", "after", "error", "response", "update", "x", "l", "FUNCTION", "getAllPropertyNames", "obj", "props", "key", "_a", "CONSTRUCTOR", "FUNCTION", "wrapper", "parent", "properties", "value", "args", "middleware", "runMiddleware", "output", "resolve", "reject", "result", "error", "exomeId", "exomeName", "Exome", "name", "CONSTRUCTOR", "id", "createID", "after", "runMiddleware", "subscriptions", "wrapper", "onAction", "Parent", "action", "callback", "type", "addMiddleware", "instance", "targetAction", "payload", "error", "response", "getExomeId", "store", "exomeId", "setExomeId", "id", "name"]
}
