UNPKG

fractal-core

Version:

A minimalist and well crafted app, content or component is our conviction

80 lines 3.19 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ava_1 = require("ava"); const worker_1 = require("./worker"); ava_1.default('Should run a Module in a worker', (t) => __awaiter(this, void 0, void 0, function* () { const results = { interfaceValue: 0, groupValue: 0, taskValue: 0, }; const moduleDef = { Root: { state: { value: 10 }, inputs: (s, F) => ({ onInit: () => __awaiter(this, void 0, void 0, function* () { F.task('myTask', { value: 3 }); }), }), interfaces: { myInterface: (s, F) => __awaiter(this, void 0, void 0, function* () { return ({ value: s.value, group: F.ctx.groups.myGroup.value }); }), }, groups: { myGroup: { value: 7 } }, }, interfaces: { myInterface: mod => ({ state: {}, handle: (id, value) => __awaiter(this, void 0, void 0, function* () { results.interfaceValue = value.value; results.groupValue = value.group; }), destroy: () => { }, }), }, groups: { myGroup: mod => ({ state: {}, handle: (id, value) => __awaiter(this, void 0, void 0, function* () { mod.setGroup(id, 'myGroup', { value: value.value + 1 }); }), destroy: () => { }, }), }, tasks: { myTask: mod => ({ state: {}, handle: (id, value) => __awaiter(this, void 0, void 0, function* () { results.taskValue = value.value; }), destroy: () => { }, }), }, }; // worker side const workerContext = { postMessage: msg => setTimeout(() => worker.onmessage({ data: msg })), }; worker_1.runInWorker(moduleDef, undefined, workerContext); // Main thread const worker = { postMessage: msg => setTimeout(() => workerContext.onmessage({ data: msg })), }; yield worker_1.runWorker(Object.assign({ worker }, moduleDef)); t.is(results.interfaceValue, 10, 'should execute interface'); t.is(results.groupValue, 8, 'should execute group'); t.is(results.taskValue, 3, 'should execute task'); })); //# sourceMappingURL=worker.spec.js.map