From ad5afa28f9daf497208216c6bb1eddfa46411557 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 2 Nov 2018 17:09:55 +0000 Subject: [PATCH] @syndicate-lang/driver-timer --- Makefile | 2 +- packages/driver-timer/.babelrc | 4 ++ packages/driver-timer/Makefile | 8 +++ packages/driver-timer/package.json | 18 ++++++ packages/driver-timer/src/index.js | 77 ++++++++++++++++++++++++ packages/syntax-playground/package.json | 1 + packages/syntax-playground/src/ticker.js | 21 +++---- 7 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 packages/driver-timer/.babelrc create mode 100644 packages/driver-timer/Makefile create mode 100644 packages/driver-timer/package.json create mode 100644 packages/driver-timer/src/index.js diff --git a/Makefile b/Makefile index 4ab5009..d48c297 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -MAKEABLE_PACKAGES=syntax syntax-playground +MAKEABLE_PACKAGES=syntax driver-timer syntax-playground all: for p in $(MAKEABLE_PACKAGES); do $(MAKE) -C packages/$$p; done diff --git a/packages/driver-timer/.babelrc b/packages/driver-timer/.babelrc new file mode 100644 index 0000000..8991585 --- /dev/null +++ b/packages/driver-timer/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": [ "@babel/preset-env" ], + "plugins": [ "@syndicate-lang/syntax/plugin" ] +} diff --git a/packages/driver-timer/Makefile b/packages/driver-timer/Makefile new file mode 100644 index 0000000..64d6f28 --- /dev/null +++ b/packages/driver-timer/Makefile @@ -0,0 +1,8 @@ +.PHONY: build + +build: + mkdir -p lib + npx syndicate-babel src --out-dir lib + +clean: + rm -rf lib diff --git a/packages/driver-timer/package.json b/packages/driver-timer/package.json new file mode 100644 index 0000000..a8c0ab1 --- /dev/null +++ b/packages/driver-timer/package.json @@ -0,0 +1,18 @@ +{ + "name": "@syndicate-lang/driver-timer", + "version": "0.0.0", + "description": "Time and timer driver for Syndicate/js", + "main": "lib/index.js", + "repository": "github:syndicate-lang/syndicate-js", + "author": "Tony Garnock-Jones ", + "license": "GPL-3.0+", + "homepage": "https://github.com/syndicate-lang/syndicate-js/tree/master/packages/driver-timer", + "devDependencies": { + "@babel/core": "^7.1.2", + "@babel/preset-env": "^7.1.0", + "@syndicate-lang/syntax": "^0.0.7" + }, + "dependencies": { + "@syndicate-lang/core": "^0.0.5" + } +} diff --git a/packages/driver-timer/src/index.js b/packages/driver-timer/src/index.js new file mode 100644 index 0000000..9842344 --- /dev/null +++ b/packages/driver-timer/src/index.js @@ -0,0 +1,77 @@ +//--------------------------------------------------------------------------- +// @syndicate-lang/syntax-test, a demo of Syndicate extensions to JS. +// Copyright (C) 2016-2018 Tony Garnock-Jones +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +//--------------------------------------------------------------------------- + +import { Observe, Dataspace } from "@syndicate-lang/core"; + +export { PeriodicTick, TimeLaterThan }; + +message type PeriodicTick(intervalMS); +assertion type TimeLaterThan(deadlineMS); + +spawn named 'driver-timer/PeriodicTick' { + during Observe(PeriodicTick($intervalMS)) spawn named ('PeriodicTick('+intervalMS+')') { + let handle = null; + let finish = Dataspace.backgroundTask(); + on start { + handle = setInterval(Dataspace.wrapExternal(() => { + << PeriodicTick(intervalMS); + }, intervalMS)); + } + on stop { + if (handle) { + clearInterval(handle); + handle = null; + } + if (finish) { + finish(); + finish = null; + } + } + } +} + +spawn named 'driver-timer/TimeLaterThan' { + during Observe(TimeLaterThan($deadlineMS)) spawn named ('TimeLaterThan('+deadlineMS+')') { + let handle = null; + let finish = Dataspace.backgroundTask(); + on start { + let delta = deadlineMS - (+(new Date())); + console.log('Observation of TimeLaterThan', deadlineMS, delta); + handle = setTimeout(Dataspace.wrapExternal(() => { + console.log('Firing TimeLaterThan', deadlineMS); + handle = null; + finish(); + finish = null; + react { + assert TimeLaterThan(deadlineMS); + } + }, delta)); + } + on stop { + console.log('Retraction of observation of TimeLaterThan', deadlineMS); + if (handle) { + clearTimeout(handle); + handle = null; + } + if (finish) { + finish(); + finish = null; + } + } + } +} diff --git a/packages/syntax-playground/package.json b/packages/syntax-playground/package.json index 2802e30..71b6b70 100644 --- a/packages/syntax-playground/package.json +++ b/packages/syntax-playground/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@syndicate-lang/core": "^0.0.5", + "@syndicate-lang/driver-timer": "^0.0.0", "webpack": "^4.23.1", "webpack-cli": "^3.1.2" } diff --git a/packages/syntax-playground/src/ticker.js b/packages/syntax-playground/src/ticker.js index 667da92..ff3b8de 100644 --- a/packages/syntax-playground/src/ticker.js +++ b/packages/syntax-playground/src/ticker.js @@ -18,29 +18,22 @@ import { Dataspace } from "@syndicate-lang/core"; -message type Tick(); +const Timer = activate require("@syndicate-lang/driver-timer"); +const TimeLaterThan = Timer.TimeLaterThan; spawn named 'ticker' { field this.counter = 0; + field this.deadline = +(new Date()); on start { console.log('ticker starting'); } on stop { console.log('ticker stopping'); } - on message Tick() { + on asserted TimeLaterThan(this.deadline) { this.counter++; console.log('tick', new Date(), this.counter); - if (this.counter < 5) { - Dataspace.backgroundTask((finish) => { - setTimeout(Dataspace.wrapExternal(() => { - << Tick(); - finish(); - }), 1000); - }); - } + this.deadline += 1000; } - on start { - console.log('sending first tick'); - << Tick(); - } + stop on (this.counter == 5); } +