Pomelo的component组件
pomelo的核心是由一系列松耦合的component组成,同时我们也可以实现我们自己的component来完成一些自己定制的功能。对于我们的聊天应用,我们尝试给其增加一个component,目的是展示如何增加一个component,以及component的生命周期管理,而不会特别关注这个component的实际功能。我们现在就给其增加一个component HelloWorld,这个component仅仅在master服务器上加载运行,在master服务器的话,它将每隔一段时间在console上打印出来一个HelloWorld,具体的时间间隔由opts配置来指定
在app下建立components/HelloWorld.js文件, 大致代码如下:
module.exports = function(app, opts) {
return new HelloWorld(app, opts);
};
var DEFAULT_INTERVAL = 3000;
var HelloWorld = function(app, opts) {
this.app = app;
this.interval = opts.interval | DEFAULT_INTERVAL;
this.timerId = null;
};
HelloWorld.name = '__HelloWorld__';
HelloWorld.prototype.start = function(cb) {
console.log('Hello World Start');
var self = this;
this.timerId = setInterval(function() {
console.log(self.app.getServerId() + ": Hello World!");
}, this.interval);
process.nextTick(cb);
}
HelloWorld.prototype.afterStart = function (cb) {
console.log('Hello World afterStart');
process.nextTick(cb);
}
HelloWorld.prototype.stop = function(force, cb) {
cosole.log('Hello World stop');
clearInterval(this.timerId);
process.nextTick(cb);
}
我们看到每一个component一般都要定义start,afterStart,stop这些hook函数,供pomelo管理其生命周期时进行调用。对于component的启动,pomelo总是先调用其加载的每一个component提供的start函数,当全部调用完后,才会去调用其加载的每一个component的afterStart方法,这里总是按顺序调用的。因为调用afterStart的时候,所有component的start已经调用完毕,可以添加一些需要全局就绪的工作。stop用于程序结束时对component进行清理时使用
在app.js配置如下:
// app.js
var helloWorld = require('./app/components/HelloWorld'); app.configure('production|development', 'master', function() {
app.load(helloWorld, {interval: 5000});
});
Pomelo的component组件的更多相关文章
- hibernate hibernate.cfg.xml component 组件
1.为什么使用component组件? 当一个表的列数目比较多时,可以根据属性分类,将一个java对象拆分为几个对象. 数据库还是一张表,不过有多个对象与之对应. 2.实例 2.1 Java 对象: ...
- [原]Unity3D深入浅出 - 认识开发环境中的Component(组件)菜单
Component(组件)是用来添加到GameObject对象上的一组相关属性,本质上每个组件都是一个类的实例,比如在Cube上添加一个Mesh网格,即面向对象的思维方式可以理解成Cube对象里包含了 ...
- Salesforce Lightning开发学习(二)Component组件开发实践
lightning的组件区分标准组件.自定义组件和AppExchange组件.标准组件由SF提供,自定义组件由developer自行开发,AppExchange组件由合作伙伴建立.下面我们写一个简单的 ...
- 微信小程序template模板与component组件的区别和使用
前言: 除了component,微信小程序中还有另一种组件化你的方式template模板,这两者之间的区别是,template主要是展示,方法则需要在调用的页面中定义.而component组件则有自己 ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- 一篇文章看懂angularjs component组件
壹 ❀ 引 我在 angularjs 一篇文章看懂自定义指令directive 一文中详细介绍了directive基本用法与完整属性介绍.directive是个很神奇的存在,你可以不设置templa ...
- Unity编程标准导引-3.1 Component 组件脚本及其基本生命周期
本文为博主原创文章,欢迎转载,请保留出处:http://blog.csdn.net/andrewfan 3.1组件 Component 组件是Unity中最核心的一个概念,它是一切编程的基础.没有组件 ...
- Vue2.0 【第二季】第7节 Component 组件 props 属性设置
目录 Vue2.0 [第二季]第7节 Component 组件 props 属性设置 第7节 Component 组件 props 属性设置 一.定义属性并获取属性值 二.属性中带' - '的处理方式 ...
- angular2 学习笔记 ( Component 组件)
refer : https://angular.cn/docs/ts/latest/guide/template-syntax.html https://angular.cn/docs/ts/late ...
随机推荐
- unity3d 多人寻路堵塞堆叠问题
使用unity提供的NavMeshAgent寻路,当有多个agent一起寻路时总会出现堵塞堆叠的问题. 本人使用了一个非常粗劣简单的方案解决此问题,当然跟魔兽的寻路完全没得比,但保证有比较好的性能,且 ...
- 利用html5压缩图片,产出base64图片
/* 将页面选择的图片等比压缩成指定大小(长边固定) file:图片文件 callBack:回调函数 maxLen:长边的长度*/function makePic(file,callBack,maxL ...
- Redis缓存服务搭建及实现数据读写--转载
来自 http://www.cnblogs.com/lc-chenlong/p/3218157.html 1. 下载安装Redis 下载地址:https://github.com/MSOpenTec ...
- linux command ---1
查看Linux的内核版本 当前系统的发行版信息(distribution):lsb_release -a , lsb(linux standard Base) and distribution inf ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- BZOJ 2276 Temperature
two-pointer型单调队列.... #include<iostream> #include<cstdio> #include<cstring> #includ ...
- JS基础知识(五)
内置对象 定义:JS语言自己定义的一些对象,供开发者使用. 常用内置对象: 1.Arguments对象 arguments.length 返回实参的个数 他只在正运行的函数内部使用.argument ...
- Android Studio:Gradle DSL method not found: 'runProguard()'
Android Studio发布了新的1.0版,更新之后却发现原来在0.8下面正常的项目编译失败了,从报错上来看是卡在gradle上面. Gradle DSL method not found: 'r ...
- create schema 与create database的区别
概论第四版中增加了create schema (第三版好像没有这个内容),但是却没有提到create database.相反目前在大多数DBMS中(如SQL Server2000.Mysql等)都有 ...
- hibernate--多对多单向关联 (重点!!!)
老师和学生的关系, 一个老师对多个学生, 一个学生也对应多个老师. 数据库会需要3个表, 一个老师表, 一个学生表, 一个老师对应学生表. 单向: 老师知道自己有多少学生, 但是学生不知道自己有多少个 ...