浏览代码

реализовал воркер и канал сообщений

kpmy 10 年之前
父节点
当前提交
ac49ac84d7
共有 21 个文件被更改,包括 7334 次插入75 次删除
  1. 28 0
      bus/msg.go
  2. 6 0
      example/index.dart
  3. 16 0
      example/index.html
  4. 0 11
      example/sim3.dart
  5. 2 2
      gopherjs_build.bat
  6. 0 0
      lib/netlist/counter.yml
  7. 0 0
      lib/netlist/demo.yml
  8. 0 0
      lib/netlist/sm.yml
  9. 0 0
      lib/netlist/sm3.yml
  10. 0 0
      lib/netlist/sm3r.yml
  11. 0 0
      lib/netlist/smr.yml
  12. 0 0
      lib/netlist/test1.yml
  13. 0 8
      lib/sim3.dart
  14. 3620 0
      lib/src/sim3.js
  15. 0 0
      lib/src/sim3.js.map
  16. 12 8
      lib/src/sim3_base.dart
  17. 3 8
      pubspec.yaml
  18. 27 17
      sim.go
  19. 3620 0
      sim3.js
  20. 0 0
      sim3.js.map
  21. 0 21
      test/all_test.dart

+ 28 - 0
bus/msg.go

@@ -0,0 +1,28 @@
+package bus
+
+import (
+	"github.com/gopherjs/gopherjs/js"
+	"github.com/mitchellh/mapstructure"
+	"ypk/assert"
+)
+
+type Msg struct {
+	Typ string
+}
+
+type Handler func(m *Msg)
+
+func Process(m *Msg) {
+	assert.For(m != nil, 20)
+	js.Global.Call("postMessage", m)
+}
+
+func Init(handler Handler) {
+	js.Global.Set("onmessage", func(oEvent *js.Object) {
+		data := oEvent.Get("data").Interface()
+		m := &Msg{}
+		err := mapstructure.Decode(data, m)
+		assert.For(err == nil, 40)
+		handler(m)
+	})
+}

+ 6 - 0
example/index.dart

@@ -0,0 +1,6 @@
+import 'dart:html';
+import 'package:sim3/sim3.dart';
+
+main() async{
+  var w = new Sim3Worker();
+}

+ 16 - 0
example/index.html

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+
+<html>
+  <head>
+  	<meta charset="utf-8">
+  	<meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>index</title>
+  </head>
+ 
+  <body>   
+    <p id="text"></p>
+    <script type="application/dart" src="index.dart"></script>
+    <!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
+    <script src="packages/browser/dart.js"></script>
+  </body>
+</html>

+ 0 - 11
example/sim3.dart

@@ -1,11 +0,0 @@
-// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
-// is governed by a BSD-style license that can be found in the LICENSE file.
-
-library sim3.example;
-
-import 'package:sim3/sim3.dart';
-
-main() {
-  var awesome = new Awesome();
-  print('awesome: ${awesome.isAwesome}');
-}

+ 2 - 2
gopherjs_build.bat

@@ -1,4 +1,4 @@
 @echo off
 gopherjs build
-copy sim3.js web\go /Y
-copy sim3.js.map web\go /Y
+copy sim3.js lib\src\ /Y
+copy sim3.js.map lib\src\ /Y

+ 0 - 0
netlist/counter.yml → lib/netlist/counter.yml


+ 0 - 0
netlist/demo.yml → lib/netlist/demo.yml


+ 0 - 0
netlist/sm.yml → lib/netlist/sm.yml


+ 0 - 0
netlist/sm3.yml → lib/netlist/sm3.yml


+ 0 - 0
netlist/sm3r.yml → lib/netlist/sm3r.yml


+ 0 - 0
netlist/smr.yml → lib/netlist/smr.yml


+ 0 - 0
netlist/test1.yml → lib/netlist/test1.yml


+ 0 - 8
lib/sim3.dart

@@ -1,11 +1,3 @@
-// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
-// is governed by a BSD-style license that can be found in the LICENSE file.
-
-/// The sim3 library.
-///
-/// This is an awesome library. More dartdocs go here.
 library sim3;
 
-// TODO: Export any libraries intended for clients of this package.
-
 export 'src/sim3_base.dart';

文件差异内容过多而无法显示
+ 3620 - 0
lib/src/sim3.js


文件差异内容过多而无法显示
+ 0 - 0
lib/src/sim3.js.map


+ 12 - 8
lib/src/sim3_base.dart

@@ -1,11 +1,15 @@
-// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
-// is governed by a BSD-style license that can be found in the LICENSE file.
+library sim3.base;
 
-// TODO: Put public facing types in this file.
+import 'dart:html';
 
-library sim3.base;
+class Sim3Worker{
+
+  Worker inner;
 
-/// Checks if you are awesome. Spoiler: you are.
-class Awesome {
-  bool get isAwesome => true;
-}
+  Sim3Worker(){
+    this.inner = new Worker("packages/sim3/src/sim3.js");
+    this.inner.onMessage.listen((m){
+      this.inner.postMessage({'Typ': 'init'});
+    });
+  }
+}

+ 3 - 8
pubspec.yaml

@@ -1,10 +1,5 @@
 name: sim3
-description: >
-  A library useful for applications or for sharing on pub.dartlang.org.
 version: 0.0.1
-#author: <your name> <email@example.com>
-#homepage: https://www.example.com
-#dependencies:
-#  lib_name: any
-dev_dependencies:
-  unittest: any
+description: A library useful for applications or for sharing on pub.dartlang.org.
+dependencies:
+  browser: any

+ 27 - 17
sim.go

@@ -2,11 +2,7 @@ package main
 
 import (
 	"log"
-	//	"github.com/ivpusic/neo"
-	//	"github.com/ivpusic/neo-cors"
-	//"runtime"
-	//	"sim3/api"
-	"github.com/gopherjs/gopherjs/js"
+	"sim3/bus"
 	_ "sim3/ncl/extra"
 	"sim3/ncl/tool"
 	"sim3/portable"
@@ -17,8 +13,13 @@ var wg *sync.WaitGroup = &sync.WaitGroup{}
 
 func init() {
 	tool.Src = portable.DataSource
-	//runtime.GOMAXPROCS(1)
-	wg.Add(1)
+}
+
+var busChan chan *bus.Msg
+
+//этот хэндлер только пишет сообщения в канал главной горутины
+func busHandler(m *bus.Msg) {
+	busChan <- m
 }
 
 func load() {
@@ -26,19 +27,28 @@ func load() {
 	t.F("counter.yml")
 }
 
+//этот хэндлер обрабатывает сообщения в рамках главной горутины
+func handle(m *bus.Msg) {
+	switch m.Typ {
+	case "init":
+		load()
+	}
+}
+
 func main() {
-	js.Global.Get("self")
 	log.Println("sim3 started")
-
-	/*	nw := func() {
-			app := neo.App()
-			app.Use(cors.Init())
-			app.Get("/tri.json", api.Tri)
-			app.Start()
+	bus.Init(busHandler)
+	busChan = make(chan *bus.Msg)
+	wg.Add(1)
+	go func(wg *sync.WaitGroup, c chan *bus.Msg) {
+		bus.Process(&bus.Msg{Typ: "init"})
+		for {
+			select {
+			case m := <-c:
+				handle(m)
+			}
 		}
-		go nw()
-	*/
-	load()
+	}(wg, busChan)
 	wg.Wait()
 	log.Println("sim3 closed")
 }

文件差异内容过多而无法显示
+ 3620 - 0
sim3.js


文件差异内容过多而无法显示
+ 0 - 0
sim3.js.map


+ 0 - 21
test/all_test.dart

@@ -1,21 +0,0 @@
-// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
-// is governed by a BSD-style license that can be found in the LICENSE file.
-
-library sim3.test;
-
-import 'package:unittest/unittest.dart';
-import 'package:sim3/sim3.dart';
-
-main() {
-  group('A group of tests', () {
-    Awesome awesome;
-
-    setUp(() {
-      awesome = new Awesome();
-    });
-
-    test('First Test', () {
-      expect(awesome.isAwesome, isTrue);
-    });
-  });
-}

部分文件因为文件数量过多而无法显示