@peers-app/peers-sdk 0.14.0 → 0.15.0
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/context/data-context.d.ts +4 -4
- package/dist/context/data-context.js +1 -1
- package/dist/context/index.d.ts +3 -3
- package/dist/context/index.js +4 -0
- package/dist/context/user-context-singleton.js +13 -14
- package/dist/context/user-context.d.ts +4 -4
- package/dist/context/user-context.js +48 -31
- package/dist/data/assistants.d.ts +1 -1
- package/dist/data/assistants.js +35 -24
- package/dist/data/change-tracking.d.ts +8 -8
- package/dist/data/change-tracking.js +45 -39
- package/dist/data/channels.js +5 -5
- package/dist/data/data-locks.d.ts +2 -2
- package/dist/data/data-locks.js +21 -23
- package/dist/data/data-locks.test.js +73 -75
- package/dist/data/device-sync-info.d.ts +1 -1
- package/dist/data/device-sync-info.js +4 -4
- package/dist/data/devices.d.ts +1 -1
- package/dist/data/devices.js +9 -12
- package/dist/data/embeddings.js +14 -11
- package/dist/data/files/file-read-stream.d.ts +2 -2
- package/dist/data/files/file-read-stream.js +23 -14
- package/dist/data/files/file-write-stream.d.ts +2 -2
- package/dist/data/files/file-write-stream.js +8 -8
- package/dist/data/files/file.types.d.ts +2 -2
- package/dist/data/files/file.types.js +17 -11
- package/dist/data/files/files.d.ts +6 -6
- package/dist/data/files/files.js +17 -19
- package/dist/data/files/files.test.js +213 -214
- package/dist/data/files/index.d.ts +4 -4
- package/dist/data/files/index.js +4 -4
- package/dist/data/group-member-roles.js +2 -2
- package/dist/data/group-members.d.ts +5 -5
- package/dist/data/group-members.js +27 -18
- package/dist/data/group-members.test.js +73 -73
- package/dist/data/group-permissions.d.ts +3 -3
- package/dist/data/group-permissions.js +13 -11
- package/dist/data/group-share.d.ts +2 -2
- package/dist/data/group-share.js +29 -24
- package/dist/data/groups.d.ts +4 -4
- package/dist/data/groups.js +27 -19
- package/dist/data/groups.test.js +44 -44
- package/dist/data/index.d.ts +6 -6
- package/dist/data/index.js +6 -6
- package/dist/data/knowledge/peer-types.js +9 -9
- package/dist/data/messages.d.ts +5 -5
- package/dist/data/messages.js +43 -30
- package/dist/data/orm/client-proxy.data-source.d.ts +4 -4
- package/dist/data/orm/client-proxy.data-source.js +10 -12
- package/dist/data/orm/cursor.d.ts +1 -1
- package/dist/data/orm/cursor.js +2 -2
- package/dist/data/orm/cursor.test.js +92 -93
- package/dist/data/orm/data-query.d.ts +3 -3
- package/dist/data/orm/data-query.js +24 -18
- package/dist/data/orm/data-query.mongo.d.ts +1 -1
- package/dist/data/orm/data-query.mongo.js +49 -51
- package/dist/data/orm/data-query.mongo.test.js +173 -204
- package/dist/data/orm/data-query.sqlite.d.ts +1 -1
- package/dist/data/orm/data-query.sqlite.js +84 -73
- package/dist/data/orm/data-query.sqlite.test.js +164 -176
- package/dist/data/orm/data-query.test.js +216 -224
- package/dist/data/orm/decorators.js +3 -3
- package/dist/data/orm/dependency-injection.test.js +53 -56
- package/dist/data/orm/doc.d.ts +4 -4
- package/dist/data/orm/doc.js +17 -21
- package/dist/data/orm/event-registry.d.ts +1 -1
- package/dist/data/orm/event-registry.test.js +16 -16
- package/dist/data/orm/factory.d.ts +2 -2
- package/dist/data/orm/factory.js +33 -33
- package/dist/data/orm/index.d.ts +10 -10
- package/dist/data/orm/index.js +10 -10
- package/dist/data/orm/multi-cursors.d.ts +1 -1
- package/dist/data/orm/multi-cursors.js +6 -6
- package/dist/data/orm/multi-cursors.test.js +152 -144
- package/dist/data/orm/sql.data-source.d.ts +7 -7
- package/dist/data/orm/sql.data-source.js +88 -93
- package/dist/data/orm/sql.data-source.test.js +109 -101
- package/dist/data/orm/subscribable.data-source.d.ts +4 -4
- package/dist/data/orm/subscribable.data-source.js +5 -5
- package/dist/data/orm/table-container-events.test.js +34 -26
- package/dist/data/orm/table-container.d.ts +6 -6
- package/dist/data/orm/table-container.js +33 -21
- package/dist/data/orm/table-container.test.js +64 -53
- package/dist/data/orm/table-definitions.system.d.ts +3 -3
- package/dist/data/orm/table-definitions.system.js +3 -3
- package/dist/data/orm/table-definitions.type.d.ts +5 -5
- package/dist/data/orm/table-dependencies.d.ts +2 -2
- package/dist/data/orm/table.d.ts +5 -5
- package/dist/data/orm/table.event-source.test.js +105 -115
- package/dist/data/orm/table.js +35 -34
- package/dist/data/orm/types.d.ts +3 -3
- package/dist/data/orm/types.js +26 -25
- package/dist/data/orm/types.test.js +166 -92
- package/dist/data/package-permissions.d.ts +1 -1
- package/dist/data/package-permissions.js +2 -2
- package/dist/data/package-version-permissions.d.ts +1 -1
- package/dist/data/package-version-permissions.js +2 -2
- package/dist/data/package-versions.d.ts +9 -9
- package/dist/data/package-versions.js +47 -33
- package/dist/data/packages.d.ts +2 -2
- package/dist/data/packages.js +36 -18
- package/dist/data/packages.utils.d.ts +2 -2
- package/dist/data/packages.utils.js +4 -4
- package/dist/data/persistent-vars.d.ts +15 -15
- package/dist/data/persistent-vars.js +165 -154
- package/dist/data/table-definitions-table.d.ts +5 -5
- package/dist/data/table-definitions-table.js +13 -12
- package/dist/data/tool-tests.js +6 -6
- package/dist/data/tools.js +29 -19
- package/dist/data/user-permissions.d.ts +1 -1
- package/dist/data/user-permissions.js +5 -5
- package/dist/data/user-permissions.test.js +90 -88
- package/dist/data/user-trust-levels.js +10 -10
- package/dist/data/users.d.ts +4 -4
- package/dist/data/users.js +16 -15
- package/dist/data/voice-messages.d.ts +2 -2
- package/dist/data/voice-messages.js +13 -13
- package/dist/data/welcome-modal.pvar.js +3 -1
- package/dist/data/workflow-logs.js +26 -18
- package/dist/data/workflow-runs.d.ts +6 -6
- package/dist/data/workflow-runs.js +70 -44
- package/dist/data/workflows.d.ts +2 -2
- package/dist/data/workflows.js +7 -9
- package/dist/device/binary-peer-connection-v2.d.ts +7 -7
- package/dist/device/binary-peer-connection-v2.js +32 -28
- package/dist/device/binary-peer-connection-v2.test.js +80 -67
- package/dist/device/binary-peer-connection.d.ts +7 -7
- package/dist/device/binary-peer-connection.js +29 -28
- package/dist/device/binary-peer-connection.test.js +35 -31
- package/dist/device/connection.d.ts +5 -5
- package/dist/device/connection.js +59 -48
- package/dist/device/connection.test.js +74 -68
- package/dist/device/device-election.d.ts +2 -2
- package/dist/device/device-election.js +25 -20
- package/dist/device/device-election.test.js +35 -36
- package/dist/device/device.d.ts +2 -2
- package/dist/device/device.js +10 -4
- package/dist/device/device.test.js +16 -17
- package/dist/device/get-trust-level-fn.d.ts +2 -2
- package/dist/device/get-trust-level-fn.js +22 -11
- package/dist/device/get-trust-level-fn.test.js +58 -58
- package/dist/device/socket-io-binary-peer.d.ts +1 -1
- package/dist/device/socket-io-binary-peer.js +16 -13
- package/dist/device/socket.type.d.ts +2 -2
- package/dist/device/streamed-socket.d.ts +2 -2
- package/dist/device/streamed-socket.js +8 -8
- package/dist/device/streamed-socket.test.js +40 -40
- package/dist/device/tx-encoding.test.js +77 -77
- package/dist/events.d.ts +1 -1
- package/dist/events.js +5 -2
- package/dist/group-invite/group-invite.js +110 -19
- package/dist/group-invite/group-invite.pvars.d.ts +2 -2
- package/dist/group-invite/group-invite.pvars.js +21 -13
- package/dist/group-invite/group-invite.types.d.ts +1 -1
- package/dist/group-invite/index.d.ts +3 -3
- package/dist/group-invite/index.js +1 -1
- package/dist/index.d.ts +25 -24
- package/dist/index.js +30 -25
- package/dist/keys.d.ts +3 -3
- package/dist/keys.js +31 -30
- package/dist/keys.test.js +69 -61
- package/dist/logging/console-logger.d.ts +1 -1
- package/dist/logging/console-logger.js +35 -40
- package/dist/logging/console-logger.test.js +115 -115
- package/dist/logging/console-logs.table.d.ts +3 -3
- package/dist/logging/console-logs.table.js +28 -23
- package/dist/mentions.js +16 -12
- package/dist/observable.d.ts +2 -2
- package/dist/observable.js +15 -9
- package/dist/observable.test.js +47 -47
- package/dist/package-loader/get-require.js +3 -4
- package/dist/package-loader/package-loader.d.ts +2 -2
- package/dist/package-loader/package-loader.js +52 -34
- package/dist/peers-ui/peers-ui.d.ts +2 -2
- package/dist/peers-ui/peers-ui.js +2 -4
- package/dist/peers-ui/peers-ui.types.d.ts +3 -3
- package/dist/peers-ui/peers-ui.types.js +0 -1
- package/dist/rpc-types.d.ts +61 -59
- package/dist/rpc-types.js +61 -55
- package/dist/serial-json.d.ts +1 -1
- package/dist/serial-json.js +50 -43
- package/dist/serial-json.test.js +22 -22
- package/dist/system-ids.js +8 -8
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/tools-factory.d.ts +1 -1
- package/dist/tools/tools-factory.js +2 -2
- package/dist/types/assistant-runner-args.d.ts +3 -3
- package/dist/types/peer-device.d.ts +1 -1
- package/dist/types/peers-package.d.ts +3 -3
- package/dist/types/workflow-logger.d.ts +1 -1
- package/dist/types/workflow-run-context.d.ts +4 -4
- package/dist/types/workflow.d.ts +4 -4
- package/dist/types/workflow.js +27 -14
- package/dist/types/zod-types.d.ts +2 -1
- package/dist/types/zod-types.js +9 -3
- package/dist/user-connect/connection-code.d.ts +1 -1
- package/dist/user-connect/connection-code.js +7 -7
- package/dist/user-connect/connection-code.test.js +106 -106
- package/dist/user-connect/index.d.ts +3 -3
- package/dist/user-connect/index.js +1 -1
- package/dist/user-connect/user-connect.pvars.js +13 -11
- package/dist/user-connect/user-connect.types.d.ts +3 -3
- package/dist/users.query.d.ts +2 -2
- package/dist/users.query.js +40 -30
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +34 -32
- package/dist/utils.test.js +12 -8
- package/dist/workflow-log-formatter.d.ts +1 -1
- package/dist/workflow-log-formatter.js +17 -18
- package/package.json +14 -8
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { type ITableDependencies, Table } from "../data/orm";
|
|
3
|
+
import { type ITableMetaData } from "../data/orm/types";
|
|
4
4
|
export declare const consoleLogSchema: z.ZodObject<{
|
|
5
5
|
logId: z.ZodEffects<z.ZodString, string, string>;
|
|
6
6
|
timestamp: z.ZodDefault<z.ZodNumber>;
|
|
@@ -13,9 +13,9 @@ export declare const consoleLogSchema: z.ZodObject<{
|
|
|
13
13
|
stackTrace: z.ZodOptional<z.ZodString>;
|
|
14
14
|
}, "strip", z.ZodTypeAny, {
|
|
15
15
|
message: string;
|
|
16
|
+
timestamp: number;
|
|
16
17
|
level: "error" | "debug" | "info" | "log" | "warn";
|
|
17
18
|
logId: string;
|
|
18
|
-
timestamp: number;
|
|
19
19
|
process: string;
|
|
20
20
|
processInstanceId: string;
|
|
21
21
|
source?: string | undefined;
|
|
@@ -37,34 +37,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
exports.ConsoleLogsTable = exports.consoleLogSchema = void 0;
|
|
38
38
|
exports.ConsoleLogs = ConsoleLogs;
|
|
39
39
|
const zod_1 = require("zod");
|
|
40
|
-
const zod_types_1 = require("../types/zod-types");
|
|
41
|
-
const types_1 = require("../data/orm/types");
|
|
42
40
|
const user_context_singleton_1 = require("../context/user-context-singleton");
|
|
41
|
+
const orm_1 = require("../data/orm");
|
|
43
42
|
const table_definitions_system_1 = require("../data/orm/table-definitions.system");
|
|
43
|
+
const types_1 = require("../data/orm/types");
|
|
44
|
+
const zod_types_1 = require("../types/zod-types");
|
|
44
45
|
const utils_1 = require("../utils");
|
|
45
|
-
const orm_1 = require("../data/orm");
|
|
46
46
|
exports.consoleLogSchema = zod_1.z.object({
|
|
47
47
|
logId: zod_types_1.zodPeerId,
|
|
48
|
-
timestamp: zod_1.z
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
timestamp: zod_1.z
|
|
49
|
+
.number()
|
|
50
|
+
.default(() => (0, utils_1.getTimestamp)())
|
|
51
|
+
.describe("The timestamp the log was created created"),
|
|
52
|
+
level: zod_1.z.enum(["debug", "info", "log", "warn", "error"]).describe("The log level"),
|
|
53
|
+
process: zod_1.z.string().describe("The process that generated the log (e.g., main, renderer, worker)"),
|
|
54
|
+
processInstanceId: zod_1.z.string().describe("Unique ID for this process instance to filter own logs"),
|
|
55
|
+
source: zod_1.z.string().optional().describe("The source file or module that generated the log"),
|
|
56
|
+
message: zod_1.z.string().describe("The log message"),
|
|
57
|
+
context: zod_types_1.zodAnyObjectOrArray.optional().describe("Additional structured context data"),
|
|
58
|
+
stackTrace: zod_1.z.string().optional().describe("Stack trace for errors"),
|
|
56
59
|
});
|
|
57
60
|
const metaData = {
|
|
58
|
-
name:
|
|
59
|
-
description:
|
|
60
|
-
primaryKeyName:
|
|
61
|
+
name: "ConsoleLogs",
|
|
62
|
+
description: "System-wide console log entries with cross-process visibility.",
|
|
63
|
+
primaryKeyName: "logId",
|
|
61
64
|
fields: (0, types_1.schemaToFields)(exports.consoleLogSchema),
|
|
62
65
|
localOnly: true, // Don't sync logs between devices
|
|
63
66
|
indexes: [
|
|
64
|
-
{ fields: [
|
|
65
|
-
{ fields: [
|
|
66
|
-
{ fields: [
|
|
67
|
-
{ fields: [
|
|
67
|
+
{ fields: ["timestamp"] },
|
|
68
|
+
{ fields: ["level"] },
|
|
69
|
+
{ fields: ["process"] },
|
|
70
|
+
{ fields: ["processInstanceId"] },
|
|
68
71
|
],
|
|
69
72
|
};
|
|
70
73
|
let ConsoleLogsTable = (() => {
|
|
@@ -85,7 +88,7 @@ let ConsoleLogsTable = (() => {
|
|
|
85
88
|
LOG_CLEANUP_INTERVAL = 24 * 60 * 60 * 1000; // 1 day
|
|
86
89
|
constructor(metaData, deps) {
|
|
87
90
|
super(metaData, deps);
|
|
88
|
-
if (typeof process !==
|
|
91
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
|
|
89
92
|
this.initializeLogCleanup();
|
|
90
93
|
}
|
|
91
94
|
}
|
|
@@ -97,7 +100,7 @@ let ConsoleLogsTable = (() => {
|
|
|
97
100
|
this.logCleanupInitialized = true;
|
|
98
101
|
// always cleanup old logs on startup - randomly spread out of the first 60 seconds to not slam the db
|
|
99
102
|
setTimeout(() => this.deleteOldLogs(), 60_000 * Math.random());
|
|
100
|
-
// regularly clean up logs
|
|
103
|
+
// regularly clean up logs
|
|
101
104
|
// TODO - consider doing this as a workflow
|
|
102
105
|
setInterval(() => this.deleteOldLogs(), this.LOG_CLEANUP_INTERVAL);
|
|
103
106
|
}
|
|
@@ -113,12 +116,14 @@ let ConsoleLogsTable = (() => {
|
|
|
113
116
|
}
|
|
114
117
|
if (ds instanceof orm_1.SQLDataSource) {
|
|
115
118
|
const db = ds.db;
|
|
116
|
-
const result = await db.exec(`delete from ConsoleLogs where timestamp < $timestamp`, {
|
|
119
|
+
const result = await db.exec(`delete from ConsoleLogs where timestamp < $timestamp`, {
|
|
120
|
+
timestamp: cutoffTimestamp,
|
|
121
|
+
});
|
|
117
122
|
console.log(`bulk deleted ${count} logs, changes: ${result.changes}`);
|
|
118
123
|
}
|
|
119
124
|
else {
|
|
120
|
-
const cursor = this.cursor({ timestamp: { $lt: cutoffTimestamp } }, { sortBy: [
|
|
121
|
-
|
|
125
|
+
const cursor = this.cursor({ timestamp: { $lt: cutoffTimestamp } }, { sortBy: ["-timestamp"] });
|
|
126
|
+
const count2 = 0;
|
|
122
127
|
for await (const log of cursor) {
|
|
123
128
|
this.delete(log.logId);
|
|
124
129
|
}
|
package/dist/mentions.js
CHANGED
|
@@ -6,32 +6,36 @@ exports.getAllMentions = exports.formatMention = exports.mentionRegex = void 0;
|
|
|
6
6
|
const mentionRegex = () => /<(@|\$)([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]+):([a-zA-Z0-9]{25})?>/;
|
|
7
7
|
exports.mentionRegex = mentionRegex;
|
|
8
8
|
const formatMention = (data) => {
|
|
9
|
-
let prefix =
|
|
10
|
-
if (data.kind ===
|
|
11
|
-
prefix =
|
|
9
|
+
let prefix = "$";
|
|
10
|
+
if (data.kind === "assistant" || data.kind === "user") {
|
|
11
|
+
prefix = "@";
|
|
12
12
|
}
|
|
13
|
-
let name = data.name.replace(/\s/g,
|
|
13
|
+
let name = data.name.replace(/\s/g, "_");
|
|
14
14
|
// remove non-alphanumeric characters
|
|
15
|
-
name = name.replace(/[^a-zA-Z0-9_-]/g,
|
|
15
|
+
name = name.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
16
16
|
let mention = `<${prefix}${data.kind}:${name}:${data.id}`;
|
|
17
17
|
// if (data.kind === 'assistant') {
|
|
18
18
|
// mention += `:${data.userId}`;
|
|
19
19
|
// }
|
|
20
|
-
mention +=
|
|
20
|
+
mention += ">";
|
|
21
21
|
return mention;
|
|
22
22
|
};
|
|
23
23
|
exports.formatMention = formatMention;
|
|
24
24
|
const getAllMentions = (markdown) => {
|
|
25
25
|
const mentions = [];
|
|
26
|
-
let
|
|
26
|
+
let _lastIndex = 0;
|
|
27
27
|
let match;
|
|
28
|
-
let
|
|
28
|
+
let _cnt = 0;
|
|
29
29
|
const regex = (0, exports.mentionRegex)();
|
|
30
|
-
while (
|
|
31
|
-
|
|
30
|
+
while (true) {
|
|
31
|
+
match = regex.exec(markdown);
|
|
32
|
+
if (match === null) {
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
_cnt++;
|
|
32
36
|
markdown = markdown.slice(match.index + match[0].length);
|
|
33
37
|
const kind = match[2];
|
|
34
|
-
const name = match[3].replace(/_/g,
|
|
38
|
+
const name = match[3].replace(/_/g, " ");
|
|
35
39
|
const id = match[4];
|
|
36
40
|
const data = {
|
|
37
41
|
kind: kind,
|
|
@@ -39,7 +43,7 @@ const getAllMentions = (markdown) => {
|
|
|
39
43
|
id,
|
|
40
44
|
};
|
|
41
45
|
mentions.push(data);
|
|
42
|
-
|
|
46
|
+
_lastIndex = regex.lastIndex;
|
|
43
47
|
}
|
|
44
48
|
return mentions;
|
|
45
49
|
};
|
package/dist/observable.d.ts
CHANGED
|
@@ -13,10 +13,10 @@ type ObservableProps<T> = {
|
|
|
13
13
|
export type Observable<T> = ObservableFn<T> & ObservableProps<T>;
|
|
14
14
|
export declare function observable<T>(initialValue: T): Observable<T>;
|
|
15
15
|
export declare function observable<T = undefined>(): Observable<T | undefined>;
|
|
16
|
-
export declare function computed<T>(fn:
|
|
16
|
+
export declare function computed<T>(fn: {
|
|
17
17
|
read: () => T;
|
|
18
18
|
write?: (val: T) => void;
|
|
19
|
-
}
|
|
19
|
+
} | (() => T)): Observable<T>;
|
|
20
20
|
export declare function isSubscribable(obj: any): obj is Observable<any>;
|
|
21
21
|
export declare function unwrapObservable<T>(obj: T | Observable<T>): T;
|
|
22
22
|
export declare function linkObservables<T>(obs1: Observable<T>, obs2: Observable<T>): Subscription;
|
package/dist/observable.js
CHANGED
|
@@ -13,7 +13,9 @@ function observable(initialValue) {
|
|
|
13
13
|
let value = initialValue;
|
|
14
14
|
const subscribers = [];
|
|
15
15
|
const obs = ((...args) => {
|
|
16
|
-
Object.values(runningComputes).forEach(c =>
|
|
16
|
+
Object.values(runningComputes).forEach((c) => {
|
|
17
|
+
c.push(obs);
|
|
18
|
+
});
|
|
17
19
|
const isSetting = args.length > 0;
|
|
18
20
|
const newValue = args[0];
|
|
19
21
|
if (isSetting && newValue !== value) {
|
|
@@ -23,7 +25,9 @@ function observable(initialValue) {
|
|
|
23
25
|
return value;
|
|
24
26
|
});
|
|
25
27
|
obs.notifySubscribers = () => {
|
|
26
|
-
subscribers.forEach(subscriber =>
|
|
28
|
+
subscribers.forEach((subscriber) => {
|
|
29
|
+
subscriber(value);
|
|
30
|
+
});
|
|
27
31
|
};
|
|
28
32
|
obs.subscribe = (subscriber) => {
|
|
29
33
|
subscribers.push(subscriber);
|
|
@@ -36,7 +40,7 @@ function observable(initialValue) {
|
|
|
36
40
|
if (iSub >= 0) {
|
|
37
41
|
subscribers.splice(iSub, 1);
|
|
38
42
|
}
|
|
39
|
-
}
|
|
43
|
+
},
|
|
40
44
|
};
|
|
41
45
|
};
|
|
42
46
|
obs.subscriberCount = () => subscribers.length;
|
|
@@ -63,7 +67,7 @@ function computed(fn) {
|
|
|
63
67
|
return recompute();
|
|
64
68
|
}
|
|
65
69
|
else {
|
|
66
|
-
throw new Error(
|
|
70
|
+
throw new Error("Computed value is readonly");
|
|
67
71
|
}
|
|
68
72
|
}
|
|
69
73
|
if (dirty) {
|
|
@@ -80,23 +84,25 @@ function computed(fn) {
|
|
|
80
84
|
}
|
|
81
85
|
};
|
|
82
86
|
// subscribe to dependencies
|
|
83
|
-
(0, lodash_1.uniq)(dependencies).forEach(dep =>
|
|
87
|
+
(0, lodash_1.uniq)(dependencies).forEach((dep) => {
|
|
88
|
+
dep.subscribe(onDepChange);
|
|
89
|
+
});
|
|
84
90
|
Object.assign(obs, _obs);
|
|
85
91
|
return obs;
|
|
86
92
|
}
|
|
87
93
|
function isSubscribable(obj) {
|
|
88
|
-
return obj && typeof obj ===
|
|
94
|
+
return obj && typeof obj === "function" && obj.subscribe && obj.notifySubscribers;
|
|
89
95
|
}
|
|
90
96
|
function unwrapObservable(obj) {
|
|
91
97
|
return isSubscribable(obj) ? obj() : obj;
|
|
92
98
|
}
|
|
93
99
|
function linkObservables(obs1, obs2) {
|
|
94
|
-
const sub1 = obs1.subscribe(value => {
|
|
100
|
+
const sub1 = obs1.subscribe((value) => {
|
|
95
101
|
if (value !== obs2()) {
|
|
96
102
|
obs2(value);
|
|
97
103
|
}
|
|
98
104
|
});
|
|
99
|
-
const sub2 = obs2.subscribe(value => {
|
|
105
|
+
const sub2 = obs2.subscribe((value) => {
|
|
100
106
|
if (value !== obs1()) {
|
|
101
107
|
obs1(value);
|
|
102
108
|
}
|
|
@@ -105,6 +111,6 @@ function linkObservables(obs1, obs2) {
|
|
|
105
111
|
dispose: () => {
|
|
106
112
|
sub1.dispose();
|
|
107
113
|
sub2.dispose();
|
|
108
|
-
}
|
|
114
|
+
},
|
|
109
115
|
};
|
|
110
116
|
}
|
package/dist/observable.test.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const observable_1 = require("./observable");
|
|
4
|
-
describe(
|
|
5
|
-
describe(
|
|
6
|
-
it(
|
|
4
|
+
describe("observable.ts", () => {
|
|
5
|
+
describe("observable", () => {
|
|
6
|
+
it("should be able to create an observable", () => {
|
|
7
7
|
const obs = (0, observable_1.observable)(3);
|
|
8
8
|
expect(obs()).toBe(3);
|
|
9
9
|
});
|
|
10
|
-
it(
|
|
10
|
+
it("should be able to update an observable", () => {
|
|
11
11
|
const obs = (0, observable_1.observable)(3);
|
|
12
12
|
obs(4);
|
|
13
13
|
expect(obs()).toBe(4);
|
|
14
14
|
});
|
|
15
|
-
it(
|
|
15
|
+
it("should be able to subscribe to an observable", () => {
|
|
16
16
|
const obs = (0, observable_1.observable)(3);
|
|
17
17
|
let value = 0;
|
|
18
|
-
const sub = obs.subscribe(newValue => {
|
|
18
|
+
const sub = obs.subscribe((newValue) => {
|
|
19
19
|
value = newValue;
|
|
20
20
|
});
|
|
21
21
|
expect(value).toBe(0);
|
|
@@ -25,30 +25,30 @@ describe('observable.ts', () => {
|
|
|
25
25
|
obs(5);
|
|
26
26
|
expect(value).toBe(4);
|
|
27
27
|
});
|
|
28
|
-
it(
|
|
28
|
+
it("should return the value when writing to itself", () => {
|
|
29
29
|
const obs = (0, observable_1.observable)(3);
|
|
30
30
|
expect(obs(4)).toBe(4);
|
|
31
31
|
});
|
|
32
|
-
it(
|
|
32
|
+
it("should be able to set an observable to undefined", () => {
|
|
33
33
|
const obs = (0, observable_1.observable)(3);
|
|
34
34
|
obs(undefined);
|
|
35
35
|
expect(obs()).toBe(undefined);
|
|
36
36
|
});
|
|
37
|
-
it(
|
|
37
|
+
it("should notify subscribers when value is set to undefined", () => {
|
|
38
38
|
const obs = (0, observable_1.observable)(3);
|
|
39
39
|
let value = 99;
|
|
40
|
-
const sub = obs.subscribe(newValue => {
|
|
40
|
+
const sub = obs.subscribe((newValue) => {
|
|
41
41
|
value = newValue;
|
|
42
42
|
});
|
|
43
43
|
obs(undefined);
|
|
44
44
|
expect(value).toBe(undefined);
|
|
45
45
|
sub.dispose();
|
|
46
46
|
});
|
|
47
|
-
it(
|
|
47
|
+
it("should be able to initialize an observable with undefined", () => {
|
|
48
48
|
const obs = (0, observable_1.observable)(undefined);
|
|
49
49
|
expect(obs()).toBe(undefined);
|
|
50
50
|
});
|
|
51
|
-
it(
|
|
51
|
+
it("should distinguish between getting (no args) and setting to undefined (with arg)", () => {
|
|
52
52
|
const obs = (0, observable_1.observable)(5);
|
|
53
53
|
// Getting with no args
|
|
54
54
|
expect(obs()).toBe(5);
|
|
@@ -56,7 +56,7 @@ describe('observable.ts', () => {
|
|
|
56
56
|
obs(undefined);
|
|
57
57
|
expect(obs()).toBe(undefined);
|
|
58
58
|
});
|
|
59
|
-
it(
|
|
59
|
+
it("should not notify subscribers when value does not change", () => {
|
|
60
60
|
const obs = (0, observable_1.observable)(3);
|
|
61
61
|
let notifyCount = 0;
|
|
62
62
|
obs.subscribe(() => {
|
|
@@ -67,8 +67,8 @@ describe('observable.ts', () => {
|
|
|
67
67
|
obs(4);
|
|
68
68
|
expect(notifyCount).toBe(1);
|
|
69
69
|
});
|
|
70
|
-
it(
|
|
71
|
-
const obs = (0, observable_1.observable)(
|
|
70
|
+
it("should handle setting undefined multiple times", () => {
|
|
71
|
+
const obs = (0, observable_1.observable)("test");
|
|
72
72
|
let notifyCount = 0;
|
|
73
73
|
obs.subscribe(() => {
|
|
74
74
|
notifyCount++;
|
|
@@ -78,12 +78,12 @@ describe('observable.ts', () => {
|
|
|
78
78
|
obs(undefined);
|
|
79
79
|
// Should not notify since value hasn't changed
|
|
80
80
|
expect(notifyCount).toBe(1);
|
|
81
|
-
obs(
|
|
81
|
+
obs("new");
|
|
82
82
|
expect(notifyCount).toBe(2);
|
|
83
83
|
obs(undefined);
|
|
84
84
|
expect(notifyCount).toBe(3);
|
|
85
85
|
});
|
|
86
|
-
it(
|
|
86
|
+
it("should correctly detect args.length for get vs set", () => {
|
|
87
87
|
const obs = (0, observable_1.observable)(10);
|
|
88
88
|
const calls = [];
|
|
89
89
|
// Wrap the observable to track calls
|
|
@@ -103,76 +103,76 @@ describe('observable.ts', () => {
|
|
|
103
103
|
wrappedObs();
|
|
104
104
|
expect(calls[2]).toEqual({ argsLength: 0, value: undefined });
|
|
105
105
|
});
|
|
106
|
-
it(
|
|
106
|
+
it("should allow creating observable without initial value for types that include undefined", () => {
|
|
107
107
|
const obs = (0, observable_1.observable)();
|
|
108
108
|
expect(obs()).toBe(undefined);
|
|
109
|
-
obs(
|
|
110
|
-
expect(obs()).toBe(
|
|
109
|
+
obs("test");
|
|
110
|
+
expect(obs()).toBe("test");
|
|
111
111
|
obs(undefined);
|
|
112
112
|
expect(obs()).toBe(undefined);
|
|
113
113
|
});
|
|
114
|
-
it(
|
|
114
|
+
it("should work with explicit type and initial value", () => {
|
|
115
115
|
const obs = (0, observable_1.observable)(42);
|
|
116
116
|
expect(obs()).toBe(42);
|
|
117
117
|
obs(100);
|
|
118
118
|
expect(obs()).toBe(100);
|
|
119
119
|
});
|
|
120
|
-
it(
|
|
120
|
+
it("should not allow creating observable without initial value for types that cannot be undefined", () => {
|
|
121
121
|
const obs = (0, observable_1.observable)();
|
|
122
122
|
// @ts-expect-error
|
|
123
123
|
const s = obs();
|
|
124
124
|
expect(s).toBeUndefined();
|
|
125
|
-
obs(
|
|
126
|
-
expect(obs()).toEqual(
|
|
125
|
+
obs("s");
|
|
126
|
+
expect(obs()).toEqual("s");
|
|
127
127
|
obs(undefined);
|
|
128
128
|
expect(obs()).toEqual(undefined);
|
|
129
|
-
const obs2 = (0, observable_1.observable)(
|
|
129
|
+
const obs2 = (0, observable_1.observable)("");
|
|
130
130
|
const s2 = obs2();
|
|
131
|
-
expect(s2).toEqual(
|
|
131
|
+
expect(s2).toEqual("");
|
|
132
132
|
// @ts-expect-error
|
|
133
133
|
obs2(undefined);
|
|
134
134
|
expect(obs2()).toBeUndefined();
|
|
135
135
|
});
|
|
136
|
-
it(
|
|
136
|
+
it("should infer type from initial value", () => {
|
|
137
137
|
// Infer number from numeric literal
|
|
138
138
|
const numObs = (0, observable_1.observable)(42);
|
|
139
139
|
const num = numObs();
|
|
140
140
|
expect(num).toBe(42);
|
|
141
141
|
// Infer string from string literal
|
|
142
|
-
const strObs = (0, observable_1.observable)(
|
|
142
|
+
const strObs = (0, observable_1.observable)("hello");
|
|
143
143
|
const str = strObs();
|
|
144
|
-
expect(str).toBe(
|
|
144
|
+
expect(str).toBe("hello");
|
|
145
145
|
// Infer boolean from boolean literal
|
|
146
146
|
const boolObs = (0, observable_1.observable)(true);
|
|
147
147
|
const bool = boolObs();
|
|
148
148
|
expect(bool).toBe(true);
|
|
149
149
|
// Infer object type
|
|
150
|
-
const objObs = (0, observable_1.observable)({ name:
|
|
150
|
+
const objObs = (0, observable_1.observable)({ name: "test", count: 5 });
|
|
151
151
|
const obj = objObs();
|
|
152
|
-
expect(obj).toEqual({ name:
|
|
152
|
+
expect(obj).toEqual({ name: "test", count: 5 });
|
|
153
153
|
// Infer array type
|
|
154
154
|
const arrObs = (0, observable_1.observable)([1, 2, 3]);
|
|
155
155
|
const arr = arrObs();
|
|
156
156
|
expect(arr).toEqual([1, 2, 3]);
|
|
157
157
|
});
|
|
158
158
|
});
|
|
159
|
-
describe(
|
|
160
|
-
it(
|
|
159
|
+
describe("computed", () => {
|
|
160
|
+
it("should be able to create a computed observable", () => {
|
|
161
161
|
const com = (0, observable_1.computed)(() => 3);
|
|
162
162
|
expect(com()).toBe(3);
|
|
163
163
|
});
|
|
164
|
-
it(
|
|
164
|
+
it("should throw if trying to set value", () => {
|
|
165
165
|
const com = (0, observable_1.computed)(() => 3);
|
|
166
166
|
expect(() => com(4)).toThrow();
|
|
167
167
|
});
|
|
168
|
-
it(
|
|
168
|
+
it("should be able to create a computed observable that depends on another observable", () => {
|
|
169
169
|
const obs1 = (0, observable_1.observable)(3);
|
|
170
170
|
const com1 = (0, observable_1.computed)(() => obs1() + 1);
|
|
171
171
|
expect(com1()).toBe(4);
|
|
172
172
|
obs1(5);
|
|
173
173
|
expect(com1()).toBe(6);
|
|
174
174
|
});
|
|
175
|
-
it(
|
|
175
|
+
it("should be able to create a computed observable that depends on another computed observable", () => {
|
|
176
176
|
const obs1 = (0, observable_1.observable)(3);
|
|
177
177
|
const com1 = (0, observable_1.computed)(() => obs1() + 1);
|
|
178
178
|
const com2 = (0, observable_1.computed)(() => com1() + 1);
|
|
@@ -180,18 +180,18 @@ describe('observable.ts', () => {
|
|
|
180
180
|
obs1(5);
|
|
181
181
|
expect(com2()).toBe(7);
|
|
182
182
|
});
|
|
183
|
-
it(
|
|
183
|
+
it("should be able to create a computed observable that depends on another computed observable that depends on another computed observable", () => {
|
|
184
184
|
const obs1 = (0, observable_1.observable)(3);
|
|
185
185
|
const com1 = (0, observable_1.computed)(() => obs1() + 1);
|
|
186
186
|
const com2 = (0, observable_1.computed)(() => com1() + 1);
|
|
187
187
|
const com3 = (0, observable_1.computed)(() => com2() + 1);
|
|
188
188
|
expect(com3()).toBe(6);
|
|
189
189
|
});
|
|
190
|
-
it(
|
|
190
|
+
it("should notify subscribers of new value when a dependency changes", () => {
|
|
191
191
|
const obs1 = (0, observable_1.observable)(3);
|
|
192
192
|
const com1 = (0, observable_1.computed)(() => obs1() + 1);
|
|
193
193
|
let newValue = 0;
|
|
194
|
-
com1.subscribe(val => {
|
|
194
|
+
com1.subscribe((val) => {
|
|
195
195
|
newValue = val;
|
|
196
196
|
});
|
|
197
197
|
expect(com1()).toBe(4);
|
|
@@ -199,7 +199,7 @@ describe('observable.ts', () => {
|
|
|
199
199
|
obs1(5);
|
|
200
200
|
expect(newValue).toBe(6);
|
|
201
201
|
});
|
|
202
|
-
it(
|
|
202
|
+
it("should not notify subscribers of new value when a dependency changes if there are no subscribers", () => {
|
|
203
203
|
const obs1 = (0, observable_1.observable)(1);
|
|
204
204
|
let computeCount = 0;
|
|
205
205
|
const com1 = (0, observable_1.computed)(() => {
|
|
@@ -207,7 +207,7 @@ describe('observable.ts', () => {
|
|
|
207
207
|
return obs1() + 1;
|
|
208
208
|
});
|
|
209
209
|
let newValue = 0;
|
|
210
|
-
const sub = com1.subscribe(val => {
|
|
210
|
+
const sub = com1.subscribe((val) => {
|
|
211
211
|
newValue = val;
|
|
212
212
|
});
|
|
213
213
|
expect(computeCount).toBe(1);
|
|
@@ -225,7 +225,7 @@ describe('observable.ts', () => {
|
|
|
225
225
|
expect(computeCount).toBe(3);
|
|
226
226
|
expect(com1()).toBe(4);
|
|
227
227
|
});
|
|
228
|
-
it(
|
|
228
|
+
it("should only recompute value when a dependency changes if there are subscribers", () => {
|
|
229
229
|
const obs1 = (0, observable_1.observable)(1);
|
|
230
230
|
let recomputeCount = 0;
|
|
231
231
|
const com1 = (0, observable_1.computed)(() => {
|
|
@@ -245,7 +245,7 @@ describe('observable.ts', () => {
|
|
|
245
245
|
expect(com1()).toBe(4);
|
|
246
246
|
expect(recomputeCount).toBe(2);
|
|
247
247
|
});
|
|
248
|
-
it(
|
|
248
|
+
it("should immediately, and still only, recompute value when a dependency changes if there are subscribers", () => {
|
|
249
249
|
const obs1 = (0, observable_1.observable)(1);
|
|
250
250
|
let recomputeCount = 0;
|
|
251
251
|
const com1 = (0, observable_1.computed)(() => {
|
|
@@ -253,7 +253,7 @@ describe('observable.ts', () => {
|
|
|
253
253
|
return obs1() + recomputeCount;
|
|
254
254
|
});
|
|
255
255
|
let newValue = 0;
|
|
256
|
-
com1.subscribe(val => {
|
|
256
|
+
com1.subscribe((val) => {
|
|
257
257
|
newValue = val;
|
|
258
258
|
});
|
|
259
259
|
expect(com1()).toBe(2);
|
|
@@ -266,7 +266,7 @@ describe('observable.ts', () => {
|
|
|
266
266
|
expect(com1()).toBe(4);
|
|
267
267
|
expect(com1()).toBe(4);
|
|
268
268
|
});
|
|
269
|
-
it(
|
|
269
|
+
it("notify should set the value and notify subscribers without recomputing", () => {
|
|
270
270
|
const obs1 = (0, observable_1.observable)(1);
|
|
271
271
|
let recomputeCount = 0;
|
|
272
272
|
const com1 = (0, observable_1.computed)(() => {
|
|
@@ -274,7 +274,7 @@ describe('observable.ts', () => {
|
|
|
274
274
|
return obs1() + recomputeCount;
|
|
275
275
|
});
|
|
276
276
|
let newValue = 0;
|
|
277
|
-
com1.subscribe(val => {
|
|
277
|
+
com1.subscribe((val) => {
|
|
278
278
|
newValue = val;
|
|
279
279
|
});
|
|
280
280
|
expect(com1()).toBe(2);
|
|
@@ -291,7 +291,7 @@ describe('observable.ts', () => {
|
|
|
291
291
|
// expect(com1()).toBe(5);
|
|
292
292
|
// expect(recomputeCount).toBe(2);
|
|
293
293
|
});
|
|
294
|
-
it(
|
|
294
|
+
it("should allow configuring a write function", () => {
|
|
295
295
|
const obs1 = (0, observable_1.observable)(1);
|
|
296
296
|
const com1 = (0, observable_1.computed)({
|
|
297
297
|
read: () => obs1() + 1,
|
|
@@ -17,12 +17,11 @@ async function getRequire(injected) {
|
|
|
17
17
|
}
|
|
18
18
|
// Try ESM fallback: createRequire(import.meta.url)
|
|
19
19
|
try {
|
|
20
|
-
const mod = await Promise.resolve().then(() => require("module"));
|
|
20
|
+
const mod = (await Promise.resolve().then(() => require("node:module")));
|
|
21
21
|
// Use import.meta.url if TS target supports it, else __filename
|
|
22
22
|
const urlOrPath =
|
|
23
|
-
// @ts-
|
|
24
|
-
typeof import.meta !== "undefined" && import.meta.url ? import.meta.url
|
|
25
|
-
: __filename;
|
|
23
|
+
// @ts-expect-error
|
|
24
|
+
typeof import.meta !== "undefined" && import.meta.url ? import.meta.url : __filename;
|
|
26
25
|
return mod.createRequire(urlOrPath);
|
|
27
26
|
}
|
|
28
27
|
catch {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DataContext } from "../context/data-context";
|
|
2
|
+
import { type IPackage } from "../data/packages";
|
|
2
3
|
import type { IPeersPackage } from "../types/peers-package";
|
|
3
|
-
import { IPackage } from "../data/packages";
|
|
4
4
|
export declare class PackageLoader {
|
|
5
5
|
readonly dataContext: DataContext;
|
|
6
6
|
static PeersSDK: any;
|
|
@@ -18,4 +18,4 @@ export declare class PackageLoader {
|
|
|
18
18
|
private _readLocalBundle;
|
|
19
19
|
private _evaluateBundle;
|
|
20
20
|
}
|
|
21
|
-
export declare function setDefaultRequire(require:
|
|
21
|
+
export declare function setDefaultRequire(require: <T>(module: string) => T): void;
|