Skip to content

Commit

Permalink
Updated to v6 of nact and added name to system
Browse files Browse the repository at this point in the history
  • Loading branch information
ncthbrt committed Apr 4, 2018
1 parent d4b1c3b commit cd868f7
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .bsb.lock
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89322
98937
8 changes: 8 additions & 0 deletions __tests__/Actor_Test.re
Original file line number Diff line number Diff line change
Expand Up @@ -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(), ());
Expand Down
246 changes: 123 additions & 123 deletions src/Nact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
}
}

Expand All @@ -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
]);
}
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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));
}
};
Expand All @@ -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);
}
};
Expand All @@ -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]),
Expand All @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cd868f7

Please sign in to comment.