@absolutejs/sync-pack-counters 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +16 -1
- package/dist/index.js.map +2 -2
- package/dist/manifest.d.ts +13 -0
- package/dist/manifest.js +8610 -0
- package/dist/manifest.js.map +123 -0
- package/dist/manifest.json +63 -0
- package/package.json +69 -49
package/dist/index.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
2
17
|
// src/index.ts
|
|
3
18
|
import {
|
|
4
19
|
defineReactiveQuery,
|
|
@@ -42,5 +57,5 @@ export {
|
|
|
42
57
|
createCountersPack
|
|
43
58
|
};
|
|
44
59
|
|
|
45
|
-
//# debugId=
|
|
60
|
+
//# debugId=EBB652243E1775F464756E2164756E21
|
|
46
61
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"/**\n * `@absolutejs/sync-pack-counters` — read-set-tracked live counts.\n *\n * Each counter is a `defineReactiveQuery` whose compute function reads\n * through `ctx.db` to derive a numeric value from host tables. The engine\n * re-runs the query (and pushes the new value to subscribers) whenever any\n * table it read changes — no manual invalidation, no operator graph, no\n * polling. Counters are derived views; the pack owns no tables.\n *\n * @example\n * ```ts\n * import { createSyncEngine } from '@absolutejs/sync/engine';\n * import { createCountersPack } from '@absolutejs/sync-pack-counters';\n *\n * const engine = createSyncEngine();\n * engine.registerReader('tasks', { all: () => db.tasks.list() });\n * engine.registerReader('notifications', { all: () => db.notifications.list() });\n *\n * engine.registerPack(createCountersPack({\n * counters: {\n * openTasks: async ({ db }) =>\n * (await db.all<Task>('tasks')).filter((task) => !task.done).length,\n * unreadNotifications: async ({ db, ctx }) =>\n * (await db.all<Notification>('notifications'))\n * .filter((n) => n.actorId === ctx.userId && n.readAt === null)\n * .length,\n * },\n * }));\n *\n * // Subscribe to a single counter — emits one row { id, key, value }.\n * useSyncCollection<CounterRow>({ collection: 'counter:openTasks' });\n * ```\n */\n\nimport {\n\tdefineReactiveQuery,\n\tdefineSyncPack,\n\ttype CollectionContext,\n\ttype ReadHandle,\n\ttype SyncPack\n} from '@absolutejs/sync/engine';\n\n/** One reactive count emission. */\nexport type CounterRow = {\n\t/** Row key — matches the counter's name. */\n\tid: string;\n\tkey: string;\n\tvalue: number;\n\t/** Epoch ms the compute completed. Useful for \"as of\" labelling. */\n\tcomputedAt: number;\n};\n\n/** What a counter's `compute` sees. Same shape as a reactive query's run-context. */\nexport type CounterContext<Ctx> = {\n\tdb: ReadHandle;\n\tctx: Ctx;\n};\n\n/** Function form — convenience when no per-counter authorize override is needed. */\nexport type CounterCompute<Ctx> = (\n\tcontext: CounterContext<Ctx>\n) => number | Promise<number>;\n\n/** Full form — `compute` + per-counter `authorize` override. */\nexport type CounterDefinition<Ctx = CollectionContext> = {\n\t/**\n\t * Compute the counter value. Reads through `db` are tracked — the\n\t * query re-runs whenever any table this compute read changes. Use\n\t * `db.where(...)` when possible for range-level dependencies that\n\t * are cheaper than full-table.\n\t */\n\tcompute: CounterCompute<Ctx>;\n\t/**\n\t * Optional access control. Return false (or throw) to deny the\n\t * subscription. Default: any ctx with a defined `getActorId` value.\n\t * For a \"global\" counter (e.g. site-wide stats visible to everyone),\n\t * return `() => true`.\n\t */\n\tauthorize?: (ctx: Ctx) => boolean | Promise<boolean>;\n};\n\n/** A counter is either a bare compute function or a full definition object. */\nexport type CounterEntry<Ctx = CollectionContext> =\n\t| CounterCompute<Ctx>\n\t| CounterDefinition<Ctx>;\n\nconst asCounterDefinition = <Ctx>(\n\tentry: CounterEntry<Ctx>\n): CounterDefinition<Ctx> =>\n\ttypeof entry === 'function' ? { compute: entry } : entry;\n\nexport type CountersPackConfig<Ctx = CollectionContext> = {\n\tprefix?: string;\n\t/**\n\t * Used by the default `authorize` to gate counter subscriptions to\n\t * authenticated callers. Override `authorize` per-counter for global\n\t * counters. Default `(ctx) => ctx.userId`.\n\t */\n\tgetActorId?: (ctx: Ctx) => string | undefined;\n\t/**\n\t * Counter definitions keyed by name. Each value can be a bare compute\n\t * function (the common case) or a full {@link CounterDefinition} when\n\t * you need to override the default authorize.\n\t */\n\tcounters: Record<string, CounterEntry<Ctx>>;\n};\n\nconst now = () => Date.now();\n\n/**\n * Build a {@link SyncPack} that exposes read-set-tracked counters. Each\n * entry in `config.counters` becomes a separate reactive query named\n * `${prefix}counter:${key}` returning a single {@link CounterRow}.\n */\nexport const createCountersPack = <Ctx = CollectionContext>(\n\tconfig: CountersPackConfig<Ctx>\n): SyncPack => {\n\tconst prefix = config.prefix ?? '';\n\tconst getActorId = (config.getActorId ??\n\t\t((ctx: CollectionContext) =>\n\t\t\t(ctx as { userId?: string }).userId)) as (\n\t\tctx: CollectionContext\n\t) => string | undefined;\n\n\tconst reactiveQueries = Object.entries(config.counters).map(\n\t\t([key, entry]) => {\n\t\t\tconst counter = asCounterDefinition<Ctx>(entry);\n\t\t\tconst collectionName = `${prefix}counter:${key}`;\n\t\t\treturn defineReactiveQuery<CounterRow, void, CollectionContext>({\n\t\t\t\tname: collectionName,\n\t\t\t\tkey: (row) => row.id,\n\t\t\t\trun: async ({ db, ctx }) => {\n\t\t\t\t\tconst value = await counter.compute({\n\t\t\t\t\t\tctx: ctx as Ctx,\n\t\t\t\t\t\tdb\n\t\t\t\t\t});\n\t\t\t\t\treturn [{ computedAt: now(), id: key, key, value }];\n\t\t\t\t},\n\t\t\t\tauthorize: (_params, ctx) => {\n\t\t\t\t\tif (counter.authorize !== undefined) {\n\t\t\t\t\t\treturn counter.authorize(ctx as Ctx);\n\t\t\t\t\t}\n\t\t\t\t\treturn getActorId(ctx) !== undefined;\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t);\n\n\treturn defineSyncPack({\n\t\tname: '@absolutejs/sync-pack-counters',\n\t\townsTables: [],\n\t\treadsTables: [],\n\t\tversion: '0.1.0',\n\t\treactiveQueries\n\t});\n};\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAkCA;AAAA;AAAA;AAAA;AAoDA,IAAM,sBAAsB,CAC3B,UAEA,OAAO,UAAU,aAAa,EAAE,SAAS,MAAM,IAAI;AAkBpD,IAAM,MAAM,MAAM,KAAK,IAAI;AAOpB,IAAM,qBAAqB,CACjC,WACc;AAAA,EACd,MAAM,SAAS,OAAO,UAAU;AAAA,EAChC,MAAM,aAAc,OAAO,eACzB,CAAC,QACA,IAA4B;AAAA,EAI/B,MAAM,kBAAkB,OAAO,QAAQ,OAAO,QAAQ,EAAE,IACvD,EAAE,KAAK,WAAW;AAAA,IACjB,MAAM,UAAU,oBAAyB,KAAK;AAAA,IAC9C,MAAM,iBAAiB,GAAG,iBAAiB;AAAA,IAC3C,OAAO,oBAAyD;AAAA,MAC/D,MAAM;AAAA,MACN,KAAK,CAAC,QAAQ,IAAI;AAAA,MAClB,KAAK,SAAS,IAAI,UAAU;AAAA,QAC3B,MAAM,QAAQ,MAAM,QAAQ,QAAQ;AAAA,UACnC;AAAA,UACA;AAAA,QACD,CAAC;AAAA,QACD,OAAO,CAAC,EAAE,YAAY,IAAI,GAAG,IAAI,KAAK,KAAK,MAAM,CAAC;AAAA;AAAA,MAEnD,WAAW,CAAC,SAAS,QAAQ;AAAA,QAC5B,IAAI,QAAQ,cAAc,WAAW;AAAA,UACpC,OAAO,QAAQ,UAAU,GAAU;AAAA,QACpC;AAAA,QACA,OAAO,WAAW,GAAG,MAAM;AAAA;AAAA,IAE7B,CAAC;AAAA,GAEH;AAAA,EAEA,OAAO,eAAe;AAAA,IACrB,MAAM;AAAA,IACN,YAAY,CAAC;AAAA,IACb,aAAa,CAAC;AAAA,IACd,SAAS;AAAA,IACT;AAAA,EACD,CAAC;AAAA;",
|
|
8
|
+
"debugId": "EBB652243E1775F464756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SyncPack } from "@absolutejs/sync/engine";
|
|
2
|
+
import type { CountersPackConfig } from "./index";
|
|
3
|
+
export declare const manifest: Omit<import("@absolutejs/manifest").PackageManifest<CountersPackConfig, SyncPack>, "contract" | "settings" | "tools"> & (({
|
|
4
|
+
contract: 1;
|
|
5
|
+
tools?: Record<string, import("@absolutejs/manifest").LegacyManifestTool<SyncPack>> | undefined;
|
|
6
|
+
} | {
|
|
7
|
+
contract: 2;
|
|
8
|
+
tools?: Record<string, import("@absolutejs/manifest").AuthorizedManifestTool<SyncPack>> | undefined;
|
|
9
|
+
}) & {
|
|
10
|
+
settings: import("@sinclair/typebox").TObject<{
|
|
11
|
+
prefix: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
12
|
+
}>;
|
|
13
|
+
});
|