@nodora/client 0.0.2 → 0.0.3
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/README.md +1 -1
- package/lib/client.d.ts +32 -0
- package/lib/client.js +41 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ const result = await checkout.evaluate("IsDiscountEligible", {
|
|
|
24
24
|
});
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
For
|
|
27
|
+
For detailed information, see the official [documentation](https://nodora.org/docs).
|
|
28
28
|
|
|
29
29
|
## License
|
|
30
30
|
|
package/lib/client.d.ts
CHANGED
|
@@ -130,6 +130,38 @@ export declare class RulesetHandle {
|
|
|
130
130
|
rule: string,
|
|
131
131
|
input?: Record<string, any>,
|
|
132
132
|
): Promise<EvaluationResult>;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Registers a handler to be invoked when the given signal is emitted
|
|
136
|
+
* during an `evaluate()` call on this handle.
|
|
137
|
+
*
|
|
138
|
+
* Multiple handlers can be registered for the same signal; all are
|
|
139
|
+
* called in registration order. Returns the handle for chaining.
|
|
140
|
+
*
|
|
141
|
+
* @param signal - The name of the signal to listen for.
|
|
142
|
+
* @param handler - Called with the signal's arguments when emitted.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* client.ruleset("Checkout")
|
|
146
|
+
* .on("discount_applied", (amount) => log(amount))
|
|
147
|
+
* .on("fraud_flagged", (reason) => alert(reason));
|
|
148
|
+
*/
|
|
149
|
+
on(
|
|
150
|
+
signal: string,
|
|
151
|
+
handler: (...args: any[]) => void,
|
|
152
|
+
): this;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Removes a previously registered handler for the given signal.
|
|
156
|
+
* The handler reference must match the one passed to `on()`.
|
|
157
|
+
*
|
|
158
|
+
* @param signal - The name of the signal.
|
|
159
|
+
* @param handler - The handler to remove.
|
|
160
|
+
*/
|
|
161
|
+
off(
|
|
162
|
+
signal: string,
|
|
163
|
+
handler: (...args: any[]) => void,
|
|
164
|
+
): this;
|
|
133
165
|
}
|
|
134
166
|
|
|
135
167
|
/**
|
package/lib/client.js
CHANGED
|
@@ -46,11 +46,13 @@ export class NodoraClient {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
_evaluate(rulesetName, ruleName, input) {
|
|
49
|
-
if (this._abortController.signal.aborted)
|
|
49
|
+
if (this._abortController.signal.aborted) {
|
|
50
|
+
throw new Error("client is destroyed");
|
|
51
|
+
}
|
|
50
52
|
|
|
51
53
|
const entry = this._cache.get(rulesetName);
|
|
52
54
|
if (!entry) {
|
|
53
|
-
throw new Error(`
|
|
55
|
+
throw new Error(`ruleset "${rulesetName}" not found`);
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
let result;
|
|
@@ -129,7 +131,7 @@ export class NodoraClient {
|
|
|
129
131
|
});
|
|
130
132
|
|
|
131
133
|
if (!res.ok) {
|
|
132
|
-
throw new Error(`
|
|
134
|
+
throw new Error(`failed to fetch releases: HTTP ${res.status}`);
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
return res.json();
|
|
@@ -238,10 +240,45 @@ class RulesetHandle {
|
|
|
238
240
|
constructor(client, ruleset) {
|
|
239
241
|
this._client = client;
|
|
240
242
|
this._ruleset = ruleset;
|
|
243
|
+
this._signals = new Map();
|
|
241
244
|
}
|
|
242
245
|
|
|
243
246
|
async evaluate(rule, input = {}) {
|
|
244
247
|
await this._client.prefetch();
|
|
245
|
-
|
|
248
|
+
const result = this._client._evaluate(this._ruleset, rule, input);
|
|
249
|
+
|
|
250
|
+
if (result.emitted_signals && result.emitted_signals.length > 0) {
|
|
251
|
+
for (const signal of result.emitted_signals) {
|
|
252
|
+
const handlers = this._signals.get(signal.name);
|
|
253
|
+
if (handlers) {
|
|
254
|
+
for (const handler of handlers) {
|
|
255
|
+
try {
|
|
256
|
+
handler(...signal.args);
|
|
257
|
+
} catch (err) {
|
|
258
|
+
this._client._onError(err);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
on(signal, handler) {
|
|
268
|
+
let handlers = this._signals.get(signal);
|
|
269
|
+
if (!handlers) {
|
|
270
|
+
handlers = new Set();
|
|
271
|
+
this._signals.set(signal, handlers);
|
|
272
|
+
}
|
|
273
|
+
handlers.add(handler);
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
off(signal, handler) {
|
|
278
|
+
const handlers = this._signals.get(signal);
|
|
279
|
+
if (!handlers) return this;
|
|
280
|
+
handlers.delete(handler);
|
|
281
|
+
if (handlers.size === 0) this._signals.delete(signal);
|
|
282
|
+
return this;
|
|
246
283
|
}
|
|
247
284
|
}
|