{"_id":"5651c8f4011c003500eca8c7","__v":15,"githubsync":"","user":"5617a0a4a410c90d00c612f4","category":{"_id":"5651c89a852cfb3500a69830","__v":2,"version":"5619a39913874c0d007038e9","pages":["5651c8f4011c003500eca8c7","5657ad06a795770d00a2afb5"],"project":"5617a1f5a410c90d00c612f5","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-11-22T13:52:26.932Z","from_sync":false,"order":3,"slug":"examples","title":"Examples"},"version":{"_id":"5619a39913874c0d007038e9","project":"5617a1f5a410c90d00c612f5","__v":5,"createdAt":"2015-10-10T23:47:37.777Z","releaseDate":"2015-10-10T23:47:37.777Z","categories":["5619a39a13874c0d007038ea","5619a39a13874c0d007038eb","5619a39a13874c0d007038ec","5619a39a13874c0d007038ed","5651062a3b4d113500ed22d1","5651c89a852cfb3500a69830","565505ad055aa40d006f0ca7","56c219d8d3199a0d00694be6"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"Orange Arrow","version_clean":"0.2.0","version":"0.2"},"project":"5617a1f5a410c90d00c612f5","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-11-22T13:53:56.976Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {EventEmitter} from \\\"events\\\";\\nimport {task, TaskContext} from \\\"natron-core\\\";\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Creating a Custom Context\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class CustomContext extends TaskContext {\\n\\n  log(opts, ...args) {\\n    let lvl = this.stack.length - 1;\\n    args.unshift(opts && opts.ico || \\\" \\\");\\n    if (lvl) {\\n      let indent = \\\" \\\".repeat(lvl * 2 - 1);\\n      args.unshift(indent);\\n    }\\n    return console.log(...args);\\n  }\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Listening for Events\n\n`start` ... `e = {task, context}`\n`finish` ... `e = {task, context, value}`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let ea = new EventEmitter();\\n\\nea.on(\\\"start\\\", (e) => {\\n  let {task, context} = e;\\n  let name = task.name || getAnonymousName(e);\\n  context.log({ico: \\\"⇢\\\"}, name);\\n});\\n\\nea.on(\\\"finish\\\", (e) => {\\n  let {task, context} = e;\\n  let name = task.name || getAnonymousName(e);\\n  context.log({ico: \\\"⇠\\\"}, name);\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n`getAnonymousName`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let un = new WeakMap();\\n\\nfunction getAnonymousName(e) {\\n  let name = un.get(e);\\n  if (!name) {\\n    name = `<task_${(Math.random() * 1e2)|0}>`;\\n    un.set(e, name);\\n  }\\n  return name;\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Defining Tasks\n\n`t`: `a => (a || b) => b`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function a(val, ico = \\\"a\\\") {\\n  if (val instanceof Array) {\\n    val = `[${val.join(\\\", \\\")}]`;\\n  }\\n  this.log({ico}, \\\"val\\\", \\\"=\\\", val);\\n  return `(${val}<${ico}>)`;\\n}\\n\\nfunction b(val) {\\n  return a.call(this, val, \\\"b\\\");\\n}\\n\\nlet t = task([a, [[a, b]], b], {\\n  name: \\\"ab-task\\\",\\n  options: {pipe: true},\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Run\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let context = new CustomContext({\\n  args: [\\\"foo\\\"],\\n  eventAggregator: ea,\\n});\\n\\n(t.runWithContext(context)\\n  .then((val) => {\\n    console.log(\\\"val\\\", \\\"=\\\", val);\\n  })\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Output\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"⇢ ab-task\\n  ⇢ a\\n  a val = foo\\n  ⇠ a\\n  ⇢ <task_42>\\n    ⇢ a\\n    ⇢ b\\n    a val = (foo<a>)\\n    b val = (foo<a>)\\n    ⇠ a\\n    ⇠ b\\n  ⇠ <task_42>\\n  ⇢ b\\n  b val = [((foo<a>)<a>), ((foo<a>)<b>)]\\n  ⇠ b\\n⇠ ab-task\\nval = ([((foo<a>)<a>), ((foo<a>)<b>)]<b>)\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","excerpt":"Using Node.js' EventEmitter to handle events","slug":"example-context-and-event-aggregator","type":"basic","title":"Context & Event Aggregator"}

Context & Event Aggregator

Using Node.js' EventEmitter to handle events

[block:code] { "codes": [ { "code": "import {EventEmitter} from \"events\";\nimport {task, TaskContext} from \"natron-core\";", "language": "javascript" } ] } [/block] ## Creating a Custom Context [block:code] { "codes": [ { "code": "class CustomContext extends TaskContext {\n\n log(opts, ...args) {\n let lvl = this.stack.length - 1;\n args.unshift(opts && opts.ico || \" \");\n if (lvl) {\n let indent = \" \".repeat(lvl * 2 - 1);\n args.unshift(indent);\n }\n return console.log(...args);\n }\n}", "language": "javascript" } ] } [/block] ## Listening for Events `start` ... `e = {task, context}` `finish` ... `e = {task, context, value}` [block:code] { "codes": [ { "code": "let ea = new EventEmitter();\n\nea.on(\"start\", (e) => {\n let {task, context} = e;\n let name = task.name || getAnonymousName(e);\n context.log({ico: \"⇢\"}, name);\n});\n\nea.on(\"finish\", (e) => {\n let {task, context} = e;\n let name = task.name || getAnonymousName(e);\n context.log({ico: \"⇠\"}, name);\n});", "language": "javascript" } ] } [/block] `getAnonymousName` [block:code] { "codes": [ { "code": "let un = new WeakMap();\n\nfunction getAnonymousName(e) {\n let name = un.get(e);\n if (!name) {\n name = `<task_${(Math.random() * 1e2)|0}>`;\n un.set(e, name);\n }\n return name;\n}", "language": "javascript" } ] } [/block] ## Defining Tasks `t`: `a => (a || b) => b` [block:code] { "codes": [ { "code": "function a(val, ico = \"a\") {\n if (val instanceof Array) {\n val = `[${val.join(\", \")}]`;\n }\n this.log({ico}, \"val\", \"=\", val);\n return `(${val}<${ico}>)`;\n}\n\nfunction b(val) {\n return a.call(this, val, \"b\");\n}\n\nlet t = task([a, [[a, b]], b], {\n name: \"ab-task\",\n options: {pipe: true},\n});", "language": "javascript" } ] } [/block] ## Run [block:code] { "codes": [ { "code": "let context = new CustomContext({\n args: [\"foo\"],\n eventAggregator: ea,\n});\n\n(t.runWithContext(context)\n .then((val) => {\n console.log(\"val\", \"=\", val);\n })\n);", "language": "javascript" } ] } [/block] ## Output [block:code] { "codes": [ { "code": "⇢ ab-task\n ⇢ a\n a val = foo\n ⇠ a\n ⇢ <task_42>\n ⇢ a\n ⇢ b\n a val = (foo<a>)\n b val = (foo<a>)\n ⇠ a\n ⇠ b\n ⇠ <task_42>\n ⇢ b\n b val = [((foo<a>)<a>), ((foo<a>)<b>)]\n ⇠ b\n⇠ ab-task\nval = ([((foo<a>)<a>), ((foo<a>)<b>)]<b>)", "language": "text" } ] } [/block]