Skip to content

Commit

Permalink
update:events
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Mar 22, 2017
0 parents commit d6bf47c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
10 changes: 10 additions & 0 deletions . babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": ["transform-class-properties"],
"presets": [
["env", {
"targets": {
"browsers": ["last 2 versions", "safari >= 7"]
}
}]
]
}
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
.DS_Store
dist

# editor config
.vscode/
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "o2_base",
"version": "0.0.1",
"description": "o2_base",
"main": "index.js",
"scripts": {
"build": "rm -rf dist && babel src -d dist"
},
"keywords": [
"base"
],
"author": "luckyadam",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-preset-env": "^1.2.2"
}
}
77 changes: 77 additions & 0 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Events {
static eventSplitter = /\s+/;
constructor (opts) {
if (typeof opts !== 'undefined' && opts.callbacks) {
this.callbacks = opts.callbacks;
} else {
this.callbacks = {};
}
}

on (events, callback, context) {
let calls, event, node, tail, list;
if (!callback) {
return this;
}
events = events.split(Events.eventSplitter);
calls = this.callbacks;
while (event = events.shift()) {
list = calls[event];
node = list ? list.tail : {};
node.next = tail = {};
node.context = context;
node.callback = callback;
calls[event] = {
tail: tail,
next: list ? list.next : node
};
}
return this;
}

off (events, callback, context) {
let event, calls, node, tail, cb, ctx;
if (!(calls = this.callbacks)) {
return this;
}
if (!(events || callback || context)) {
delete this.callbacks;
return this;
}
events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
while (event = events.shift()) {
node = calls[event];
delete calls[event];
if (!node || !(callback || context)) {
continue;
}
tail = node.tail;
while ((node = node.next) !== tail) {
cb = node.callback;
ctx = node.context;
if ((callback && cb !== callback) || (context && ctx !== context)) {
this.on(event, cb, ctx);
}
}
}
return this;
}

trigger (events) {
let event, node, calls, tail, args, all, rest;
if (!(calls = this.callbacks)) {
return this;
}
events = events.split(Events.eventSplitter);
rest = [].slice.call(arguments, 1);
while (event = events.shift()) {
if (node = calls[event]) {
tail = node.tail;
while ((node = node.next) !== tail) {
node.callback.apply(node.context || this, rest);
}
}
}
return this;
}
}
14 changes: 14 additions & 0 deletions src/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Widget {
constructor ({ $el }) {
this.$el = $el;
this.template = '';
}

fetch () {

}

update () {

}
}

0 comments on commit d6bf47c

Please sign in to comment.