diff --git a/.bsb.lock b/.bsb.lock index 910a7dd..9fcdd05 100644 --- a/.bsb.lock +++ b/.bsb.lock @@ -1 +1 @@ -89322 \ No newline at end of file +98937 \ No newline at end of file diff --git a/__tests__/Actor_Test.re b/__tests__/Actor_Test.re index 624b89c..b0cb6b8 100644 --- a/__tests__/Actor_Test.re +++ b/__tests__/Actor_Test.re @@ -287,6 +287,14 @@ describe("Stateful Actor", () => { }); }); +describe("System", () => + test("Can name system", () => { + let system = start(~name="albert", ()); + expect(ActorPath.systemName(ActorPath.fromReference(system))) + |> toEqual("albert"); + }) +); + describe("Persistent Actor", () => { testPromise("allows queries to resolve", () => { let system = start(~persistenceEngine=createMockPersistenceEngine(), ()); diff --git a/src/Nact.js b/src/Nact.js index 18bd435..76a6d86 100644 --- a/src/Nact.js +++ b/src/Nact.js @@ -63,7 +63,7 @@ function unsafeEncoder(obj) { }; function unsafeDecoder(result) { - if (result && typeof (result) === 'object' && result.type === WrappedEvent) { + if(result && typeof(result) === 'object' && result.type === WrappedEvent) { var serialized = result.serialized || JSON.stringify(result.data); return JSON.parse(serialized, (key, value) => { if (value && typeof (value) === 'object' && value.type === WrappedVariant) { @@ -91,21 +91,21 @@ function logLevelFromJs(param) { function logLevelToString(param) { switch (param) { - case 0: - return "off"; - case 1: - return "trace"; - case 2: - return "debug"; - case 3: - return "info"; - case 4: - return "warn"; - case 5: - return "error"; - case 6: - return "critical"; - + case 0 : + return "off"; + case 1 : + return "trace"; + case 2 : + return "debug"; + case 3 : + return "info"; + case 4 : + return "warn"; + case 5 : + return "error"; + case 6 : + return "critical"; + } } @@ -116,48 +116,48 @@ function fromJsLog(msg) { var match = msg.type; if (match == null) { return /* Unknown */Block.__(4, [ - unsafeEncoder(msg), - createdAt, - path - ]); + unsafeEncoder(msg), + createdAt, + path + ]); } else { switch (match) { - case "event": - return /* Event */Block.__(3, [ - defaultTo("", Js_primitive.null_undefined_to_opt(msg.name)), - defaultTo(null, Js_primitive.null_undefined_to_opt(msg.properties)), - createdAt, - path - ]); - case "exception": - return /* Error */Block.__(1, [ - defaultTo([ - Caml_builtin_exceptions.failure, - "Error is undefined" - ], Js_primitive.null_undefined_to_opt(msg.exception)), - createdAt, - path - ]); - case "metric": - return /* Metric */Block.__(2, [ - defaultTo("", Js_primitive.null_undefined_to_opt(msg.name)), - defaultTo(null, Js_primitive.null_undefined_to_opt(msg.values)), - createdAt, - path - ]); - case "trace": - return /* Message */Block.__(0, [ - defaultTo(/* Off */0, logLevelFromJs(defaultTo(0, Js_primitive.null_undefined_to_opt(msg.level)))), - defaultTo("", Js_primitive.null_undefined_to_opt(msg.message)), - createdAt, - path - ]); + case "event" : + return /* Event */Block.__(3, [ + defaultTo("", Js_primitive.null_undefined_to_opt(msg.name)), + defaultTo(null, Js_primitive.null_undefined_to_opt(msg.properties)), + createdAt, + path + ]); + case "exception" : + return /* Error */Block.__(1, [ + defaultTo([ + Caml_builtin_exceptions.failure, + "Error is undefined" + ], Js_primitive.null_undefined_to_opt(msg.exception)), + createdAt, + path + ]); + case "metric" : + return /* Metric */Block.__(2, [ + defaultTo("", Js_primitive.null_undefined_to_opt(msg.name)), + defaultTo(null, Js_primitive.null_undefined_to_opt(msg.values)), + createdAt, + path + ]); + case "trace" : + return /* Message */Block.__(0, [ + defaultTo(/* Off */0, logLevelFromJs(defaultTo(0, Js_primitive.null_undefined_to_opt(msg.level)))), + defaultTo("", Js_primitive.null_undefined_to_opt(msg.message)), + createdAt, + path + ]); default: return /* Unknown */Block.__(4, [ - unsafeEncoder(msg), - createdAt, - path - ]); + unsafeEncoder(msg), + createdAt, + path + ]); } } } @@ -215,7 +215,7 @@ function mapCtx(untypedCtx) { /* children */Belt_SetString.fromArray(Nact_jsMap.keys(untypedCtx.children)), /* name */untypedCtx.name, /* logger */untypedCtx.log - ]; + ]; } function mapPersistentCtx(untypedCtx, encoder) { @@ -226,12 +226,12 @@ function mapPersistentCtx(untypedCtx, encoder) { /* self : ActorRef */[untypedCtx.self], /* name */untypedCtx.name, /* persist */(function (param) { - return Curry._1(partial_arg, Curry._1(encoder, param)); - }), + return Curry._1(partial_arg, Curry._1(encoder, param)); + }), /* children */Belt_SetString.fromArray(Nact_jsMap.keys(untypedCtx.children)), /* recovering */defaultTo(/* false */0, Js_primitive.null_undefined_to_opt(untypedCtx.recovering)), /* logger */untypedCtx.log - ]; + ]; } function mapSupervisionCtx(untypedCtx) { @@ -241,42 +241,42 @@ function mapSupervisionCtx(untypedCtx) { /* self : ActorRef */[untypedCtx.self], /* name */untypedCtx.name, /* children */Belt_SetString.fromArray(Nact_jsMap.keys(untypedCtx.children)) - ]; + ]; } function mapSupervisionFunction(optionalF, decoder) { var decoder$1 = Js_option.getWithDefault((function (prim) { - return prim; - }), decoder); + return prim; + }), decoder); if (optionalF) { var f = optionalF[0]; return (function (msg, err, ctx) { - return Curry._3(f, Curry._1(decoder$1, msg), err, mapSupervisionCtx(ctx)).then((function (decision) { - var tmp; - switch (decision) { - case 0: - tmp = ctx.stop; - break; - case 1: - tmp = ctx.stopAll; - break; - case 2: - tmp = ctx.reset; - break; - case 3: - tmp = ctx.resetAll; - break; - case 4: - tmp = ctx.escalate; - break; - case 5: - tmp = ctx.resume; - break; - - } - return Promise.resolve(tmp); - })); - }); + return Curry._3(f, Curry._1(decoder$1, msg), err, mapSupervisionCtx(ctx)).then((function (decision) { + var tmp; + switch (decision) { + case 0 : + tmp = ctx.stop; + break; + case 1 : + tmp = ctx.stopAll; + break; + case 2 : + tmp = ctx.reset; + break; + case 3 : + tmp = ctx.resetAll; + break; + case 4 : + tmp = ctx.escalate; + break; + case 5 : + tmp = ctx.resume; + break; + + } + return Promise.resolve(tmp); + })); + }); } else { return undefined; } @@ -285,10 +285,10 @@ function mapSupervisionFunction(optionalF, decoder) { function useStatefulSupervisionPolicy(f, initialState) { var state = [initialState]; return (function (msg, err, ctx) { - var match = Curry._4(f, msg, err, state[0], ctx); - state[0] = match[0]; - return match[1]; - }); + var match = Curry._4(f, msg, err, state[0], ctx); + state[0] = match[0]; + return match[1]; + }); } function spawn(name, shutdownAfter, onCrash, param, func, initialState) { @@ -301,7 +301,7 @@ function spawn(name, shutdownAfter, onCrash, param, func, initialState) { try { return Curry._3(func, state, msg, mapCtx(ctx)); } - catch (raw_err) { + catch (raw_err){ return Promise.reject(Js_exn.internalToOCamlException(raw_err)); } }; @@ -318,7 +318,7 @@ function spawnStateless(name, shutdownAfter, onCrash, param, func) { try { return Curry._2(func, msg, mapCtx(ctx)); } - catch (exn) { + catch (exn){ return Promise.resolve(/* () */0); } }; @@ -328,17 +328,17 @@ function spawnStateless(name, shutdownAfter, onCrash, param, func) { function spawnPersistent(key, name, shutdownAfter, snapshotEvery, onCrash, decoder, stateDecoder, stateEncoder, encoder, param, func, initialState) { var decoder$1 = defaultTo((function (prim) { - return unsafeDecoder(prim); - }), decoder); + return unsafeDecoder(prim); + }), decoder); var stateDecoder$1 = defaultTo((function (prim) { - return unsafeDecoder(prim); - }), stateDecoder); + return unsafeDecoder(prim); + }), stateDecoder); var stateEncoder$1 = defaultTo((function (prim) { - return unsafeEncoder(prim); - }), stateEncoder); + return unsafeEncoder(prim); + }), stateEncoder); var encoder$1 = defaultTo((function (prim) { - return unsafeEncoder(prim); - }), encoder); + return unsafeEncoder(prim); + }), encoder); var options = { shutdownAfter: Js_null_undefined.fromOption(shutdownAfter), onCrash: mapSupervisionFunction(onCrash, /* Some */[decoder$1]), @@ -350,12 +350,12 @@ function spawnPersistent(key, name, shutdownAfter, snapshotEvery, onCrash, decod try { tmp = Curry._3(func, state$1, Curry._1(decoder$1, msg), mapPersistentCtx(ctx, encoder$1)); } - catch (raw_err) { + catch (raw_err){ tmp = Promise.reject(Js_exn.internalToOCamlException(raw_err)); } return tmp.then((function (result) { - return Promise.resolve(Curry._1(stateEncoder$1, result)); - })); + return Promise.resolve(Curry._1(stateEncoder$1, result)); + })); }; var untypedRef = Nact.spawnPersistent(param[0], f, key, Js_null_undefined.fromOption(name), options); return /* ActorRef */[untypedRef]; @@ -377,36 +377,36 @@ function nobody() { function spawnAdapter(parent, mapping) { return spawnStateless(/* None */0, /* None */0, /* None */0, parent, (function (msg, _) { - return Promise.resolve(dispatch(parent, Curry._1(mapping, msg))); - })); + return Promise.resolve(dispatch(parent, Curry._1(mapping, msg))); + })); } function start(name, persistenceEngine, logger, _) { var plugins = persistenceEngine ? /* :: */[ - Nact.configurePersistence(persistenceEngine[0]), + Nact.configurePersistence(persistenceEngine[0]), /* [] */0 - ] : /* [] */0; + ] : /* [] */0; var plugins$1; if (logger) { var logger$1 = logger[0]; plugins$1 = /* :: */[ Nact.configureLogging((function (param) { - var loggingActorFunction = logger$1; - var system = param; - var loggerActor = Curry._1(loggingActorFunction, /* ActorRef */[system]); - return spawnAdapter(loggerActor, fromJsLog)[0]; - })), + var loggingActorFunction = logger$1; + var system = param; + var loggerActor = Curry._1(loggingActorFunction, /* ActorRef */[system]); + return spawnAdapter(loggerActor, fromJsLog)[0]; + })), plugins ]; } else { plugins$1 = plugins; } var plugins$2 = name ? /* :: */[ - { - name: name[0] - }, - plugins$1 - ] : plugins$1; + { + name: name[0] + }, + plugins$1 + ] : plugins$1; if (plugins$2) { var match = plugins$2[1]; var a = plugins$2[0]; @@ -437,11 +437,11 @@ function query(timeout, param, msgF) { return Curry._1(msgF, /* ActorRef */[tempReference]); }; return Nact.query(param[0], f, timeout).catch((function () { - return Promise.reject([ - QueryTimeout, - timeout - ]); - })); + return Promise.reject([ + QueryTimeout, + timeout + ]); + })); } var seconds = 1000;